Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/namei.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Some corrections by tytso. |
| 10 | */ |
| 11 | |
| 12 | /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname |
| 13 | * lookup logic. |
| 14 | */ |
| 15 | /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/init.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 19 | #include <linux/export.h> |
David S. Miller | 4469690 | 2012-05-23 20:12:50 -0700 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/pagemap.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 25 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/personality.h> |
| 27 | #include <linux/security.h> |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 28 | #include <linux/ima.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/syscalls.h> |
| 30 | #include <linux/mount.h> |
| 31 | #include <linux/audit.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 32 | #include <linux/capability.h> |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 33 | #include <linux/file.h> |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 34 | #include <linux/fcntl.h> |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 35 | #include <linux/device_cgroup.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 36 | #include <linux/fs_struct.h> |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 37 | #include <linux/posix_acl.h> |
Linus Torvalds | 99d263d | 2014-09-13 11:30:10 -0700 | [diff] [blame] | 38 | #include <linux/hash.h> |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 39 | #include <linux/bitops.h> |
Eric W. Biederman | aeaa4a7 | 2016-07-23 11:20:44 -0500 | [diff] [blame] | 40 | #include <linux/init_task.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 41 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 43 | #include "internal.h" |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 44 | #include "mount.h" |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* [Feb-1997 T. Schoebel-Theuer] |
| 47 | * Fundamental changes in the pathname lookup mechanisms (namei) |
| 48 | * were necessary because of omirr. The reason is that omirr needs |
| 49 | * to know the _real_ pathname, not the user-supplied one, in case |
| 50 | * of symlinks (and also when transname replacements occur). |
| 51 | * |
| 52 | * The new code replaces the old recursive symlink resolution with |
| 53 | * an iterative one (in case of non-nested symlink chains). It does |
| 54 | * this with calls to <fs>_follow_link(). |
| 55 | * As a side effect, dir_namei(), _namei() and follow_link() are now |
| 56 | * replaced with a single function lookup_dentry() that can handle all |
| 57 | * the special cases of the former code. |
| 58 | * |
| 59 | * With the new dcache, the pathname is stored at each inode, at least as |
| 60 | * long as the refcount of the inode is positive. As a side effect, the |
| 61 | * size of the dcache depends on the inode cache and thus is dynamic. |
| 62 | * |
| 63 | * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink |
| 64 | * resolution to correspond with current state of the code. |
| 65 | * |
| 66 | * Note that the symlink resolution is not *completely* iterative. |
| 67 | * There is still a significant amount of tail- and mid- recursion in |
| 68 | * the algorithm. Also, note that <fs>_readlink() is not used in |
| 69 | * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() |
| 70 | * may return different results than <fs>_follow_link(). Many virtual |
| 71 | * filesystems (including /proc) exhibit this behavior. |
| 72 | */ |
| 73 | |
| 74 | /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: |
| 75 | * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL |
| 76 | * and the name already exists in form of a symlink, try to create the new |
| 77 | * name indicated by the symlink. The old code always complained that the |
| 78 | * name already exists, due to not following the symlink even if its target |
| 79 | * is nonexistent. The new semantics affects also mknod() and link() when |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 80 | * the name is a symlink pointing to a non-existent name. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * |
| 82 | * I don't know which semantics is the right one, since I have no access |
| 83 | * to standards. But I found by trial that HP-UX 9.0 has the full "new" |
| 84 | * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the |
| 85 | * "old" one. Personally, I think the new semantics is much more logical. |
| 86 | * Note that "ln old new" where "new" is a symlink pointing to a non-existing |
| 87 | * file does succeed in both HP-UX and SunOs, but not in Solaris |
| 88 | * and in the old Linux semantics. |
| 89 | */ |
| 90 | |
| 91 | /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink |
| 92 | * semantics. See the comments in "open_namei" and "do_link" below. |
| 93 | * |
| 94 | * [10-Sep-98 Alan Modra] Another symlink change. |
| 95 | */ |
| 96 | |
| 97 | /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: |
| 98 | * inside the path - always follow. |
| 99 | * in the last component in creation/removal/renaming - never follow. |
| 100 | * if LOOKUP_FOLLOW passed - follow. |
| 101 | * if the pathname has trailing slashes - follow. |
| 102 | * otherwise - don't follow. |
| 103 | * (applied in that order). |
| 104 | * |
| 105 | * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT |
| 106 | * restored for 2.4. This is the last surviving part of old 4.2BSD bug. |
| 107 | * During the 2.4 we need to fix the userland stuff depending on it - |
| 108 | * hopefully we will be able to get rid of that wart in 2.5. So far only |
| 109 | * XEmacs seems to be relying on it... |
| 110 | */ |
| 111 | /* |
| 112 | * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 113 | * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | * any extra contention... |
| 115 | */ |
| 116 | |
| 117 | /* In order to reduce some races, while at the same time doing additional |
| 118 | * checking and hopefully speeding things up, we copy filenames to the |
| 119 | * kernel data space before using them.. |
| 120 | * |
| 121 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). |
| 122 | * PATH_MAX includes the nul terminator --RR. |
| 123 | */ |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 124 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 125 | #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 126 | |
David Drysdale | 51f39a1 | 2014-12-12 16:57:29 -0800 | [diff] [blame] | 127 | struct filename * |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 128 | getname_flags(const char __user *filename, int flags, int *empty) |
| 129 | { |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 130 | struct filename *result; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 131 | char *kname; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 132 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Jeff Layton | 7ac8626 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 134 | result = audit_reusename(filename); |
| 135 | if (result) |
| 136 | return result; |
| 137 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 138 | result = __getname(); |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 139 | if (unlikely(!result)) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 140 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 142 | /* |
| 143 | * First, try to embed the struct filename inside the names_cache |
| 144 | * allocation |
| 145 | */ |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 146 | kname = (char *)result->iname; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 147 | result->name = kname; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 148 | |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 149 | len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 150 | if (unlikely(len < 0)) { |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 151 | __putname(result); |
| 152 | return ERR_PTR(len); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 153 | } |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 154 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 155 | /* |
| 156 | * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a |
| 157 | * separate struct filename so we can dedicate the entire |
| 158 | * names_cache allocation for the pathname, and re-do the copy from |
| 159 | * userland. |
| 160 | */ |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 161 | if (unlikely(len == EMBEDDED_NAME_MAX)) { |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 162 | const size_t size = offsetof(struct filename, iname[1]); |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 163 | kname = (char *)result; |
| 164 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 165 | /* |
| 166 | * size is chosen that way we to guarantee that |
| 167 | * result->iname[0] is within the same object and that |
| 168 | * kname can't be equal to result->iname, no matter what. |
| 169 | */ |
| 170 | result = kzalloc(size, GFP_KERNEL); |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 171 | if (unlikely(!result)) { |
| 172 | __putname(kname); |
| 173 | return ERR_PTR(-ENOMEM); |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 174 | } |
| 175 | result->name = kname; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 176 | len = strncpy_from_user(kname, filename, PATH_MAX); |
| 177 | if (unlikely(len < 0)) { |
| 178 | __putname(kname); |
| 179 | kfree(result); |
| 180 | return ERR_PTR(len); |
| 181 | } |
| 182 | if (unlikely(len == PATH_MAX)) { |
| 183 | __putname(kname); |
| 184 | kfree(result); |
| 185 | return ERR_PTR(-ENAMETOOLONG); |
| 186 | } |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 189 | result->refcnt = 1; |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 190 | /* The empty path is special. */ |
| 191 | if (unlikely(!len)) { |
| 192 | if (empty) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 193 | *empty = 1; |
Al Viro | 94b5d26 | 2015-02-22 19:38:03 -0500 | [diff] [blame] | 194 | if (!(flags & LOOKUP_EMPTY)) { |
| 195 | putname(result); |
| 196 | return ERR_PTR(-ENOENT); |
| 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 199 | |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 200 | result->uptr = filename; |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 201 | result->aname = NULL; |
Jeff Layton | 7950e38 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 202 | audit_getname(result); |
| 203 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 206 | struct filename * |
| 207 | getname(const char __user * filename) |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 208 | { |
Linus Torvalds | f7493e5 | 2012-03-22 16:10:40 -0700 | [diff] [blame] | 209 | return getname_flags(filename, 0, NULL); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 212 | struct filename * |
| 213 | getname_kernel(const char * filename) |
| 214 | { |
| 215 | struct filename *result; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 216 | int len = strlen(filename) + 1; |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 217 | |
| 218 | result = __getname(); |
| 219 | if (unlikely(!result)) |
| 220 | return ERR_PTR(-ENOMEM); |
| 221 | |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 222 | if (len <= EMBEDDED_NAME_MAX) { |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 223 | result->name = (char *)result->iname; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 224 | } else if (len <= PATH_MAX) { |
Al Viro | 30ce4d1 | 2018-04-08 11:57:10 -0400 | [diff] [blame] | 225 | const size_t size = offsetof(struct filename, iname[1]); |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 226 | struct filename *tmp; |
| 227 | |
Al Viro | 30ce4d1 | 2018-04-08 11:57:10 -0400 | [diff] [blame] | 228 | tmp = kmalloc(size, GFP_KERNEL); |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 229 | if (unlikely(!tmp)) { |
| 230 | __putname(result); |
| 231 | return ERR_PTR(-ENOMEM); |
| 232 | } |
| 233 | tmp->name = (char *)result; |
Paul Moore | 0851854 | 2015-01-21 23:59:56 -0500 | [diff] [blame] | 234 | result = tmp; |
| 235 | } else { |
| 236 | __putname(result); |
| 237 | return ERR_PTR(-ENAMETOOLONG); |
| 238 | } |
| 239 | memcpy((char *)result->name, filename, len); |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 240 | result->uptr = NULL; |
| 241 | result->aname = NULL; |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 242 | result->refcnt = 1; |
Paul Moore | fd3522f | 2015-01-22 00:00:10 -0500 | [diff] [blame] | 243 | audit_getname(result); |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 244 | |
Linus Torvalds | c4ad8f9 | 2014-02-05 12:54:53 -0800 | [diff] [blame] | 245 | return result; |
| 246 | } |
| 247 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 248 | void putname(struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 250 | BUG_ON(name->refcnt <= 0); |
| 251 | |
| 252 | if (--name->refcnt > 0) |
| 253 | return; |
| 254 | |
Al Viro | fd2f7cb | 2015-02-22 20:07:13 -0500 | [diff] [blame] | 255 | if (name->name != name->iname) { |
Paul Moore | 55422d0 | 2015-01-22 00:00:23 -0500 | [diff] [blame] | 256 | __putname(name->name); |
| 257 | kfree(name); |
| 258 | } else |
| 259 | __putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 262 | static int check_acl(struct inode *inode, int mask) |
| 263 | { |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 264 | #ifdef CONFIG_FS_POSIX_ACL |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 265 | struct posix_acl *acl; |
| 266 | |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 267 | if (mask & MAY_NOT_BLOCK) { |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 268 | acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); |
| 269 | if (!acl) |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 270 | return -EAGAIN; |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 271 | /* no ->get_acl() calls in RCU mode... */ |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 272 | if (is_uncached_acl(acl)) |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 273 | return -ECHILD; |
Ari Savolainen | 206b1d0 | 2011-08-06 19:43:07 +0300 | [diff] [blame] | 274 | return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 277 | acl = get_acl(inode, ACL_TYPE_ACCESS); |
| 278 | if (IS_ERR(acl)) |
| 279 | return PTR_ERR(acl); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 280 | if (acl) { |
| 281 | int error = posix_acl_permission(inode, acl, mask); |
| 282 | posix_acl_release(acl); |
| 283 | return error; |
| 284 | } |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 285 | #endif |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 286 | |
| 287 | return -EAGAIN; |
| 288 | } |
| 289 | |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 290 | /* |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 291 | * This does the basic permission checking |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 292 | */ |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 293 | static int acl_permission_check(struct inode *inode, int mask) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 294 | { |
Linus Torvalds | 26cf46b | 2011-05-13 11:51:01 -0700 | [diff] [blame] | 295 | unsigned int mode = inode->i_mode; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 296 | |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 297 | if (likely(uid_eq(current_fsuid(), inode->i_uid))) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 298 | mode >>= 6; |
| 299 | else { |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 300 | if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 301 | int error = check_acl(inode, mask); |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 302 | if (error != -EAGAIN) |
| 303 | return error; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if (in_group_p(inode->i_gid)) |
| 307 | mode >>= 3; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * If the DACs are ok we don't need any capability check. |
| 312 | */ |
Al Viro | 9c2c703 | 2011-06-20 19:06:22 -0400 | [diff] [blame] | 313 | if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 314 | return 0; |
| 315 | return -EACCES; |
| 316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
| 318 | /** |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 319 | * generic_permission - check for access rights on a Posix-like filesystem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | * @inode: inode to check access rights for |
Andreas Gruenbacher | 8fd90c8 | 2011-10-23 23:13:30 +0530 | [diff] [blame] | 321 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | * |
| 323 | * Used to check for read/write/execute permissions on a file. |
| 324 | * We use "fsuid" for this, letting us set arbitrary permissions |
| 325 | * for filesystem access without changing the "normal" uids which |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 326 | * are used for other things. |
| 327 | * |
| 328 | * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk |
| 329 | * request cannot be satisfied (eg. requires blocking or too much complexity). |
| 330 | * It would then be called again in ref-walk mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | */ |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 332 | int generic_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 334 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | /* |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 337 | * Do the basic permission checks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | */ |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 339 | ret = acl_permission_check(inode, mask); |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 340 | if (ret != -EACCES) |
| 341 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 343 | if (S_ISDIR(inode->i_mode)) { |
| 344 | /* DACs are overridable for directories */ |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 345 | if (!(mask & MAY_WRITE)) |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 346 | if (capable_wrt_inode_uidgid(inode, |
| 347 | CAP_DAC_READ_SEARCH)) |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 348 | return 0; |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 349 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | return 0; |
Stephen Smalley | 2a4c224 | 2017-03-10 12:14:18 -0500 | [diff] [blame] | 351 | return -EACCES; |
| 352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | /* |
| 355 | * Searching includes executable on directories, else just read. |
| 356 | */ |
Serge E. Hallyn | 7ea6600 | 2009-12-29 14:50:19 -0600 | [diff] [blame] | 357 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 358 | if (mask == MAY_READ) |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 359 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return 0; |
Stephen Smalley | 2a4c224 | 2017-03-10 12:14:18 -0500 | [diff] [blame] | 361 | /* |
| 362 | * Read/write DACs are always overridable. |
| 363 | * Executable DACs are overridable when there is |
| 364 | * at least one exec bit set. |
| 365 | */ |
| 366 | if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) |
| 367 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE)) |
| 368 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | return -EACCES; |
| 371 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 372 | EXPORT_SYMBOL(generic_permission); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 374 | /* |
| 375 | * We _really_ want to just do "generic_permission()" without |
| 376 | * even looking at the inode->i_op values. So we keep a cache |
| 377 | * flag in inode->i_opflags, that says "this has not special |
| 378 | * permission function, use the fast case". |
| 379 | */ |
| 380 | static inline int do_inode_permission(struct inode *inode, int mask) |
| 381 | { |
| 382 | if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { |
| 383 | if (likely(inode->i_op->permission)) |
| 384 | return inode->i_op->permission(inode, mask); |
| 385 | |
| 386 | /* This gets set once for the inode lifetime */ |
| 387 | spin_lock(&inode->i_lock); |
| 388 | inode->i_opflags |= IOP_FASTPERM; |
| 389 | spin_unlock(&inode->i_lock); |
| 390 | } |
| 391 | return generic_permission(inode, mask); |
| 392 | } |
| 393 | |
Christoph Hellwig | cb23beb | 2008-10-24 09:59:29 +0200 | [diff] [blame] | 394 | /** |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 395 | * sb_permission - Check superblock-level permissions |
| 396 | * @sb: Superblock of inode to check permission on |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 397 | * @inode: Inode to check permission on |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 398 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
| 399 | * |
| 400 | * Separate out file-system wide checks from inode-specific permission checks. |
| 401 | */ |
| 402 | static int sb_permission(struct super_block *sb, struct inode *inode, int mask) |
| 403 | { |
| 404 | if (unlikely(mask & MAY_WRITE)) { |
| 405 | umode_t mode = inode->i_mode; |
| 406 | |
| 407 | /* Nobody gets write access to a read-only fs. */ |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 408 | if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 409 | return -EROFS; |
| 410 | } |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * inode_permission - Check for access rights to a given inode |
| 416 | * @inode: Inode to check permission on |
| 417 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
| 418 | * |
| 419 | * Check for read/write/execute permissions on an inode. We use fs[ug]id for |
| 420 | * this, letting us set arbitrary permissions for filesystem access without |
| 421 | * changing the "normal" UIDs which are used for other things. |
| 422 | * |
| 423 | * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. |
| 424 | */ |
| 425 | int inode_permission(struct inode *inode, int mask) |
| 426 | { |
| 427 | int retval; |
| 428 | |
| 429 | retval = sb_permission(inode->i_sb, inode, mask); |
| 430 | if (retval) |
| 431 | return retval; |
Eric Biggers | 4bfd054 | 2018-01-16 21:44:24 -0800 | [diff] [blame] | 432 | |
| 433 | if (unlikely(mask & MAY_WRITE)) { |
| 434 | /* |
| 435 | * Nobody gets write access to an immutable file. |
| 436 | */ |
| 437 | if (IS_IMMUTABLE(inode)) |
| 438 | return -EPERM; |
| 439 | |
| 440 | /* |
| 441 | * Updating mtime will likely cause i_uid and i_gid to be |
| 442 | * written back improperly if their true value is unknown |
| 443 | * to the vfs. |
| 444 | */ |
| 445 | if (HAS_UNMAPPED_ID(inode)) |
| 446 | return -EACCES; |
| 447 | } |
| 448 | |
| 449 | retval = do_inode_permission(inode, mask); |
| 450 | if (retval) |
| 451 | return retval; |
| 452 | |
| 453 | retval = devcgroup_inode_permission(inode, mask); |
| 454 | if (retval) |
| 455 | return retval; |
| 456 | |
| 457 | return security_inode_permission(inode, mask); |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 458 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 459 | EXPORT_SYMBOL(inode_permission); |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 460 | |
| 461 | /** |
Jan Blunck | 5dd784d0 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 462 | * path_get - get a reference to a path |
| 463 | * @path: path to get the reference to |
| 464 | * |
| 465 | * Given a path increment the reference count to the dentry and the vfsmount. |
| 466 | */ |
Al Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 467 | void path_get(const struct path *path) |
Jan Blunck | 5dd784d0 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 468 | { |
| 469 | mntget(path->mnt); |
| 470 | dget(path->dentry); |
| 471 | } |
| 472 | EXPORT_SYMBOL(path_get); |
| 473 | |
| 474 | /** |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 475 | * path_put - put a reference to a path |
| 476 | * @path: path to put the reference to |
| 477 | * |
| 478 | * Given a path decrement the reference count to the dentry and the vfsmount. |
| 479 | */ |
Al Viro | dcf787f | 2013-03-01 23:51:07 -0500 | [diff] [blame] | 480 | void path_put(const struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | { |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 482 | dput(path->dentry); |
| 483 | mntput(path->mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 485 | EXPORT_SYMBOL(path_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 487 | #define EMBEDDED_LEVELS 2 |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 488 | struct nameidata { |
| 489 | struct path path; |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 490 | struct qstr last; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 491 | struct path root; |
| 492 | struct inode *inode; /* path.dentry.d_inode */ |
| 493 | unsigned int flags; |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 494 | unsigned seq, m_seq, r_seq; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 495 | int last_type; |
| 496 | unsigned depth; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 497 | int total_link_count; |
Al Viro | 697fc6c | 2015-05-02 19:38:35 -0400 | [diff] [blame] | 498 | struct saved { |
| 499 | struct path link; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 500 | struct delayed_call done; |
Al Viro | 697fc6c | 2015-05-02 19:38:35 -0400 | [diff] [blame] | 501 | const char *name; |
Al Viro | 0450b2d | 2015-05-08 13:23:53 -0400 | [diff] [blame] | 502 | unsigned seq; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 503 | } *stack, internal[EMBEDDED_LEVELS]; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 504 | struct filename *name; |
| 505 | struct nameidata *saved; |
| 506 | unsigned root_seq; |
| 507 | int dfd; |
Kees Cook | 3859a27 | 2016-10-28 01:22:25 -0700 | [diff] [blame] | 508 | } __randomize_layout; |
Al Viro | 1f55a6e | 2014-11-01 19:30:41 -0400 | [diff] [blame] | 509 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 510 | static void set_nameidata(struct nameidata *p, int dfd, struct filename *name) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 511 | { |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 512 | struct nameidata *old = current->nameidata; |
| 513 | p->stack = p->internal; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 514 | p->dfd = dfd; |
| 515 | p->name = name; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 516 | p->total_link_count = old ? old->total_link_count : 0; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 517 | p->saved = old; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 518 | current->nameidata = p; |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 519 | } |
| 520 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 521 | static void restore_nameidata(void) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 522 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 523 | struct nameidata *now = current->nameidata, *old = now->saved; |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 524 | |
| 525 | current->nameidata = old; |
| 526 | if (old) |
| 527 | old->total_link_count = now->total_link_count; |
Al Viro | e1a63bb | 2015-12-05 21:06:33 -0500 | [diff] [blame] | 528 | if (now->stack != now->internal) |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 529 | kfree(now->stack); |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static int __nd_alloc_stack(struct nameidata *nd) |
| 533 | { |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 534 | struct saved *p; |
| 535 | |
| 536 | if (nd->flags & LOOKUP_RCU) { |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 537 | p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 538 | GFP_ATOMIC); |
| 539 | if (unlikely(!p)) |
| 540 | return -ECHILD; |
| 541 | } else { |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 542 | p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 543 | GFP_KERNEL); |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 544 | if (unlikely(!p)) |
| 545 | return -ENOMEM; |
| 546 | } |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 547 | memcpy(p, nd->internal, sizeof(nd->internal)); |
| 548 | nd->stack = p; |
| 549 | return 0; |
| 550 | } |
| 551 | |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 552 | /** |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 553 | * path_connected - Verify that a dentry is below mnt.mnt_root |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 554 | * |
| 555 | * Rename can sometimes move a file or directory outside of a bind |
| 556 | * mount, path_connected allows those cases to be detected. |
| 557 | */ |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 558 | static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 559 | { |
Eric W. Biederman | 95dd775 | 2018-03-14 18:20:29 -0500 | [diff] [blame] | 560 | struct super_block *sb = mnt->mnt_sb; |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 561 | |
Eric W. Biederman | 95dd775 | 2018-03-14 18:20:29 -0500 | [diff] [blame] | 562 | /* Bind mounts and multi-root filesystems can have disconnected paths */ |
| 563 | if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root)) |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 564 | return true; |
| 565 | |
Al Viro | 6b03f7e | 2020-02-24 15:53:19 -0500 | [diff] [blame] | 566 | return is_subdir(dentry, mnt->mnt_root); |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 567 | } |
| 568 | |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 569 | static inline int nd_alloc_stack(struct nameidata *nd) |
| 570 | { |
Al Viro | da4e0be | 2015-05-03 20:52:15 -0400 | [diff] [blame] | 571 | if (likely(nd->depth != EMBEDDED_LEVELS)) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 572 | return 0; |
| 573 | if (likely(nd->stack != nd->internal)) |
| 574 | return 0; |
| 575 | return __nd_alloc_stack(nd); |
| 576 | } |
| 577 | |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 578 | static void drop_links(struct nameidata *nd) |
| 579 | { |
| 580 | int i = nd->depth; |
| 581 | while (i--) { |
| 582 | struct saved *last = nd->stack + i; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 583 | do_delayed_call(&last->done); |
| 584 | clear_delayed_call(&last->done); |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | static void terminate_walk(struct nameidata *nd) |
| 589 | { |
| 590 | drop_links(nd); |
| 591 | if (!(nd->flags & LOOKUP_RCU)) { |
| 592 | int i; |
| 593 | path_put(&nd->path); |
| 594 | for (i = 0; i < nd->depth; i++) |
| 595 | path_put(&nd->stack[i].link); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 596 | if (nd->flags & LOOKUP_ROOT_GRABBED) { |
Al Viro | 102b8af | 2015-05-12 17:35:52 -0400 | [diff] [blame] | 597 | path_put(&nd->root); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 598 | nd->flags &= ~LOOKUP_ROOT_GRABBED; |
Al Viro | 102b8af | 2015-05-12 17:35:52 -0400 | [diff] [blame] | 599 | } |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 600 | } else { |
| 601 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 602 | rcu_read_unlock(); |
| 603 | } |
| 604 | nd->depth = 0; |
| 605 | } |
| 606 | |
| 607 | /* path_put is needed afterwards regardless of success or failure */ |
| 608 | static bool legitimize_path(struct nameidata *nd, |
| 609 | struct path *path, unsigned seq) |
| 610 | { |
| 611 | int res = __legitimize_mnt(path->mnt, nd->m_seq); |
| 612 | if (unlikely(res)) { |
| 613 | if (res > 0) |
| 614 | path->mnt = NULL; |
| 615 | path->dentry = NULL; |
| 616 | return false; |
| 617 | } |
| 618 | if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { |
| 619 | path->dentry = NULL; |
| 620 | return false; |
| 621 | } |
| 622 | return !read_seqcount_retry(&path->dentry->d_seq, seq); |
| 623 | } |
| 624 | |
| 625 | static bool legitimize_links(struct nameidata *nd) |
| 626 | { |
| 627 | int i; |
| 628 | for (i = 0; i < nd->depth; i++) { |
| 629 | struct saved *last = nd->stack + i; |
| 630 | if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { |
| 631 | drop_links(nd); |
| 632 | nd->depth = i + 1; |
| 633 | return false; |
| 634 | } |
| 635 | } |
| 636 | return true; |
| 637 | } |
| 638 | |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 639 | static bool legitimize_root(struct nameidata *nd) |
| 640 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 641 | /* |
| 642 | * For scoped-lookups (where nd->root has been zeroed), we need to |
| 643 | * restart the whole lookup from scratch -- because set_root() is wrong |
| 644 | * for these lookups (nd->dfd is the root, not the filesystem root). |
| 645 | */ |
| 646 | if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED)) |
| 647 | return false; |
| 648 | /* Nothing to do if nd->root is zero or is managed by the VFS user. */ |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 649 | if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT)) |
| 650 | return true; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 651 | nd->flags |= LOOKUP_ROOT_GRABBED; |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 652 | return legitimize_path(nd, &nd->root, nd->root_seq); |
| 653 | } |
| 654 | |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 655 | /* |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 656 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 657 | * Documentation/filesystems/path-lookup.txt). In situations when we can't |
| 658 | * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab |
Mike Marshall | 57e3715 | 2015-11-30 11:11:59 -0500 | [diff] [blame] | 659 | * normal reference counts on dentries and vfsmounts to transition to ref-walk |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 660 | * mode. Refcounts are grabbed at the last known good point before rcu-walk |
| 661 | * got stuck, so ref-walk may continue from there. If this is not successful |
| 662 | * (eg. a seqcount has changed), then failure is returned and it's up to caller |
| 663 | * to restart the path walk from the beginning in ref-walk mode. |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 664 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 665 | |
| 666 | /** |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 667 | * unlazy_walk - try to switch to ref-walk mode. |
| 668 | * @nd: nameidata pathwalk data |
Randy Dunlap | 3919162 | 2011-01-08 19:36:21 -0800 | [diff] [blame] | 669 | * Returns: 0 on success, -ECHILD on failure |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 670 | * |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 671 | * unlazy_walk attempts to legitimize the current nd->path and nd->root |
| 672 | * for ref-walk mode. |
| 673 | * Must be called from rcu-walk context. |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 674 | * Nothing should touch nameidata between unlazy_walk() failure and |
| 675 | * terminate_walk(). |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 676 | */ |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 677 | static int unlazy_walk(struct nameidata *nd) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 678 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 679 | struct dentry *parent = nd->path.dentry; |
| 680 | |
| 681 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 682 | |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 683 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 684 | if (unlikely(!legitimize_links(nd))) |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 685 | goto out1; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 686 | if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) |
| 687 | goto out; |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 688 | if (unlikely(!legitimize_root(nd))) |
| 689 | goto out; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 690 | rcu_read_unlock(); |
| 691 | BUG_ON(nd->inode != parent->d_inode); |
| 692 | return 0; |
| 693 | |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 694 | out1: |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 695 | nd->path.mnt = NULL; |
| 696 | nd->path.dentry = NULL; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 697 | out: |
| 698 | rcu_read_unlock(); |
| 699 | return -ECHILD; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * unlazy_child - try to switch to ref-walk mode. |
| 704 | * @nd: nameidata pathwalk data |
| 705 | * @dentry: child of nd->path.dentry |
| 706 | * @seq: seq number to check dentry against |
| 707 | * Returns: 0 on success, -ECHILD on failure |
| 708 | * |
| 709 | * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry |
| 710 | * for ref-walk mode. @dentry must be a path found by a do_lookup call on |
| 711 | * @nd. Must be called from rcu-walk context. |
| 712 | * Nothing should touch nameidata between unlazy_child() failure and |
| 713 | * terminate_walk(). |
| 714 | */ |
| 715 | static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq) |
| 716 | { |
| 717 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
| 718 | |
| 719 | nd->flags &= ~LOOKUP_RCU; |
| 720 | if (unlikely(!legitimize_links(nd))) |
| 721 | goto out2; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 722 | if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) |
| 723 | goto out2; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 724 | if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 725 | goto out1; |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 726 | |
Linus Torvalds | 1557008 | 2013-09-02 11:38:06 -0700 | [diff] [blame] | 727 | /* |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 728 | * We need to move both the parent and the dentry from the RCU domain |
| 729 | * to be properly refcounted. And the sequence number in the dentry |
| 730 | * validates *both* dentry counters, since we checked the sequence |
| 731 | * number of the parent after we got the child sequence number. So we |
| 732 | * know the parent must still be valid if the child sequence number is |
Linus Torvalds | 1557008 | 2013-09-02 11:38:06 -0700 | [diff] [blame] | 733 | */ |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 734 | if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) |
| 735 | goto out; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 736 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
| 737 | goto out_dput; |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 738 | /* |
| 739 | * Sequence counts matched. Now make sure that the root is |
| 740 | * still valid and get it if required. |
| 741 | */ |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 742 | if (unlikely(!legitimize_root(nd))) |
| 743 | goto out_dput; |
Al Viro | 8b61e74 | 2013-11-08 12:45:01 -0500 | [diff] [blame] | 744 | rcu_read_unlock(); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 745 | return 0; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 746 | |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 747 | out2: |
| 748 | nd->path.mnt = NULL; |
| 749 | out1: |
| 750 | nd->path.dentry = NULL; |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 751 | out: |
Al Viro | 8b61e74 | 2013-11-08 12:45:01 -0500 | [diff] [blame] | 752 | rcu_read_unlock(); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 753 | return -ECHILD; |
| 754 | out_dput: |
| 755 | rcu_read_unlock(); |
| 756 | dput(dentry); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 757 | return -ECHILD; |
| 758 | } |
| 759 | |
Al Viro | 4ce16ef3 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 760 | static inline int d_revalidate(struct dentry *dentry, unsigned int flags) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 761 | { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 762 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) |
| 763 | return dentry->d_op->d_revalidate(dentry, flags); |
| 764 | else |
| 765 | return 1; |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 766 | } |
| 767 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 768 | /** |
| 769 | * complete_walk - successful completion of path walk |
| 770 | * @nd: pointer nameidata |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 771 | * |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 772 | * If we had been in RCU mode, drop out of it and legitimize nd->path. |
| 773 | * Revalidate the final result, unless we'd already done that during |
| 774 | * the path walk or the filesystem doesn't ask for it. Return 0 on |
| 775 | * success, -error on failure. In case of failure caller does not |
| 776 | * need to drop nd->path. |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 777 | */ |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 778 | static int complete_walk(struct nameidata *nd) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 779 | { |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 780 | struct dentry *dentry = nd->path.dentry; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 781 | int status; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 782 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 783 | if (nd->flags & LOOKUP_RCU) { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 784 | /* |
| 785 | * We don't want to zero nd->root for scoped-lookups or |
| 786 | * externally-managed nd->root. |
| 787 | */ |
| 788 | if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED))) |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 789 | nd->root.mnt = NULL; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 790 | if (unlikely(unlazy_walk(nd))) |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 791 | return -ECHILD; |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 792 | } |
| 793 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 794 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
| 795 | /* |
| 796 | * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't |
| 797 | * ever step outside the root during lookup" and should already |
| 798 | * be guaranteed by the rest of namei, we want to avoid a namei |
| 799 | * BUG resulting in userspace being given a path that was not |
| 800 | * scoped within the root at some point during the lookup. |
| 801 | * |
| 802 | * So, do a final sanity-check to make sure that in the |
| 803 | * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) |
| 804 | * we won't silently return an fd completely outside of the |
| 805 | * requested root to userspace. |
| 806 | * |
| 807 | * Userspace could move the path outside the root after this |
| 808 | * check, but as discussed elsewhere this is not a concern (the |
| 809 | * resolved file was inside the root at some point). |
| 810 | */ |
| 811 | if (!path_is_under(&nd->path, &nd->root)) |
| 812 | return -EXDEV; |
| 813 | } |
| 814 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 815 | if (likely(!(nd->flags & LOOKUP_JUMPED))) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 816 | return 0; |
| 817 | |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 818 | if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 819 | return 0; |
| 820 | |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 821 | status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 822 | if (status > 0) |
| 823 | return 0; |
| 824 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 825 | if (!status) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 826 | status = -ESTALE; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 827 | |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 828 | return status; |
| 829 | } |
| 830 | |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 831 | static int set_root(struct nameidata *nd) |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 832 | { |
Al Viro | 7bd8837 | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 833 | struct fs_struct *fs = current->fs; |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 834 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 835 | /* |
| 836 | * Jumping to the real root in a scoped-lookup is a BUG in namei, but we |
| 837 | * still have to ensure it doesn't happen because it will cause a breakout |
| 838 | * from the dirfd. |
| 839 | */ |
| 840 | if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) |
| 841 | return -ENOTRECOVERABLE; |
| 842 | |
Al Viro | 9e6697e | 2015-12-05 20:07:21 -0500 | [diff] [blame] | 843 | if (nd->flags & LOOKUP_RCU) { |
| 844 | unsigned seq; |
| 845 | |
| 846 | do { |
| 847 | seq = read_seqcount_begin(&fs->seq); |
| 848 | nd->root = fs->root; |
| 849 | nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); |
| 850 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 851 | } else { |
| 852 | get_fs_root(fs, &nd->root); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 853 | nd->flags |= LOOKUP_ROOT_GRABBED; |
Al Viro | 9e6697e | 2015-12-05 20:07:21 -0500 | [diff] [blame] | 854 | } |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 855 | return 0; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 856 | } |
| 857 | |
Nick Piggin | 7b9337a | 2011-01-14 08:42:43 +0000 | [diff] [blame] | 858 | static inline void path_to_nameidata(const struct path *path, |
| 859 | struct nameidata *nd) |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 860 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 861 | if (!(nd->flags & LOOKUP_RCU)) { |
| 862 | dput(nd->path.dentry); |
| 863 | if (nd->path.mnt != path->mnt) |
| 864 | mntput(nd->path.mnt); |
Huang Shijie | 9a22968 | 2010-04-02 17:37:13 +0800 | [diff] [blame] | 865 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 866 | nd->path.mnt = path->mnt; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 867 | nd->path.dentry = path->dentry; |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 870 | static int nd_jump_root(struct nameidata *nd) |
| 871 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 872 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 873 | return -EXDEV; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 874 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
| 875 | /* Absolute path arguments to path_init() are allowed. */ |
| 876 | if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) |
| 877 | return -EXDEV; |
| 878 | } |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 879 | if (!nd->root.mnt) { |
| 880 | int error = set_root(nd); |
| 881 | if (error) |
| 882 | return error; |
| 883 | } |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 884 | if (nd->flags & LOOKUP_RCU) { |
| 885 | struct dentry *d; |
| 886 | nd->path = nd->root; |
| 887 | d = nd->path.dentry; |
| 888 | nd->inode = d->d_inode; |
| 889 | nd->seq = nd->root_seq; |
| 890 | if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq))) |
| 891 | return -ECHILD; |
| 892 | } else { |
| 893 | path_put(&nd->path); |
| 894 | nd->path = nd->root; |
| 895 | path_get(&nd->path); |
| 896 | nd->inode = nd->path.dentry->d_inode; |
| 897 | } |
| 898 | nd->flags |= LOOKUP_JUMPED; |
| 899 | return 0; |
| 900 | } |
| 901 | |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 902 | /* |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 903 | * Helper to directly jump to a known parsed path from ->get_link, |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 904 | * caller must have taken a reference to path beforehand. |
| 905 | */ |
Aleksa Sarai | 1bc8207 | 2019-12-07 01:13:28 +1100 | [diff] [blame] | 906 | int nd_jump_link(struct path *path) |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 907 | { |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 908 | int error = -ELOOP; |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 909 | struct nameidata *nd = current->nameidata; |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 910 | |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 911 | if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) |
| 912 | goto err; |
| 913 | |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 914 | error = -EXDEV; |
| 915 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
| 916 | if (nd->path.mnt != path->mnt) |
| 917 | goto err; |
| 918 | } |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 919 | /* Not currently safe for scoped-lookups. */ |
| 920 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) |
| 921 | goto err; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 922 | |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 923 | path_put(&nd->path); |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 924 | nd->path = *path; |
| 925 | nd->inode = nd->path.dentry->d_inode; |
| 926 | nd->flags |= LOOKUP_JUMPED; |
Aleksa Sarai | 1bc8207 | 2019-12-07 01:13:28 +1100 | [diff] [blame] | 927 | return 0; |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 928 | |
| 929 | err: |
| 930 | path_put(path); |
| 931 | return error; |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 932 | } |
| 933 | |
Al Viro | b9ff442 | 2015-05-02 20:19:23 -0400 | [diff] [blame] | 934 | static inline void put_link(struct nameidata *nd) |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 935 | { |
Al Viro | 21c3003 | 2015-05-03 21:06:24 -0400 | [diff] [blame] | 936 | struct saved *last = nd->stack + --nd->depth; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 937 | do_delayed_call(&last->done); |
Al Viro | 6548fae | 2015-05-07 20:32:22 -0400 | [diff] [blame] | 938 | if (!(nd->flags & LOOKUP_RCU)) |
| 939 | path_put(&last->link); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 940 | } |
| 941 | |
Linus Torvalds | 561ec64 | 2012-10-26 10:05:07 -0700 | [diff] [blame] | 942 | int sysctl_protected_symlinks __read_mostly = 0; |
| 943 | int sysctl_protected_hardlinks __read_mostly = 0; |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 944 | int sysctl_protected_fifos __read_mostly; |
| 945 | int sysctl_protected_regular __read_mostly; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 946 | |
| 947 | /** |
| 948 | * may_follow_link - Check symlink following for unsafe situations |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 949 | * @nd: nameidata pathwalk data |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 950 | * |
| 951 | * In the case of the sysctl_protected_symlinks sysctl being enabled, |
| 952 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is |
| 953 | * in a sticky world-writable directory. This is to protect privileged |
| 954 | * processes from failing races against path names that may change out |
| 955 | * from under them by way of other users creating malicious symlinks. |
| 956 | * It will permit symlinks to be followed only when outside a sticky |
| 957 | * world-writable directory, or when the uid of the symlink and follower |
| 958 | * match, or when the directory owner matches the symlink's owner. |
| 959 | * |
| 960 | * Returns 0 if following the symlink is allowed, -ve on error. |
| 961 | */ |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 962 | static inline int may_follow_link(struct nameidata *nd, const struct inode *inode) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 963 | { |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 964 | const struct inode *parent; |
Seth Forshee | 2d7f9e2 | 2016-04-26 14:36:23 -0500 | [diff] [blame] | 965 | kuid_t puid; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 966 | |
| 967 | if (!sysctl_protected_symlinks) |
| 968 | return 0; |
| 969 | |
| 970 | /* Allowed if owner and follower match. */ |
Eric W. Biederman | 81abe27 | 2012-08-03 09:38:08 -0700 | [diff] [blame] | 971 | if (uid_eq(current_cred()->fsuid, inode->i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 972 | return 0; |
| 973 | |
| 974 | /* Allowed if parent directory not sticky and world-writable. */ |
Al Viro | aa65fa3 | 2015-08-04 23:23:50 -0400 | [diff] [blame] | 975 | parent = nd->inode; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 976 | if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
| 977 | return 0; |
| 978 | |
| 979 | /* Allowed if parent directory and link owner match. */ |
Seth Forshee | 2d7f9e2 | 2016-04-26 14:36:23 -0500 | [diff] [blame] | 980 | puid = parent->i_uid; |
| 981 | if (uid_valid(puid) && uid_eq(puid, inode->i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 982 | return 0; |
| 983 | |
Al Viro | 31956502 | 2015-05-07 20:37:40 -0400 | [diff] [blame] | 984 | if (nd->flags & LOOKUP_RCU) |
| 985 | return -ECHILD; |
| 986 | |
Richard Guy Briggs | ea841ba | 2018-03-21 04:42:21 -0400 | [diff] [blame] | 987 | audit_inode(nd->name, nd->stack[0].link.dentry, 0); |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 988 | audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 989 | return -EACCES; |
| 990 | } |
| 991 | |
| 992 | /** |
| 993 | * safe_hardlink_source - Check for safe hardlink conditions |
| 994 | * @inode: the source inode to hardlink from |
| 995 | * |
| 996 | * Return false if at least one of the following conditions: |
| 997 | * - inode is not a regular file |
| 998 | * - inode is setuid |
| 999 | * - inode is setgid and group-exec |
| 1000 | * - access failure for read and write |
| 1001 | * |
| 1002 | * Otherwise returns true. |
| 1003 | */ |
| 1004 | static bool safe_hardlink_source(struct inode *inode) |
| 1005 | { |
| 1006 | umode_t mode = inode->i_mode; |
| 1007 | |
| 1008 | /* Special files should not get pinned to the filesystem. */ |
| 1009 | if (!S_ISREG(mode)) |
| 1010 | return false; |
| 1011 | |
| 1012 | /* Setuid files should not get pinned to the filesystem. */ |
| 1013 | if (mode & S_ISUID) |
| 1014 | return false; |
| 1015 | |
| 1016 | /* Executable setgid files should not get pinned to the filesystem. */ |
| 1017 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) |
| 1018 | return false; |
| 1019 | |
| 1020 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ |
| 1021 | if (inode_permission(inode, MAY_READ | MAY_WRITE)) |
| 1022 | return false; |
| 1023 | |
| 1024 | return true; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * may_linkat - Check permissions for creating a hardlink |
| 1029 | * @link: the source to hardlink from |
| 1030 | * |
| 1031 | * Block hardlink when all of: |
| 1032 | * - sysctl_protected_hardlinks enabled |
| 1033 | * - fsuid does not match inode |
| 1034 | * - hardlink source is unsafe (see safe_hardlink_source() above) |
Dirk Steinmetz | f2ca379 | 2015-10-20 16:09:19 +0200 | [diff] [blame] | 1035 | * - not CAP_FOWNER in a namespace with the inode owner uid mapped |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1036 | * |
| 1037 | * Returns 0 if successful, -ve on error. |
| 1038 | */ |
| 1039 | static int may_linkat(struct path *link) |
| 1040 | { |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 1041 | struct inode *inode = link->dentry->d_inode; |
| 1042 | |
| 1043 | /* Inode writeback is not safe when the uid or gid are invalid. */ |
| 1044 | if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid)) |
| 1045 | return -EOVERFLOW; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1046 | |
| 1047 | if (!sysctl_protected_hardlinks) |
| 1048 | return 0; |
| 1049 | |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1050 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, |
| 1051 | * otherwise, it must be a safe source. |
| 1052 | */ |
Kees Cook | cc658db | 2017-06-21 09:53:06 -0700 | [diff] [blame] | 1053 | if (safe_hardlink_source(inode) || inode_owner_or_capable(inode)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1054 | return 0; |
| 1055 | |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1056 | audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1057 | return -EPERM; |
| 1058 | } |
| 1059 | |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1060 | /** |
| 1061 | * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory |
| 1062 | * should be allowed, or not, on files that already |
| 1063 | * exist. |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1064 | * @dir_mode: mode bits of directory |
| 1065 | * @dir_uid: owner of directory |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1066 | * @inode: the inode of the file to open |
| 1067 | * |
| 1068 | * Block an O_CREAT open of a FIFO (or a regular file) when: |
| 1069 | * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled |
| 1070 | * - the file already exists |
| 1071 | * - we are in a sticky directory |
| 1072 | * - we don't own the file |
| 1073 | * - the owner of the directory doesn't own the file |
| 1074 | * - the directory is world writable |
| 1075 | * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 |
| 1076 | * the directory doesn't have to be world writable: being group writable will |
| 1077 | * be enough. |
| 1078 | * |
| 1079 | * Returns 0 if the open is allowed, -ve on error. |
| 1080 | */ |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1081 | static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid, |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1082 | struct inode * const inode) |
| 1083 | { |
| 1084 | if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || |
| 1085 | (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1086 | likely(!(dir_mode & S_ISVTX)) || |
| 1087 | uid_eq(inode->i_uid, dir_uid) || |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1088 | uid_eq(current_fsuid(), inode->i_uid)) |
| 1089 | return 0; |
| 1090 | |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1091 | if (likely(dir_mode & 0002) || |
| 1092 | (dir_mode & 0020 && |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1093 | ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || |
| 1094 | (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1095 | const char *operation = S_ISFIFO(inode->i_mode) ? |
| 1096 | "sticky_create_fifo" : |
| 1097 | "sticky_create_regular"; |
| 1098 | audit_log_path_denied(AUDIT_ANOM_CREAT, operation); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1099 | return -EACCES; |
| 1100 | } |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
David Howells | f015f126 | 2012-06-25 12:55:28 +0100 | [diff] [blame] | 1104 | /* |
| 1105 | * follow_up - Find the mountpoint of path's vfsmount |
| 1106 | * |
| 1107 | * Given a path, find the mountpoint of its source file system. |
| 1108 | * Replace @path with the path of the mountpoint in the parent mount. |
| 1109 | * Up is towards /. |
| 1110 | * |
| 1111 | * Return 1 if we went up a level and 0 if we were already at the |
| 1112 | * root. |
| 1113 | */ |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 1114 | int follow_up(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1116 | struct mount *mnt = real_mount(path->mnt); |
| 1117 | struct mount *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | struct dentry *mountpoint; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1119 | |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1120 | read_seqlock_excl(&mount_lock); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1121 | parent = mnt->mnt_parent; |
Al Viro | 3c0a616 | 2012-07-18 17:32:50 +0400 | [diff] [blame] | 1122 | if (parent == mnt) { |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1123 | read_sequnlock_excl(&mount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | return 0; |
| 1125 | } |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1126 | mntget(&parent->mnt); |
Al Viro | a73324d | 2011-11-24 22:25:07 -0500 | [diff] [blame] | 1127 | mountpoint = dget(mnt->mnt_mountpoint); |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1128 | read_sequnlock_excl(&mount_lock); |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 1129 | dput(path->dentry); |
| 1130 | path->dentry = mountpoint; |
| 1131 | mntput(path->mnt); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1132 | path->mnt = &parent->mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | return 1; |
| 1134 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 1135 | EXPORT_SYMBOL(follow_up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 1137 | /* |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1138 | * Perform an automount |
| 1139 | * - return -EISDIR to tell follow_managed() to stop and return the path we |
| 1140 | * were called with. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | */ |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1142 | static int follow_automount(struct path *path, int *count, unsigned lookup_flags) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1143 | { |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1144 | struct dentry *dentry = path->dentry; |
Al Viro | 463ffb2 | 2005-06-06 13:36:05 -0700 | [diff] [blame] | 1145 | |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 1146 | /* We don't want to mount if someone's just doing a stat - |
| 1147 | * unless they're stat'ing a directory and appended a '/' to |
| 1148 | * the name. |
| 1149 | * |
| 1150 | * We do, however, want to mount if someone wants to open or |
| 1151 | * create a file of any type under the mountpoint, wants to |
| 1152 | * traverse through the mountpoint or wants to open the |
| 1153 | * mounted directory. Also, autofs may mark negative dentries |
| 1154 | * as being automount points. These will need the attentions |
| 1155 | * of the daemon to instantiate them before they can be used. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1156 | */ |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1157 | if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
Ian Kent | 5d38f04 | 2017-11-29 16:11:26 -0800 | [diff] [blame] | 1158 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1159 | dentry->d_inode) |
Ian Kent | 5d38f04 | 2017-11-29 16:11:26 -0800 | [diff] [blame] | 1160 | return -EISDIR; |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 1161 | |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1162 | if (count && (*count)++ >= MAXSYMLINKS) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1163 | return -ELOOP; |
| 1164 | |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1165 | return finish_automount(dentry->d_op->d_automount(path), path); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | /* |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1169 | * mount traversal - out-of-line part. One note on ->d_flags accesses - |
| 1170 | * dentries are pinned but not locked here, so negative dentry can go |
| 1171 | * positive right under us. Use of smp_load_acquire() provides a barrier |
| 1172 | * sufficient for ->d_inode and ->d_flags consistency. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1173 | */ |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1174 | static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, |
| 1175 | int *count, unsigned lookup_flags) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1176 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1177 | struct vfsmount *mnt = path->mnt; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1178 | bool need_mntput = false; |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1179 | int ret = 0; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1180 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1181 | while (flags & DCACHE_MANAGED_DENTRY) { |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1182 | /* Allow the filesystem to manage the transit without i_mutex |
| 1183 | * being held. */ |
Al Viro | d41efb5 | 2019-11-04 22:30:52 -0500 | [diff] [blame] | 1184 | if (flags & DCACHE_MANAGE_TRANSIT) { |
Ian Kent | fb5f51c | 2016-11-24 08:03:41 +1100 | [diff] [blame] | 1185 | ret = path->dentry->d_op->d_manage(path, false); |
Al Viro | 508c877 | 2020-01-14 22:09:57 -0500 | [diff] [blame] | 1186 | flags = smp_load_acquire(&path->dentry->d_flags); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1187 | if (ret < 0) |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1188 | break; |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1191 | if (flags & DCACHE_MOUNTED) { // something's mounted on it.. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1192 | struct vfsmount *mounted = lookup_mnt(path); |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1193 | if (mounted) { // ... in our namespace |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1194 | dput(path->dentry); |
| 1195 | if (need_mntput) |
| 1196 | mntput(path->mnt); |
| 1197 | path->mnt = mounted; |
| 1198 | path->dentry = dget(mounted->mnt_root); |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1199 | // here we know it's positive |
| 1200 | flags = path->dentry->d_flags; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1201 | need_mntput = true; |
| 1202 | continue; |
| 1203 | } |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1206 | if (!(flags & DCACHE_NEED_AUTOMOUNT)) |
| 1207 | break; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1208 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1209 | // uncovered automount point |
| 1210 | ret = follow_automount(path, count, lookup_flags); |
| 1211 | flags = smp_load_acquire(&path->dentry->d_flags); |
| 1212 | if (ret < 0) |
| 1213 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1215 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1216 | if (ret == -EISDIR) |
| 1217 | ret = 0; |
| 1218 | // possible if you race with several mount --move |
| 1219 | if (need_mntput && path->mnt == mnt) |
| 1220 | mntput(path->mnt); |
| 1221 | if (!ret && unlikely(d_flags_negative(flags))) |
Al Viro | d41efb5 | 2019-11-04 22:30:52 -0500 | [diff] [blame] | 1222 | ret = -ENOENT; |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1223 | *jumped = need_mntput; |
Al Viro | 8402752 | 2015-04-22 10:30:08 -0400 | [diff] [blame] | 1224 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1227 | static inline int traverse_mounts(struct path *path, bool *jumped, |
| 1228 | int *count, unsigned lookup_flags) |
| 1229 | { |
| 1230 | unsigned flags = smp_load_acquire(&path->dentry->d_flags); |
| 1231 | |
| 1232 | /* fastpath */ |
| 1233 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { |
| 1234 | *jumped = false; |
| 1235 | if (unlikely(d_flags_negative(flags))) |
| 1236 | return -ENOENT; |
| 1237 | return 0; |
| 1238 | } |
| 1239 | return __traverse_mounts(path, flags, jumped, count, lookup_flags); |
| 1240 | } |
| 1241 | |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1242 | int follow_down_one(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | { |
| 1244 | struct vfsmount *mounted; |
| 1245 | |
Al Viro | 1c755af | 2009-04-18 14:06:57 -0400 | [diff] [blame] | 1246 | mounted = lookup_mnt(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | if (mounted) { |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 1248 | dput(path->dentry); |
| 1249 | mntput(path->mnt); |
| 1250 | path->mnt = mounted; |
| 1251 | path->dentry = dget(mounted->mnt_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | return 1; |
| 1253 | } |
| 1254 | return 0; |
| 1255 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 1256 | EXPORT_SYMBOL(follow_down_one); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1258 | /* |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1259 | * Follow down to the covering mount currently visible to userspace. At each |
| 1260 | * point, the filesystem owning that dentry may be queried as to whether the |
| 1261 | * caller is permitted to proceed or not. |
| 1262 | */ |
| 1263 | int follow_down(struct path *path) |
| 1264 | { |
| 1265 | struct vfsmount *mnt = path->mnt; |
| 1266 | bool jumped; |
| 1267 | int ret = traverse_mounts(path, &jumped, NULL, 0); |
| 1268 | |
| 1269 | if (path->mnt != mnt) |
| 1270 | mntput(mnt); |
| 1271 | return ret; |
| 1272 | } |
| 1273 | EXPORT_SYMBOL(follow_down); |
| 1274 | |
| 1275 | /* |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1276 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
| 1277 | * we meet a managed dentry that would need blocking. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1278 | */ |
| 1279 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1280 | struct inode **inode, unsigned *seqp) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1281 | { |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1282 | struct dentry *dentry = path->dentry; |
| 1283 | unsigned int flags = dentry->d_flags; |
| 1284 | |
| 1285 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) |
| 1286 | return true; |
| 1287 | |
| 1288 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1289 | return false; |
| 1290 | |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1291 | for (;;) { |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1292 | /* |
| 1293 | * Don't forget we might have a non-mountpoint managed dentry |
| 1294 | * that wants to block transit. |
| 1295 | */ |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1296 | if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { |
| 1297 | int res = dentry->d_op->d_manage(path, true); |
| 1298 | if (res) |
| 1299 | return res == -EISDIR; |
| 1300 | flags = dentry->d_flags; |
NeilBrown | b8faf03 | 2014-08-04 17:06:29 +1000 | [diff] [blame] | 1301 | } |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1302 | |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1303 | if (flags & DCACHE_MOUNTED) { |
| 1304 | struct mount *mounted = __lookup_mnt(path->mnt, dentry); |
| 1305 | if (mounted) { |
| 1306 | path->mnt = &mounted->mnt; |
| 1307 | dentry = path->dentry = mounted->mnt.mnt_root; |
| 1308 | nd->flags |= LOOKUP_JUMPED; |
| 1309 | *seqp = read_seqcount_begin(&dentry->d_seq); |
| 1310 | *inode = dentry->d_inode; |
| 1311 | /* |
| 1312 | * We don't need to re-check ->d_seq after this |
| 1313 | * ->d_inode read - there will be an RCU delay |
| 1314 | * between mount hash removal and ->mnt_root |
| 1315 | * becoming unpinned. |
| 1316 | */ |
| 1317 | flags = dentry->d_flags; |
| 1318 | continue; |
| 1319 | } |
| 1320 | if (read_seqretry(&mount_lock, nd->m_seq)) |
| 1321 | return false; |
| 1322 | } |
| 1323 | return !(flags & DCACHE_NEED_AUTOMOUNT); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1324 | } |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1325 | } |
| 1326 | |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1327 | static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, |
| 1328 | struct path *path, struct inode **inode, |
| 1329 | unsigned int *seqp) |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1330 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1331 | bool jumped; |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1332 | int ret; |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1333 | |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1334 | path->mnt = nd->path.mnt; |
| 1335 | path->dentry = dentry; |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1336 | if (nd->flags & LOOKUP_RCU) { |
| 1337 | unsigned int seq = *seqp; |
| 1338 | if (unlikely(!*inode)) |
| 1339 | return -ENOENT; |
| 1340 | if (likely(__follow_mount_rcu(nd, path, inode, seqp))) |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1341 | return 0; |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1342 | if (unlazy_child(nd, dentry, seq)) |
| 1343 | return -ECHILD; |
| 1344 | // *path might've been clobbered by __follow_mount_rcu() |
| 1345 | path->mnt = nd->path.mnt; |
| 1346 | path->dentry = dentry; |
| 1347 | } |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1348 | ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); |
| 1349 | if (jumped) { |
| 1350 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1351 | ret = -EXDEV; |
| 1352 | else |
| 1353 | nd->flags |= LOOKUP_JUMPED; |
| 1354 | } |
| 1355 | if (unlikely(ret)) { |
| 1356 | dput(path->dentry); |
| 1357 | if (path->mnt != nd->path.mnt) |
| 1358 | mntput(path->mnt); |
| 1359 | } else { |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1360 | *inode = d_backing_inode(path->dentry); |
| 1361 | *seqp = 0; /* out of RCU mode, so the value doesn't matter */ |
| 1362 | } |
| 1363 | return ret; |
| 1364 | } |
| 1365 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1366 | /* |
| 1367 | * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() |
| 1368 | */ |
| 1369 | static void follow_mount(struct path *path) |
| 1370 | { |
| 1371 | while (d_mountpoint(path->dentry)) { |
| 1372 | struct vfsmount *mounted = lookup_mnt(path); |
| 1373 | if (!mounted) |
| 1374 | break; |
| 1375 | dput(path->dentry); |
| 1376 | mntput(path->mnt); |
| 1377 | path->mnt = mounted; |
| 1378 | path->dentry = dget(mounted->mnt_root); |
| 1379 | } |
| 1380 | } |
| 1381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | /* |
Oleg Drokin | f4fdace | 2016-07-07 22:04:04 -0400 | [diff] [blame] | 1383 | * This looks up the name in dcache and possibly revalidates the found dentry. |
| 1384 | * NULL is returned if the dentry does not exist in the cache. |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1385 | */ |
Al Viro | e3c1392 | 2016-03-06 14:03:27 -0500 | [diff] [blame] | 1386 | static struct dentry *lookup_dcache(const struct qstr *name, |
| 1387 | struct dentry *dir, |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 1388 | unsigned int flags) |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1389 | { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1390 | struct dentry *dentry = d_lookup(dir, name); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1391 | if (dentry) { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1392 | int error = d_revalidate(dentry, flags); |
| 1393 | if (unlikely(error <= 0)) { |
| 1394 | if (!error) |
| 1395 | d_invalidate(dentry); |
| 1396 | dput(dentry); |
| 1397 | return ERR_PTR(error); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1398 | } |
| 1399 | } |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1400 | return dentry; |
| 1401 | } |
| 1402 | |
| 1403 | /* |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1404 | * Parent directory has inode locked exclusive. This is one |
| 1405 | * and only case when ->lookup() gets called on non in-lookup |
| 1406 | * dentries - as the matter of fact, this only gets called |
| 1407 | * when directory is guaranteed to have no in-lookup children |
| 1408 | * at all. |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1409 | */ |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1410 | static struct dentry *__lookup_hash(const struct qstr *name, |
| 1411 | struct dentry *base, unsigned int flags) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1412 | { |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1413 | struct dentry *dentry = lookup_dcache(name, base, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1414 | struct dentry *old; |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1415 | struct inode *dir = base->d_inode; |
| 1416 | |
| 1417 | if (dentry) |
| 1418 | return dentry; |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1419 | |
| 1420 | /* Don't create child dentry for a dead directory. */ |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1421 | if (unlikely(IS_DEADDIR(dir))) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1422 | return ERR_PTR(-ENOENT); |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1423 | |
| 1424 | dentry = d_alloc(base, name); |
| 1425 | if (unlikely(!dentry)) |
| 1426 | return ERR_PTR(-ENOMEM); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1427 | |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1428 | old = dir->i_op->lookup(dir, dentry, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1429 | if (unlikely(old)) { |
| 1430 | dput(dentry); |
| 1431 | dentry = old; |
| 1432 | } |
| 1433 | return dentry; |
| 1434 | } |
| 1435 | |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1436 | static struct dentry *lookup_fast(struct nameidata *nd, |
| 1437 | struct inode **inode, |
| 1438 | unsigned *seqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1440 | struct dentry *dentry, *parent = nd->path.dentry; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1441 | int status = 1; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1442 | |
Al Viro | 3cac260 | 2009-08-13 18:27:43 +0400 | [diff] [blame] | 1443 | /* |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1444 | * Rename seqlock is not required here because in the off chance |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1445 | * of a false negative due to a concurrent rename, the caller is |
| 1446 | * going to fall back to non-racy lookup. |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1447 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1448 | if (nd->flags & LOOKUP_RCU) { |
| 1449 | unsigned seq; |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 1450 | dentry = __d_lookup_rcu(parent, &nd->last, &seq); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1451 | if (unlikely(!dentry)) { |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 1452 | if (unlazy_walk(nd)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1453 | return ERR_PTR(-ECHILD); |
| 1454 | return NULL; |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1455 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1456 | |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1457 | /* |
| 1458 | * This sequence count validates that the inode matches |
| 1459 | * the dentry name information from lookup. |
| 1460 | */ |
David Howells | 63afdfc | 2015-05-06 15:59:00 +0100 | [diff] [blame] | 1461 | *inode = d_backing_inode(dentry); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1462 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1463 | return ERR_PTR(-ECHILD); |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1464 | |
| 1465 | /* |
| 1466 | * This sequence count validates that the parent had no |
| 1467 | * changes while we did the lookup of the dentry above. |
| 1468 | * |
| 1469 | * The memory barrier in read_seqcount_begin of child is |
| 1470 | * enough, we can use __read_seqcount_retry here. |
| 1471 | */ |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1472 | if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq))) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1473 | return ERR_PTR(-ECHILD); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1474 | |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1475 | *seqp = seq; |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1476 | status = d_revalidate(dentry, nd->flags); |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1477 | if (likely(status > 0)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1478 | return dentry; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 1479 | if (unlazy_child(nd, dentry, seq)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1480 | return ERR_PTR(-ECHILD); |
Al Viro | 209a7fb | 2017-01-09 01:35:39 -0500 | [diff] [blame] | 1481 | if (unlikely(status == -ECHILD)) |
| 1482 | /* we'd been told to redo it in non-rcu mode */ |
| 1483 | status = d_revalidate(dentry, nd->flags); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1484 | } else { |
Al Viro | e97cdc8 | 2013-01-24 18:16:00 -0500 | [diff] [blame] | 1485 | dentry = __d_lookup(parent, &nd->last); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1486 | if (unlikely(!dentry)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1487 | return NULL; |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1488 | status = d_revalidate(dentry, nd->flags); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1489 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1490 | if (unlikely(status <= 0)) { |
Al Viro | e9742b5 | 2016-03-05 22:04:59 -0500 | [diff] [blame] | 1491 | if (!status) |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1492 | d_invalidate(dentry); |
Eric W. Biederman | 5542aa2 | 2014-02-13 09:46:25 -0800 | [diff] [blame] | 1493 | dput(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1494 | return ERR_PTR(status); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1495 | } |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1496 | return dentry; |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | /* Fast lookup failed, do it the slow way */ |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1500 | static struct dentry *__lookup_slow(const struct qstr *name, |
| 1501 | struct dentry *dir, |
| 1502 | unsigned int flags) |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1503 | { |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1504 | struct dentry *dentry, *old; |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1505 | struct inode *inode = dir->d_inode; |
Al Viro | d9171b9 | 2016-04-15 03:33:13 -0400 | [diff] [blame] | 1506 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1507 | |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1508 | /* Don't go there if it's already dead */ |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1509 | if (unlikely(IS_DEADDIR(inode))) |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1510 | return ERR_PTR(-ENOENT); |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1511 | again: |
Al Viro | d9171b9 | 2016-04-15 03:33:13 -0400 | [diff] [blame] | 1512 | dentry = d_alloc_parallel(dir, name, &wq); |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1513 | if (IS_ERR(dentry)) |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1514 | return dentry; |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1515 | if (unlikely(!d_in_lookup(dentry))) { |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1516 | int error = d_revalidate(dentry, flags); |
| 1517 | if (unlikely(error <= 0)) { |
| 1518 | if (!error) { |
| 1519 | d_invalidate(dentry); |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1520 | dput(dentry); |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1521 | goto again; |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1522 | } |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1523 | dput(dentry); |
| 1524 | dentry = ERR_PTR(error); |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1525 | } |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1526 | } else { |
| 1527 | old = inode->i_op->lookup(inode, dentry, flags); |
| 1528 | d_lookup_done(dentry); |
| 1529 | if (unlikely(old)) { |
| 1530 | dput(dentry); |
| 1531 | dentry = old; |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1532 | } |
| 1533 | } |
Al Viro | e3c1392 | 2016-03-06 14:03:27 -0500 | [diff] [blame] | 1534 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1537 | static struct dentry *lookup_slow(const struct qstr *name, |
| 1538 | struct dentry *dir, |
| 1539 | unsigned int flags) |
| 1540 | { |
| 1541 | struct inode *inode = dir->d_inode; |
| 1542 | struct dentry *res; |
| 1543 | inode_lock_shared(inode); |
| 1544 | res = __lookup_slow(name, dir, flags); |
| 1545 | inode_unlock_shared(inode); |
| 1546 | return res; |
| 1547 | } |
| 1548 | |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1549 | static inline int may_lookup(struct nameidata *nd) |
| 1550 | { |
| 1551 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | 4ad5abb | 2011-06-20 19:57:03 -0400 | [diff] [blame] | 1552 | int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1553 | if (err != -ECHILD) |
| 1554 | return err; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 1555 | if (unlazy_walk(nd)) |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1556 | return -ECHILD; |
| 1557 | } |
Al Viro | 4ad5abb | 2011-06-20 19:57:03 -0400 | [diff] [blame] | 1558 | return inode_permission(nd->inode, MAY_EXEC); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1559 | } |
| 1560 | |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1561 | enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; |
| 1562 | |
Al Viro | 06708ad | 2020-01-14 14:26:57 -0500 | [diff] [blame] | 1563 | static const char *pick_link(struct nameidata *nd, struct path *link, |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1564 | struct inode *inode, unsigned seq, int flags) |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1565 | { |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 1566 | struct saved *last; |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1567 | const char *res; |
| 1568 | int error; |
| 1569 | |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 1570 | if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) { |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1571 | path_to_nameidata(link, nd); |
Al Viro | 06708ad | 2020-01-14 14:26:57 -0500 | [diff] [blame] | 1572 | return ERR_PTR(-ELOOP); |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1573 | } |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 1574 | if (!(nd->flags & LOOKUP_RCU)) { |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 1575 | if (link->mnt == nd->path.mnt) |
| 1576 | mntget(link->mnt); |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1577 | } |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1578 | error = nd_alloc_stack(nd); |
| 1579 | if (unlikely(error)) { |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 1580 | if (error == -ECHILD) { |
Al Viro | ad1633a | 2017-01-08 22:35:31 -0500 | [diff] [blame] | 1581 | if (unlikely(!legitimize_path(nd, link, seq))) { |
| 1582 | drop_links(nd); |
| 1583 | nd->depth = 0; |
| 1584 | nd->flags &= ~LOOKUP_RCU; |
| 1585 | nd->path.mnt = NULL; |
| 1586 | nd->path.dentry = NULL; |
Al Viro | ad1633a | 2017-01-08 22:35:31 -0500 | [diff] [blame] | 1587 | rcu_read_unlock(); |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 1588 | } else if (likely(unlazy_walk(nd)) == 0) |
Al Viro | ad1633a | 2017-01-08 22:35:31 -0500 | [diff] [blame] | 1589 | error = nd_alloc_stack(nd); |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 1590 | } |
| 1591 | if (error) { |
| 1592 | path_put(link); |
Al Viro | 06708ad | 2020-01-14 14:26:57 -0500 | [diff] [blame] | 1593 | return ERR_PTR(error); |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 1594 | } |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1595 | } |
| 1596 | |
Al Viro | ab10492 | 2015-05-10 11:50:01 -0400 | [diff] [blame] | 1597 | last = nd->stack + nd->depth++; |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 1598 | last->link = *link; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 1599 | clear_delayed_call(&last->done); |
Al Viro | 0450b2d | 2015-05-08 13:23:53 -0400 | [diff] [blame] | 1600 | last->seq = seq; |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1601 | |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1602 | if (flags & WALK_TRAILING) { |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1603 | error = may_follow_link(nd, inode); |
| 1604 | if (unlikely(error)) |
| 1605 | return ERR_PTR(error); |
| 1606 | } |
| 1607 | |
| 1608 | if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS)) |
| 1609 | return ERR_PTR(-ELOOP); |
| 1610 | |
| 1611 | if (!(nd->flags & LOOKUP_RCU)) { |
| 1612 | touch_atime(&last->link); |
| 1613 | cond_resched(); |
| 1614 | } else if (atime_needs_update(&last->link, inode)) { |
| 1615 | if (unlikely(unlazy_walk(nd))) |
| 1616 | return ERR_PTR(-ECHILD); |
| 1617 | touch_atime(&last->link); |
| 1618 | } |
| 1619 | |
| 1620 | error = security_inode_follow_link(link->dentry, inode, |
| 1621 | nd->flags & LOOKUP_RCU); |
| 1622 | if (unlikely(error)) |
| 1623 | return ERR_PTR(error); |
| 1624 | |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1625 | res = READ_ONCE(inode->i_link); |
| 1626 | if (!res) { |
| 1627 | const char * (*get)(struct dentry *, struct inode *, |
| 1628 | struct delayed_call *); |
| 1629 | get = inode->i_op->get_link; |
| 1630 | if (nd->flags & LOOKUP_RCU) { |
| 1631 | res = get(NULL, inode, &last->done); |
| 1632 | if (res == ERR_PTR(-ECHILD)) { |
| 1633 | if (unlikely(unlazy_walk(nd))) |
| 1634 | return ERR_PTR(-ECHILD); |
| 1635 | res = get(link->dentry, inode, &last->done); |
| 1636 | } |
| 1637 | } else { |
| 1638 | res = get(link->dentry, inode, &last->done); |
| 1639 | } |
| 1640 | if (!res) |
| 1641 | goto all_done; |
| 1642 | if (IS_ERR(res)) |
| 1643 | return res; |
| 1644 | } |
| 1645 | if (*res == '/') { |
| 1646 | error = nd_jump_root(nd); |
| 1647 | if (unlikely(error)) |
| 1648 | return ERR_PTR(error); |
| 1649 | while (unlikely(*++res == '/')) |
| 1650 | ; |
| 1651 | } |
| 1652 | if (*res) |
| 1653 | return res; |
| 1654 | all_done: // pure jump |
| 1655 | put_link(nd); |
| 1656 | return NULL; |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1657 | } |
| 1658 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1659 | /* |
| 1660 | * Do we need to follow links? We _really_ want to be able |
| 1661 | * to do this check without having to look at inode->i_op, |
| 1662 | * so we keep a cache of "no, this doesn't need follow_link" |
| 1663 | * for the common case. |
| 1664 | */ |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1665 | static const char *step_into(struct nameidata *nd, int flags, |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1666 | struct dentry *dentry, struct inode *inode, unsigned seq) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1667 | { |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1668 | struct path path; |
| 1669 | int err = handle_mounts(nd, dentry, &path, &inode, &seq); |
| 1670 | |
| 1671 | if (err < 0) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1672 | return ERR_PTR(err); |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1673 | if (likely(!d_is_symlink(path.dentry)) || |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 1674 | ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || |
Al Viro | aca2903 | 2020-01-09 15:17:57 -0500 | [diff] [blame] | 1675 | (flags & WALK_NOFOLLOW)) { |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1676 | /* not a symlink or should not follow */ |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1677 | path_to_nameidata(&path, nd); |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1678 | nd->inode = inode; |
| 1679 | nd->seq = seq; |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1680 | return NULL; |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1681 | } |
Al Viro | a7f7754 | 2016-02-27 19:31:01 -0500 | [diff] [blame] | 1682 | /* make sure that d_is_symlink above matches inode */ |
| 1683 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1684 | if (read_seqcount_retry(&path.dentry->d_seq, seq)) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1685 | return ERR_PTR(-ECHILD); |
Al Viro | a7f7754 | 2016-02-27 19:31:01 -0500 | [diff] [blame] | 1686 | } |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1687 | return pick_link(nd, &path, inode, seq, flags); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1690 | static struct dentry *follow_dotdot_rcu(struct nameidata *nd, |
| 1691 | struct inode **inodep, |
| 1692 | unsigned *seqp) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1693 | { |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1694 | struct dentry *parent, *old; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1695 | |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1696 | if (path_equal(&nd->path, &nd->root)) |
| 1697 | goto in_root; |
| 1698 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1699 | struct path path = nd->path; |
| 1700 | unsigned seq; |
| 1701 | |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1702 | while (1) { |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1703 | struct mount *mnt = real_mount(path.mnt); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1704 | struct mount *mparent = mnt->mnt_parent; |
| 1705 | struct dentry *mountpoint = mnt->mnt_mountpoint; |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1706 | seq = read_seqcount_begin(&mountpoint->d_seq); |
| 1707 | if (&mparent->mnt == path.mnt) |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1708 | goto in_root; |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1709 | path.dentry = mountpoint; |
| 1710 | path.mnt = &mparent->mnt; |
| 1711 | if (path_equal(&path, &nd->root)) |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1712 | goto in_root; |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1713 | if (path.dentry != path.mnt->mnt_root) |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1714 | break; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1715 | } |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1716 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1717 | return ERR_PTR(-ECHILD); |
| 1718 | nd->path = path; |
| 1719 | nd->inode = path.dentry->d_inode; |
| 1720 | nd->seq = seq; |
| 1721 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
| 1722 | return ERR_PTR(-ECHILD); |
| 1723 | /* we know that mountpoint was pinned */ |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1724 | } |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1725 | old = nd->path.dentry; |
| 1726 | parent = old->d_parent; |
| 1727 | *inodep = parent->d_inode; |
| 1728 | *seqp = read_seqcount_begin(&parent->d_seq); |
| 1729 | if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) |
| 1730 | return ERR_PTR(-ECHILD); |
| 1731 | if (unlikely(!path_connected(nd->path.mnt, parent))) |
| 1732 | return ERR_PTR(-ECHILD); |
| 1733 | return parent; |
| 1734 | in_root: |
Al Viro | efe772d | 2020-02-28 10:06:37 -0500 | [diff] [blame] | 1735 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
| 1736 | return ERR_PTR(-ECHILD); |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1737 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 1738 | return ERR_PTR(-ECHILD); |
| 1739 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1740 | } |
| 1741 | |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1742 | static struct dentry *follow_dotdot(struct nameidata *nd, |
| 1743 | struct inode **inodep, |
| 1744 | unsigned *seqp) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1745 | { |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1746 | struct dentry *parent; |
| 1747 | |
| 1748 | if (path_equal(&nd->path, &nd->root)) |
| 1749 | goto in_root; |
| 1750 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame^] | 1751 | struct path path = nd->path; |
| 1752 | path_get(&path); |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1753 | while (1) { |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame^] | 1754 | if (!follow_up(&path)) { |
| 1755 | path_put(&path); |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1756 | goto in_root; |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame^] | 1757 | } |
| 1758 | if (path_equal(&path, &nd->root)) { |
| 1759 | path_put(&path); |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1760 | goto in_root; |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame^] | 1761 | } |
| 1762 | if (path.dentry != nd->path.mnt->mnt_root) |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1763 | break; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1764 | } |
Al Viro | 165200d | 2020-02-28 10:17:52 -0500 | [diff] [blame^] | 1765 | path_put(&nd->path); |
| 1766 | nd->path = path; |
| 1767 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1768 | return ERR_PTR(-EXDEV); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1769 | } |
Al Viro | 12487f3 | 2020-02-26 14:59:56 -0500 | [diff] [blame] | 1770 | /* rare case of legitimate dget_parent()... */ |
| 1771 | parent = dget_parent(nd->path.dentry); |
| 1772 | if (unlikely(!path_connected(nd->path.mnt, parent))) { |
| 1773 | dput(parent); |
| 1774 | return ERR_PTR(-ENOENT); |
| 1775 | } |
| 1776 | *seqp = 0; |
| 1777 | *inodep = parent->d_inode; |
| 1778 | return parent; |
| 1779 | |
| 1780 | in_root: |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1781 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 1782 | return ERR_PTR(-EXDEV); |
| 1783 | dget(nd->path.dentry); |
| 1784 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1785 | } |
| 1786 | |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1787 | static const char *handle_dots(struct nameidata *nd, int type) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1788 | { |
| 1789 | if (type == LAST_DOTDOT) { |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1790 | const char *error = NULL; |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1791 | struct dentry *parent; |
| 1792 | struct inode *inode; |
| 1793 | unsigned seq; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1794 | |
| 1795 | if (!nd->root.mnt) { |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1796 | error = ERR_PTR(set_root(nd)); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1797 | if (error) |
| 1798 | return error; |
| 1799 | } |
| 1800 | if (nd->flags & LOOKUP_RCU) |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1801 | parent = follow_dotdot_rcu(nd, &inode, &seq); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1802 | else |
Al Viro | c2df196 | 2020-02-26 14:33:30 -0500 | [diff] [blame] | 1803 | parent = follow_dotdot(nd, &inode, &seq); |
| 1804 | if (IS_ERR(parent)) |
| 1805 | return ERR_CAST(parent); |
| 1806 | if (unlikely(!parent)) |
| 1807 | error = step_into(nd, WALK_NOFOLLOW, |
| 1808 | nd->path.dentry, nd->inode, nd->seq); |
| 1809 | else |
| 1810 | error = step_into(nd, WALK_NOFOLLOW, |
| 1811 | parent, inode, seq); |
| 1812 | if (unlikely(error)) |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1813 | return error; |
| 1814 | |
| 1815 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
| 1816 | /* |
| 1817 | * If there was a racing rename or mount along our |
| 1818 | * path, then we can't be sure that ".." hasn't jumped |
| 1819 | * above nd->root (and so userspace should retry or use |
| 1820 | * some fallback). |
| 1821 | */ |
| 1822 | smp_rmb(); |
| 1823 | if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))) |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1824 | return ERR_PTR(-EAGAIN); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1825 | if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))) |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1826 | return ERR_PTR(-EAGAIN); |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1827 | } |
| 1828 | } |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1829 | return NULL; |
Al Viro | 957dd41 | 2020-02-26 01:40:04 -0500 | [diff] [blame] | 1830 | } |
| 1831 | |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1832 | static const char *walk_component(struct nameidata *nd, int flags) |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1833 | { |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1834 | struct dentry *dentry; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1835 | struct inode *inode; |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1836 | unsigned seq; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1837 | /* |
| 1838 | * "." and ".." are special - ".." especially so because it has |
| 1839 | * to be able to know about the current root directory and |
| 1840 | * parent relationships. |
| 1841 | */ |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 1842 | if (unlikely(nd->last_type != LAST_NORM)) { |
Al Viro | 1c4ff1a | 2016-11-14 01:39:36 -0500 | [diff] [blame] | 1843 | if (!(flags & WALK_MORE) && nd->depth) |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 1844 | put_link(nd); |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 1845 | return handle_dots(nd, nd->last_type); |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 1846 | } |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1847 | dentry = lookup_fast(nd, &inode, &seq); |
| 1848 | if (IS_ERR(dentry)) |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1849 | return ERR_CAST(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1850 | if (unlikely(!dentry)) { |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1851 | dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); |
| 1852 | if (IS_ERR(dentry)) |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1853 | return ERR_CAST(dentry); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1854 | } |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 1855 | if (!(flags & WALK_MORE) && nd->depth) |
| 1856 | put_link(nd); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1857 | return step_into(nd, flags, dentry, inode, seq); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1858 | } |
| 1859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | /* |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1861 | * We can do the critical dentry name comparison and hashing |
| 1862 | * operations one word at a time, but we are limited to: |
| 1863 | * |
| 1864 | * - Architectures with fast unaligned word accesses. We could |
| 1865 | * do a "get_unaligned()" if this helps and is sufficiently |
| 1866 | * fast. |
| 1867 | * |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1868 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
| 1869 | * do not trap on the (extremely unlikely) case of a page |
| 1870 | * crossing operation. |
| 1871 | * |
| 1872 | * - Furthermore, we need an efficient 64-bit compile for the |
| 1873 | * 64-bit case in order to generate the "number of bytes in |
| 1874 | * the final mask". Again, that could be replaced with a |
| 1875 | * efficient population count instruction or similar. |
| 1876 | */ |
| 1877 | #ifdef CONFIG_DCACHE_WORD_ACCESS |
| 1878 | |
Linus Torvalds | f68e556 | 2012-04-06 13:54:56 -0700 | [diff] [blame] | 1879 | #include <asm/word-at-a-time.h> |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1880 | |
George Spelvin | 468a942 | 2016-05-26 22:11:51 -0400 | [diff] [blame] | 1881 | #ifdef HASH_MIX |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1882 | |
George Spelvin | 468a942 | 2016-05-26 22:11:51 -0400 | [diff] [blame] | 1883 | /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ |
| 1884 | |
| 1885 | #elif defined(CONFIG_64BIT) |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1886 | /* |
| 1887 | * Register pressure in the mixing function is an issue, particularly |
| 1888 | * on 32-bit x86, but almost any function requires one state value and |
| 1889 | * one temporary. Instead, use a function designed for two state values |
| 1890 | * and no temporaries. |
| 1891 | * |
| 1892 | * This function cannot create a collision in only two iterations, so |
| 1893 | * we have two iterations to achieve avalanche. In those two iterations, |
| 1894 | * we have six layers of mixing, which is enough to spread one bit's |
| 1895 | * influence out to 2^6 = 64 state bits. |
| 1896 | * |
| 1897 | * Rotate constants are scored by considering either 64 one-bit input |
| 1898 | * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the |
| 1899 | * probability of that delta causing a change to each of the 128 output |
| 1900 | * bits, using a sample of random initial states. |
| 1901 | * |
| 1902 | * The Shannon entropy of the computed probabilities is then summed |
| 1903 | * to produce a score. Ideally, any input change has a 50% chance of |
| 1904 | * toggling any given output bit. |
| 1905 | * |
| 1906 | * Mixing scores (in bits) for (12,45): |
| 1907 | * Input delta: 1-bit 2-bit |
| 1908 | * 1 round: 713.3 42542.6 |
| 1909 | * 2 rounds: 2753.7 140389.8 |
| 1910 | * 3 rounds: 5954.1 233458.2 |
| 1911 | * 4 rounds: 7862.6 256672.2 |
| 1912 | * Perfect: 8192 258048 |
| 1913 | * (64*128) (64*63/2 * 128) |
| 1914 | */ |
| 1915 | #define HASH_MIX(x, y, a) \ |
| 1916 | ( x ^= (a), \ |
| 1917 | y ^= x, x = rol64(x,12),\ |
| 1918 | x += y, y = rol64(y,45),\ |
| 1919 | y *= 9 ) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1920 | |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1921 | /* |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1922 | * Fold two longs into one 32-bit hash value. This must be fast, but |
| 1923 | * latency isn't quite as critical, as there is a fair bit of additional |
| 1924 | * work done before the hash value is used. |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1925 | */ |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1926 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1927 | { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1928 | y ^= x * GOLDEN_RATIO_64; |
| 1929 | y *= GOLDEN_RATIO_64; |
| 1930 | return y >> 32; |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1931 | } |
| 1932 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1933 | #else /* 32-bit case */ |
| 1934 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1935 | /* |
| 1936 | * Mixing scores (in bits) for (7,20): |
| 1937 | * Input delta: 1-bit 2-bit |
| 1938 | * 1 round: 330.3 9201.6 |
| 1939 | * 2 rounds: 1246.4 25475.4 |
| 1940 | * 3 rounds: 1907.1 31295.1 |
| 1941 | * 4 rounds: 2042.3 31718.6 |
| 1942 | * Perfect: 2048 31744 |
| 1943 | * (32*64) (32*31/2 * 64) |
| 1944 | */ |
| 1945 | #define HASH_MIX(x, y, a) \ |
| 1946 | ( x ^= (a), \ |
| 1947 | y ^= x, x = rol32(x, 7),\ |
| 1948 | x += y, y = rol32(y,20),\ |
| 1949 | y *= 9 ) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1950 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1951 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1952 | { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1953 | /* Use arch-optimized multiply if one exists */ |
| 1954 | return __hash_32(y ^ __hash_32(x)); |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1955 | } |
| 1956 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1957 | #endif |
| 1958 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1959 | /* |
| 1960 | * Return the hash of a string of known length. This is carfully |
| 1961 | * designed to match hash_name(), which is the more critical function. |
| 1962 | * In particular, we must end by hashing a final word containing 0..7 |
| 1963 | * payload bytes, to match the way that hash_name() iterates until it |
| 1964 | * finds the delimiter after the name. |
| 1965 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1966 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1967 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1968 | unsigned long a, x = 0, y = (unsigned long)salt; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1969 | |
| 1970 | for (;;) { |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1971 | if (!len) |
| 1972 | goto done; |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 1973 | a = load_unaligned_zeropad(name); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1974 | if (len < sizeof(unsigned long)) |
| 1975 | break; |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1976 | HASH_MIX(x, y, a); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1977 | name += sizeof(unsigned long); |
| 1978 | len -= sizeof(unsigned long); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1979 | } |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1980 | x ^= a & bytemask_from_count(len); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1981 | done: |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1982 | return fold_hash(x, y); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1983 | } |
| 1984 | EXPORT_SYMBOL(full_name_hash); |
| 1985 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1986 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1987 | u64 hashlen_string(const void *salt, const char *name) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1988 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1989 | unsigned long a = 0, x = 0, y = (unsigned long)salt; |
| 1990 | unsigned long adata, mask, len; |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1991 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
| 1992 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1993 | len = 0; |
| 1994 | goto inside; |
| 1995 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1996 | do { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1997 | HASH_MIX(x, y, a); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1998 | len += sizeof(unsigned long); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1999 | inside: |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2000 | a = load_unaligned_zeropad(name+len); |
| 2001 | } while (!has_zero(a, &adata, &constants)); |
| 2002 | |
| 2003 | adata = prep_zero_mask(a, adata, &constants); |
| 2004 | mask = create_zero_mask(adata); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2005 | x ^= a & zero_bytemask(mask); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2006 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2007 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2008 | } |
| 2009 | EXPORT_SYMBOL(hashlen_string); |
| 2010 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2011 | /* |
| 2012 | * Calculate the length and hash of the path component, and |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2013 | * return the "hash_len" as the result. |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2014 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2015 | static inline u64 hash_name(const void *salt, const char *name) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2016 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2017 | unsigned long a = 0, b, x = 0, y = (unsigned long)salt; |
| 2018 | unsigned long adata, bdata, mask, len; |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2019 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2020 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2021 | len = 0; |
| 2022 | goto inside; |
| 2023 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2024 | do { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2025 | HASH_MIX(x, y, a); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2026 | len += sizeof(unsigned long); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2027 | inside: |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 2028 | a = load_unaligned_zeropad(name+len); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2029 | b = a ^ REPEAT_BYTE('/'); |
| 2030 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2031 | |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2032 | adata = prep_zero_mask(a, adata, &constants); |
| 2033 | bdata = prep_zero_mask(b, bdata, &constants); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2034 | mask = create_zero_mask(adata | bdata); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2035 | x ^= a & zero_bytemask(mask); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2036 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2037 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2038 | } |
| 2039 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2040 | #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2041 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2042 | /* Return the hash of a string of known length */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2043 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2044 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2045 | unsigned long hash = init_name_hash(salt); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2046 | while (len--) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2047 | hash = partial_name_hash((unsigned char)*name++, hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2048 | return end_name_hash(hash); |
| 2049 | } |
Linus Torvalds | ae942ae | 2012-03-02 19:40:57 -0800 | [diff] [blame] | 2050 | EXPORT_SYMBOL(full_name_hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2051 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2052 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2053 | u64 hashlen_string(const void *salt, const char *name) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2054 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2055 | unsigned long hash = init_name_hash(salt); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2056 | unsigned long len = 0, c; |
| 2057 | |
| 2058 | c = (unsigned char)*name; |
George Spelvin | e0ab7af | 2016-05-29 08:05:56 -0400 | [diff] [blame] | 2059 | while (c) { |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2060 | len++; |
| 2061 | hash = partial_name_hash(c, hash); |
| 2062 | c = (unsigned char)name[len]; |
George Spelvin | e0ab7af | 2016-05-29 08:05:56 -0400 | [diff] [blame] | 2063 | } |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2064 | return hashlen_create(end_name_hash(hash), len); |
| 2065 | } |
George Spelvin | f2a031b | 2016-05-29 01:26:41 -0400 | [diff] [blame] | 2066 | EXPORT_SYMBOL(hashlen_string); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2067 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 2068 | /* |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2069 | * We know there's a real path component here of at least |
| 2070 | * one character. |
| 2071 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2072 | static inline u64 hash_name(const void *salt, const char *name) |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2073 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2074 | unsigned long hash = init_name_hash(salt); |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2075 | unsigned long len = 0, c; |
| 2076 | |
| 2077 | c = (unsigned char)*name; |
| 2078 | do { |
| 2079 | len++; |
| 2080 | hash = partial_name_hash(c, hash); |
| 2081 | c = (unsigned char)name[len]; |
| 2082 | } while (c && c != '/'); |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2083 | return hashlen_create(end_name_hash(hash), len); |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2084 | } |
| 2085 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2086 | #endif |
| 2087 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2088 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | * Name resolution. |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 2090 | * This is the basic name resolution function, turning a pathname into |
| 2091 | * the final dentry. We expect 'base' to be positive and a directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | * |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 2093 | * Returns 0 and nd will have valid dentry and mnt on success. |
| 2094 | * Returns error and drops reference to input namei data on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | */ |
Al Viro | 6de88d7 | 2009-08-09 01:41:57 +0400 | [diff] [blame] | 2096 | static int link_path_walk(const char *name, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | { |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2098 | int depth = 0; // depth <= nd->depth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | int err; |
Al Viro | 32cd746 | 2015-04-18 20:30:49 -0400 | [diff] [blame] | 2100 | |
Al Viro | b4c0353 | 2020-01-19 11:44:51 -0500 | [diff] [blame] | 2101 | nd->last_type = LAST_ROOT; |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2102 | nd->flags |= LOOKUP_PARENT; |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2103 | if (IS_ERR(name)) |
| 2104 | return PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | while (*name=='/') |
| 2106 | name++; |
| 2107 | if (!*name) |
Al Viro | 9e18f10 | 2015-04-18 20:44:34 -0400 | [diff] [blame] | 2108 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | /* At this point we know we have a real path component. */ |
| 2111 | for(;;) { |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2112 | const char *link; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2113 | u64 hash_len; |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2114 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 2116 | err = may_lookup(nd); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2117 | if (err) |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2118 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2120 | hash_len = hash_name(nd->path.dentry, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2122 | type = LAST_NORM; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2123 | if (name[0] == '.') switch (hashlen_len(hash_len)) { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2124 | case 2: |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2125 | if (name[1] == '.') { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2126 | type = LAST_DOTDOT; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2127 | nd->flags |= LOOKUP_JUMPED; |
| 2128 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2129 | break; |
| 2130 | case 1: |
| 2131 | type = LAST_DOT; |
| 2132 | } |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2133 | if (likely(type == LAST_NORM)) { |
| 2134 | struct dentry *parent = nd->path.dentry; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2135 | nd->flags &= ~LOOKUP_JUMPED; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2136 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
James Hogan | a060dc5 | 2014-09-16 13:07:35 +0100 | [diff] [blame] | 2137 | struct qstr this = { { .hash_len = hash_len }, .name = name }; |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 2138 | err = parent->d_op->d_hash(parent, &this); |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2139 | if (err < 0) |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2140 | return err; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2141 | hash_len = this.hash_len; |
| 2142 | name = this.name; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2143 | } |
| 2144 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2145 | |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2146 | nd->last.hash_len = hash_len; |
| 2147 | nd->last.name = name; |
Al Viro | 5f4a6a6 | 2013-01-24 18:04:22 -0500 | [diff] [blame] | 2148 | nd->last_type = type; |
| 2149 | |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2150 | name += hashlen_len(hash_len); |
| 2151 | if (!*name) |
Al Viro | bdf6cbf | 2015-04-18 20:21:40 -0400 | [diff] [blame] | 2152 | goto OK; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2153 | /* |
| 2154 | * If it wasn't NUL, we know it was '/'. Skip that |
| 2155 | * slash, and continue until no more slashes. |
| 2156 | */ |
| 2157 | do { |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2158 | name++; |
| 2159 | } while (unlikely(*name == '/')); |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2160 | if (unlikely(!*name)) { |
| 2161 | OK: |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2162 | /* pathname or trailing symlink, done */ |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2163 | if (!depth) { |
| 2164 | nd->flags &= ~LOOKUP_PARENT; |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2165 | return 0; |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2166 | } |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2167 | /* last component of nested symlink */ |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2168 | name = nd->stack[--depth].name; |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 2169 | link = walk_component(nd, 0); |
Al Viro | 1c4ff1a | 2016-11-14 01:39:36 -0500 | [diff] [blame] | 2170 | } else { |
| 2171 | /* not the last component */ |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 2172 | link = walk_component(nd, WALK_MORE); |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2173 | } |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2174 | if (unlikely(link)) { |
| 2175 | if (IS_ERR(link)) |
| 2176 | return PTR_ERR(link); |
| 2177 | /* a symlink to follow */ |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2178 | nd->stack[depth++].name = name; |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2179 | name = link; |
| 2180 | continue; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2181 | } |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2182 | if (unlikely(!d_can_lookup(nd->path.dentry))) { |
| 2183 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 2184 | if (unlazy_walk(nd)) |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2185 | return -ECHILD; |
| 2186 | } |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2187 | return -ENOTDIR; |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2192 | /* must be paired with terminate_walk() */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2193 | static const char *path_init(struct nameidata *nd, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 2195 | int error; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2196 | const char *s = nd->name->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | |
Linus Torvalds | c0eb027 | 2017-04-02 17:10:08 -0700 | [diff] [blame] | 2198 | if (!*s) |
| 2199 | flags &= ~LOOKUP_RCU; |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2200 | if (flags & LOOKUP_RCU) |
| 2201 | rcu_read_lock(); |
Linus Torvalds | c0eb027 | 2017-04-02 17:10:08 -0700 | [diff] [blame] | 2202 | |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2203 | nd->flags = flags | LOOKUP_JUMPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | nd->depth = 0; |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 2205 | |
| 2206 | nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); |
| 2207 | nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); |
| 2208 | smp_rmb(); |
| 2209 | |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2210 | if (flags & LOOKUP_ROOT) { |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2211 | struct dentry *root = nd->root.dentry; |
| 2212 | struct inode *inode = root->d_inode; |
Al Viro | 9389386 | 2017-04-15 17:29:14 -0400 | [diff] [blame] | 2213 | if (*s && unlikely(!d_can_lookup(root))) |
| 2214 | return ERR_PTR(-ENOTDIR); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2215 | nd->path = nd->root; |
| 2216 | nd->inode = inode; |
| 2217 | if (flags & LOOKUP_RCU) { |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 2218 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | 8f47a016 | 2015-05-09 19:02:01 -0400 | [diff] [blame] | 2219 | nd->root_seq = nd->seq; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2220 | } else { |
| 2221 | path_get(&nd->path); |
| 2222 | } |
Al Viro | 368ee9b | 2015-05-08 17:19:59 -0400 | [diff] [blame] | 2223 | return s; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2224 | } |
| 2225 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 2226 | nd->root.mnt = NULL; |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 2227 | nd->path.mnt = NULL; |
| 2228 | nd->path.dentry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2230 | /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ |
| 2231 | if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 2232 | error = nd_jump_root(nd); |
| 2233 | if (unlikely(error)) |
| 2234 | return ERR_PTR(error); |
| 2235 | return s; |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | /* Relative pathname -- get the starting-point it is relative to. */ |
| 2239 | if (nd->dfd == AT_FDCWD) { |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2240 | if (flags & LOOKUP_RCU) { |
| 2241 | struct fs_struct *fs = current->fs; |
| 2242 | unsigned seq; |
| 2243 | |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2244 | do { |
| 2245 | seq = read_seqcount_begin(&fs->seq); |
| 2246 | nd->path = fs->pwd; |
Al Viro | ef55d91 | 2015-12-05 20:25:06 -0500 | [diff] [blame] | 2247 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2248 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
| 2249 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 2250 | } else { |
| 2251 | get_fs_pwd(current->fs, &nd->path); |
Al Viro | ef55d91 | 2015-12-05 20:25:06 -0500 | [diff] [blame] | 2252 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2253 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2254 | } else { |
Jeff Layton | 582aa64 | 2012-12-11 08:56:16 -0500 | [diff] [blame] | 2255 | /* Caller must check execute permissions on the starting path component */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2256 | struct fd f = fdget_raw(nd->dfd); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2257 | struct dentry *dentry; |
| 2258 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2259 | if (!f.file) |
Al Viro | 368ee9b | 2015-05-08 17:19:59 -0400 | [diff] [blame] | 2260 | return ERR_PTR(-EBADF); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2261 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2262 | dentry = f.file->f_path.dentry; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2263 | |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2264 | if (*s && unlikely(!d_can_lookup(dentry))) { |
| 2265 | fdput(f); |
| 2266 | return ERR_PTR(-ENOTDIR); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 2267 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2268 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2269 | nd->path = f.file->f_path; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2270 | if (flags & LOOKUP_RCU) { |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2271 | nd->inode = nd->path.dentry->d_inode; |
| 2272 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2273 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2274 | path_get(&nd->path); |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2275 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2276 | } |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2277 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | } |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2279 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 2280 | /* For scoped-lookups we need to set the root to the dirfd as well. */ |
| 2281 | if (flags & LOOKUP_IS_SCOPED) { |
| 2282 | nd->root = nd->path; |
| 2283 | if (flags & LOOKUP_RCU) { |
| 2284 | nd->root_seq = nd->seq; |
| 2285 | } else { |
| 2286 | path_get(&nd->root); |
| 2287 | nd->flags |= LOOKUP_ROOT_GRABBED; |
| 2288 | } |
| 2289 | } |
| 2290 | return s; |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2291 | } |
| 2292 | |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 2293 | static inline const char *lookup_last(struct nameidata *nd) |
Al Viro | 95fa25d | 2015-04-22 13:46:57 -0400 | [diff] [blame] | 2294 | { |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2295 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) |
| 2296 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 2297 | |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2298 | return walk_component(nd, WALK_TRAILING); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2299 | } |
| 2300 | |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2301 | static int handle_lookup_down(struct nameidata *nd) |
| 2302 | { |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 2303 | if (!(nd->flags & LOOKUP_RCU)) |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 2304 | dget(nd->path.dentry); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 2305 | return PTR_ERR(step_into(nd, WALK_NOFOLLOW, |
| 2306 | nd->path.dentry, nd->inode, nd->seq)); |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2307 | } |
| 2308 | |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2309 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2310 | static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2311 | { |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2312 | const char *s = path_init(nd, flags); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2313 | int err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2314 | |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2315 | if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2316 | err = handle_lookup_down(nd); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 2317 | if (unlikely(err < 0)) |
| 2318 | s = ERR_PTR(err); |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2319 | } |
| 2320 | |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 2321 | while (!(err = link_path_walk(s, nd)) && |
| 2322 | (s = lookup_last(nd)) != NULL) |
| 2323 | ; |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 2324 | if (!err) |
| 2325 | err = complete_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2326 | |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 2327 | if (!err && nd->flags & LOOKUP_DIRECTORY) |
| 2328 | if (!d_can_lookup(nd->path.dentry)) |
Al Viro | bd23a53 | 2011-03-23 09:56:30 -0400 | [diff] [blame] | 2329 | err = -ENOTDIR; |
Al Viro | 161aff1 | 2020-01-11 22:52:26 -0500 | [diff] [blame] | 2330 | if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { |
| 2331 | err = handle_lookup_down(nd); |
| 2332 | nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please... |
| 2333 | } |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 2334 | if (!err) { |
| 2335 | *path = nd->path; |
| 2336 | nd->path.mnt = NULL; |
| 2337 | nd->path.dentry = NULL; |
| 2338 | } |
| 2339 | terminate_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2340 | return err; |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2341 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2342 | |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 2343 | int filename_lookup(int dfd, struct filename *name, unsigned flags, |
| 2344 | struct path *path, struct path *root) |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2345 | { |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 2346 | int retval; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2347 | struct nameidata nd; |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2348 | if (IS_ERR(name)) |
| 2349 | return PTR_ERR(name); |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2350 | if (unlikely(root)) { |
| 2351 | nd.root = *root; |
| 2352 | flags |= LOOKUP_ROOT; |
| 2353 | } |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2354 | set_nameidata(&nd, dfd, name); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2355 | retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2356 | if (unlikely(retval == -ECHILD)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2357 | retval = path_lookupat(&nd, flags, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2358 | if (unlikely(retval == -ESTALE)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2359 | retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2360 | |
| 2361 | if (likely(!retval)) |
Al Viro | 161aff1 | 2020-01-11 22:52:26 -0500 | [diff] [blame] | 2362 | audit_inode(name, path->dentry, |
| 2363 | flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2364 | restore_nameidata(); |
Al Viro | e4bd1c1 | 2015-05-12 16:40:39 -0400 | [diff] [blame] | 2365 | putname(name); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2366 | return retval; |
| 2367 | } |
| 2368 | |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2369 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2370 | static int path_parentat(struct nameidata *nd, unsigned flags, |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2371 | struct path *parent) |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2372 | { |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2373 | const char *s = path_init(nd, flags); |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2374 | int err = link_path_walk(s, nd); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2375 | if (!err) |
| 2376 | err = complete_walk(nd); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2377 | if (!err) { |
| 2378 | *parent = nd->path; |
| 2379 | nd->path.mnt = NULL; |
| 2380 | nd->path.dentry = NULL; |
| 2381 | } |
| 2382 | terminate_walk(nd); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2383 | return err; |
| 2384 | } |
| 2385 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2386 | static struct filename *filename_parentat(int dfd, struct filename *name, |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2387 | unsigned int flags, struct path *parent, |
| 2388 | struct qstr *last, int *type) |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2389 | { |
| 2390 | int retval; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2391 | struct nameidata nd; |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2392 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2393 | if (IS_ERR(name)) |
| 2394 | return name; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2395 | set_nameidata(&nd, dfd, name); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2396 | retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2397 | if (unlikely(retval == -ECHILD)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2398 | retval = path_parentat(&nd, flags, parent); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2399 | if (unlikely(retval == -ESTALE)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2400 | retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2401 | if (likely(!retval)) { |
| 2402 | *last = nd.last; |
| 2403 | *type = nd.last_type; |
Al Viro | c9b07ea | 2019-07-14 13:22:27 -0400 | [diff] [blame] | 2404 | audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2405 | } else { |
| 2406 | putname(name); |
| 2407 | name = ERR_PTR(retval); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2408 | } |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2409 | restore_nameidata(); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2410 | return name; |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2411 | } |
| 2412 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2413 | /* does lookup, returns the object with parent locked */ |
| 2414 | struct dentry *kern_path_locked(const char *name, struct path *path) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2415 | { |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2416 | struct filename *filename; |
| 2417 | struct dentry *d; |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2418 | struct qstr last; |
| 2419 | int type; |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2420 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2421 | filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path, |
| 2422 | &last, &type); |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2423 | if (IS_ERR(filename)) |
| 2424 | return ERR_CAST(filename); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2425 | if (unlikely(type != LAST_NORM)) { |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2426 | path_put(path); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2427 | putname(filename); |
| 2428 | return ERR_PTR(-EINVAL); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2429 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2430 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2431 | d = __lookup_hash(&last, path->dentry, 0); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2432 | if (IS_ERR(d)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2433 | inode_unlock(path->dentry->d_inode); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2434 | path_put(path); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2435 | } |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2436 | putname(filename); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2437 | return d; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2438 | } |
| 2439 | |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2440 | int kern_path(const char *name, unsigned int flags, struct path *path) |
| 2441 | { |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2442 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
| 2443 | flags, path, NULL); |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2444 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2445 | EXPORT_SYMBOL(kern_path); |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2446 | |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2447 | /** |
| 2448 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair |
| 2449 | * @dentry: pointer to dentry of the base directory |
| 2450 | * @mnt: pointer to vfs mount of the base directory |
| 2451 | * @name: pointer to file name |
| 2452 | * @flags: lookup flags |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2453 | * @path: pointer to struct path to fill |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2454 | */ |
| 2455 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, |
| 2456 | const char *name, unsigned int flags, |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2457 | struct path *path) |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2458 | { |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2459 | struct path root = {.mnt = mnt, .dentry = dentry}; |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2460 | /* the first argument of filename_lookup() is ignored with root */ |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2461 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
| 2462 | flags , path, &root); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2463 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2464 | EXPORT_SYMBOL(vfs_path_lookup); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2465 | |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2466 | static int lookup_one_len_common(const char *name, struct dentry *base, |
| 2467 | int len, struct qstr *this) |
| 2468 | { |
| 2469 | this->name = name; |
| 2470 | this->len = len; |
| 2471 | this->hash = full_name_hash(base, name, len); |
| 2472 | if (!len) |
| 2473 | return -EACCES; |
| 2474 | |
| 2475 | if (unlikely(name[0] == '.')) { |
| 2476 | if (len < 2 || (len == 2 && name[1] == '.')) |
| 2477 | return -EACCES; |
| 2478 | } |
| 2479 | |
| 2480 | while (len--) { |
| 2481 | unsigned int c = *(const unsigned char *)name++; |
| 2482 | if (c == '/' || c == '\0') |
| 2483 | return -EACCES; |
| 2484 | } |
| 2485 | /* |
| 2486 | * See if the low-level filesystem might want |
| 2487 | * to use its own hash.. |
| 2488 | */ |
| 2489 | if (base->d_flags & DCACHE_OP_HASH) { |
| 2490 | int err = base->d_op->d_hash(base, this); |
| 2491 | if (err < 0) |
| 2492 | return err; |
| 2493 | } |
| 2494 | |
| 2495 | return inode_permission(base->d_inode, MAY_EXEC); |
| 2496 | } |
| 2497 | |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2498 | /** |
David Howells | 0da0b7f | 2018-06-15 15:19:22 +0100 | [diff] [blame] | 2499 | * try_lookup_one_len - filesystem helper to lookup single pathname component |
| 2500 | * @name: pathname component to lookup |
| 2501 | * @base: base directory to lookup from |
| 2502 | * @len: maximum length @len should be interpreted to |
| 2503 | * |
| 2504 | * Look up a dentry by name in the dcache, returning NULL if it does not |
| 2505 | * currently exist. The function does not try to create a dentry. |
| 2506 | * |
| 2507 | * Note that this routine is purely a helper for filesystem usage and should |
| 2508 | * not be called by generic code. |
| 2509 | * |
| 2510 | * The caller must hold base->i_mutex. |
| 2511 | */ |
| 2512 | struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) |
| 2513 | { |
| 2514 | struct qstr this; |
| 2515 | int err; |
| 2516 | |
| 2517 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
| 2518 | |
| 2519 | err = lookup_one_len_common(name, base, len, &this); |
| 2520 | if (err) |
| 2521 | return ERR_PTR(err); |
| 2522 | |
| 2523 | return lookup_dcache(&this, base, 0); |
| 2524 | } |
| 2525 | EXPORT_SYMBOL(try_lookup_one_len); |
| 2526 | |
| 2527 | /** |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2528 | * lookup_one_len - filesystem helper to lookup single pathname component |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2529 | * @name: pathname component to lookup |
| 2530 | * @base: base directory to lookup from |
| 2531 | * @len: maximum length @len should be interpreted to |
| 2532 | * |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2533 | * Note that this routine is purely a helper for filesystem usage and should |
Al Viro | 9e7543e | 2015-02-23 02:49:48 -0500 | [diff] [blame] | 2534 | * not be called by generic code. |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2535 | * |
| 2536 | * The caller must hold base->i_mutex. |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2537 | */ |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2538 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) |
| 2539 | { |
Al Viro | 8613a20 | 2018-04-06 16:45:33 -0400 | [diff] [blame] | 2540 | struct dentry *dentry; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2541 | struct qstr this; |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2542 | int err; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2543 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2544 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2545 | |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2546 | err = lookup_one_len_common(name, base, len, &this); |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2547 | if (err) |
| 2548 | return ERR_PTR(err); |
| 2549 | |
Al Viro | 8613a20 | 2018-04-06 16:45:33 -0400 | [diff] [blame] | 2550 | dentry = lookup_dcache(&this, base, 0); |
| 2551 | return dentry ? dentry : __lookup_slow(&this, base, 0); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2552 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2553 | EXPORT_SYMBOL(lookup_one_len); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2554 | |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2555 | /** |
| 2556 | * lookup_one_len_unlocked - filesystem helper to lookup single pathname component |
| 2557 | * @name: pathname component to lookup |
| 2558 | * @base: base directory to lookup from |
| 2559 | * @len: maximum length @len should be interpreted to |
| 2560 | * |
| 2561 | * Note that this routine is purely a helper for filesystem usage and should |
| 2562 | * not be called by generic code. |
| 2563 | * |
| 2564 | * Unlike lookup_one_len, it should be called without the parent |
| 2565 | * i_mutex held, and will take the i_mutex itself if necessary. |
| 2566 | */ |
| 2567 | struct dentry *lookup_one_len_unlocked(const char *name, |
| 2568 | struct dentry *base, int len) |
| 2569 | { |
| 2570 | struct qstr this; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2571 | int err; |
Linus Torvalds | 20d00ee | 2016-07-29 12:17:52 -0700 | [diff] [blame] | 2572 | struct dentry *ret; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2573 | |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2574 | err = lookup_one_len_common(name, base, len, &this); |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2575 | if (err) |
| 2576 | return ERR_PTR(err); |
| 2577 | |
Linus Torvalds | 20d00ee | 2016-07-29 12:17:52 -0700 | [diff] [blame] | 2578 | ret = lookup_dcache(&this, base, 0); |
| 2579 | if (!ret) |
| 2580 | ret = lookup_slow(&this, base, 0); |
| 2581 | return ret; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2582 | } |
| 2583 | EXPORT_SYMBOL(lookup_one_len_unlocked); |
| 2584 | |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 2585 | /* |
| 2586 | * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) |
| 2587 | * on negatives. Returns known positive or ERR_PTR(); that's what |
| 2588 | * most of the users want. Note that pinned negative with unlocked parent |
| 2589 | * _can_ become positive at any time, so callers of lookup_one_len_unlocked() |
| 2590 | * need to be very careful; pinned positives have ->d_inode stable, so |
| 2591 | * this one avoids such problems. |
| 2592 | */ |
| 2593 | struct dentry *lookup_positive_unlocked(const char *name, |
| 2594 | struct dentry *base, int len) |
| 2595 | { |
| 2596 | struct dentry *ret = lookup_one_len_unlocked(name, base, len); |
Al Viro | 2fa6b1e | 2019-11-12 16:13:06 -0500 | [diff] [blame] | 2597 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 2598 | dput(ret); |
| 2599 | ret = ERR_PTR(-ENOENT); |
| 2600 | } |
| 2601 | return ret; |
| 2602 | } |
| 2603 | EXPORT_SYMBOL(lookup_positive_unlocked); |
| 2604 | |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2605 | #ifdef CONFIG_UNIX98_PTYS |
| 2606 | int path_pts(struct path *path) |
| 2607 | { |
| 2608 | /* Find something mounted on "pts" in the same directory as |
| 2609 | * the input path. |
| 2610 | */ |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2611 | struct dentry *parent = dget_parent(path->dentry); |
| 2612 | struct dentry *child; |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2613 | struct qstr this; |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2614 | |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2615 | if (unlikely(!path_connected(path->mnt, parent))) { |
| 2616 | dput(parent); |
Al Viro | 63b2772 | 2020-02-24 16:01:19 -0500 | [diff] [blame] | 2617 | return -ENOENT; |
Al Viro | a6a7eb7 | 2020-03-11 13:05:03 -0400 | [diff] [blame] | 2618 | } |
Al Viro | 63b2772 | 2020-02-24 16:01:19 -0500 | [diff] [blame] | 2619 | dput(path->dentry); |
| 2620 | path->dentry = parent; |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2621 | this.name = "pts"; |
| 2622 | this.len = 3; |
| 2623 | child = d_hash_and_lookup(parent, &this); |
| 2624 | if (!child) |
| 2625 | return -ENOENT; |
| 2626 | |
| 2627 | path->dentry = child; |
| 2628 | dput(parent); |
| 2629 | follow_mount(path); |
| 2630 | return 0; |
| 2631 | } |
| 2632 | #endif |
| 2633 | |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2634 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
| 2635 | struct path *path, int *empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | { |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2637 | return filename_lookup(dfd, getname_flags(name, flags, empty), |
| 2638 | flags, path, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | } |
Al Viro | b853a16 | 2015-05-13 09:12:02 -0400 | [diff] [blame] | 2640 | EXPORT_SYMBOL(user_path_at_empty); |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2641 | |
Miklos Szeredi | cbdf35b | 2014-10-24 00:14:36 +0200 | [diff] [blame] | 2642 | int __check_sticky(struct inode *dir, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | { |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2644 | kuid_t fsuid = current_fsuid(); |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 2645 | |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2646 | if (uid_eq(inode->i_uid, fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | return 0; |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2648 | if (uid_eq(dir->i_uid, fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | return 0; |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 2650 | return !capable_wrt_inode_uidgid(inode, CAP_FOWNER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | } |
Miklos Szeredi | cbdf35b | 2014-10-24 00:14:36 +0200 | [diff] [blame] | 2652 | EXPORT_SYMBOL(__check_sticky); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | |
| 2654 | /* |
| 2655 | * Check whether we can remove a link victim from directory dir, check |
| 2656 | * whether the type of victim is right. |
| 2657 | * 1. We can't do it if dir is read-only (done in permission()) |
| 2658 | * 2. We should have write and exec permissions on dir |
| 2659 | * 3. We can't remove anything from append-only dir |
| 2660 | * 4. We can't do anything with immutable dir (done in permission()) |
| 2661 | * 5. If the sticky bit on dir is set we should either |
| 2662 | * a. be owner of dir, or |
| 2663 | * b. be owner of victim, or |
| 2664 | * c. have CAP_FOWNER capability |
| 2665 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 2666 | * links pointing to it. |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 2667 | * 7. If the victim has an unknown uid or gid we can't change the inode. |
| 2668 | * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 2669 | * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 2670 | * 10. We can't remove a root or mountpoint. |
| 2671 | * 11. We don't allow removal of NFS sillyrenamed files; it's handled by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | * nfs_async_unlink(). |
| 2673 | */ |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2674 | static int may_delete(struct inode *dir, struct dentry *victim, bool isdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | { |
David Howells | 63afdfc | 2015-05-06 15:59:00 +0100 | [diff] [blame] | 2676 | struct inode *inode = d_backing_inode(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | int error; |
| 2678 | |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2679 | if (d_is_negative(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | return -ENOENT; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2681 | BUG_ON(!inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | |
| 2683 | BUG_ON(victim->d_parent->d_inode != dir); |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 2684 | |
| 2685 | /* Inode writeback is not safe when the uid or gid are invalid. */ |
| 2686 | if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid)) |
| 2687 | return -EOVERFLOW; |
| 2688 | |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 2689 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 2691 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | if (error) |
| 2693 | return error; |
| 2694 | if (IS_APPEND(dir)) |
| 2695 | return -EPERM; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2696 | |
| 2697 | if (check_sticky(dir, inode) || IS_APPEND(inode) || |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 2698 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | return -EPERM; |
| 2700 | if (isdir) { |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 2701 | if (!d_is_dir(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | return -ENOTDIR; |
| 2703 | if (IS_ROOT(victim)) |
| 2704 | return -EBUSY; |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 2705 | } else if (d_is_dir(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | return -EISDIR; |
| 2707 | if (IS_DEADDIR(dir)) |
| 2708 | return -ENOENT; |
| 2709 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 2710 | return -EBUSY; |
| 2711 | return 0; |
| 2712 | } |
| 2713 | |
| 2714 | /* Check whether we can create an object with dentry child in directory |
| 2715 | * dir. |
| 2716 | * 1. We can't do it if child already exists (open has special treatment for |
| 2717 | * this case, but since we are inlined it's OK) |
| 2718 | * 2. We can't do it if dir is read-only (done in permission()) |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2719 | * 3. We can't do it if the fs can't represent the fsuid or fsgid. |
| 2720 | * 4. We should have write and exec permissions on dir |
| 2721 | * 5. We can't do it if dir is immutable (done in permission()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | */ |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 2723 | static inline int may_create(struct inode *dir, struct dentry *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | { |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2725 | struct user_namespace *s_user_ns; |
Jeff Layton | 14e972b | 2013-05-08 10:25:58 -0400 | [diff] [blame] | 2726 | audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | if (child->d_inode) |
| 2728 | return -EEXIST; |
| 2729 | if (IS_DEADDIR(dir)) |
| 2730 | return -ENOENT; |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2731 | s_user_ns = dir->i_sb->s_user_ns; |
| 2732 | if (!kuid_has_mapping(s_user_ns, current_fsuid()) || |
| 2733 | !kgid_has_mapping(s_user_ns, current_fsgid())) |
| 2734 | return -EOVERFLOW; |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 2735 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | } |
| 2737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | /* |
| 2739 | * p1 and p2 should be directories on the same fs. |
| 2740 | */ |
| 2741 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) |
| 2742 | { |
| 2743 | struct dentry *p; |
| 2744 | |
| 2745 | if (p1 == p2) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2746 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | return NULL; |
| 2748 | } |
| 2749 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 2750 | mutex_lock(&p1->d_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2752 | p = d_ancestor(p2, p1); |
| 2753 | if (p) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2754 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); |
| 2755 | inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2756 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2759 | p = d_ancestor(p1, p2); |
| 2760 | if (p) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2761 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
| 2762 | inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2763 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2766 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
| 2767 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | return NULL; |
| 2769 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2770 | EXPORT_SYMBOL(lock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | |
| 2772 | void unlock_rename(struct dentry *p1, struct dentry *p2) |
| 2773 | { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2774 | inode_unlock(p1->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | if (p1 != p2) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2776 | inode_unlock(p2->d_inode); |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 2777 | mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | } |
| 2779 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2780 | EXPORT_SYMBOL(unlock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2782 | int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2783 | bool want_excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 2785 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | if (error) |
| 2787 | return error; |
| 2788 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 2789 | if (!dir->i_op->create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | return -EACCES; /* shouldn't it be ENOSYS? */ |
| 2791 | mode &= S_IALLUGO; |
| 2792 | mode |= S_IFREG; |
| 2793 | error = security_inode_create(dir, dentry, mode); |
| 2794 | if (error) |
| 2795 | return error; |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2796 | error = dir->i_op->create(dir, dentry, mode, want_excl); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 2797 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 2798 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | return error; |
| 2800 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2801 | EXPORT_SYMBOL(vfs_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | |
Al Viro | 8e6c848 | 2017-12-01 17:12:45 -0500 | [diff] [blame] | 2803 | int vfs_mkobj(struct dentry *dentry, umode_t mode, |
| 2804 | int (*f)(struct dentry *, umode_t, void *), |
| 2805 | void *arg) |
| 2806 | { |
| 2807 | struct inode *dir = dentry->d_parent->d_inode; |
| 2808 | int error = may_create(dir, dentry); |
| 2809 | if (error) |
| 2810 | return error; |
| 2811 | |
| 2812 | mode &= S_IALLUGO; |
| 2813 | mode |= S_IFREG; |
| 2814 | error = security_inode_create(dir, dentry, mode); |
| 2815 | if (error) |
| 2816 | return error; |
| 2817 | error = f(dentry, mode, arg); |
| 2818 | if (!error) |
| 2819 | fsnotify_create(dir, dentry); |
| 2820 | return error; |
| 2821 | } |
| 2822 | EXPORT_SYMBOL(vfs_mkobj); |
| 2823 | |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 2824 | bool may_open_dev(const struct path *path) |
| 2825 | { |
| 2826 | return !(path->mnt->mnt_flags & MNT_NODEV) && |
| 2827 | !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); |
| 2828 | } |
| 2829 | |
Al Viro | f0bb5aa | 2016-11-20 20:27:12 -0500 | [diff] [blame] | 2830 | static int may_open(const struct path *path, int acc_mode, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | { |
Christoph Hellwig | 3fb6419 | 2008-10-24 09:58:10 +0200 | [diff] [blame] | 2832 | struct dentry *dentry = path->dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | struct inode *inode = dentry->d_inode; |
| 2834 | int error; |
| 2835 | |
| 2836 | if (!inode) |
| 2837 | return -ENOENT; |
| 2838 | |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2839 | switch (inode->i_mode & S_IFMT) { |
| 2840 | case S_IFLNK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | return -ELOOP; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2842 | case S_IFDIR: |
| 2843 | if (acc_mode & MAY_WRITE) |
| 2844 | return -EISDIR; |
| 2845 | break; |
| 2846 | case S_IFBLK: |
| 2847 | case S_IFCHR: |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 2848 | if (!may_open_dev(path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | return -EACCES; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2850 | /*FALLTHRU*/ |
| 2851 | case S_IFIFO: |
| 2852 | case S_IFSOCK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | flag &= ~O_TRUNC; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2854 | break; |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 2855 | } |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 2856 | |
Al Viro | 62fb4a1 | 2015-12-26 22:33:24 -0500 | [diff] [blame] | 2857 | error = inode_permission(inode, MAY_OPEN | acc_mode); |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 2858 | if (error) |
| 2859 | return error; |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 2860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | /* |
| 2862 | * An append-only file must be opened in append mode for writing. |
| 2863 | */ |
| 2864 | if (IS_APPEND(inode)) { |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 2865 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2866 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | if (flag & O_TRUNC) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2868 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | /* O_NOATIME can only be set by the owner or superuser */ |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 2872 | if (flag & O_NOATIME && !inode_owner_or_capable(inode)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2873 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | |
J. Bruce Fields | f3c7691e | 2011-09-21 10:58:13 -0400 | [diff] [blame] | 2875 | return 0; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2876 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2878 | static int handle_truncate(struct file *filp) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2879 | { |
Al Viro | f0bb5aa | 2016-11-20 20:27:12 -0500 | [diff] [blame] | 2880 | const struct path *path = &filp->f_path; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2881 | struct inode *inode = path->dentry->d_inode; |
| 2882 | int error = get_write_access(inode); |
| 2883 | if (error) |
| 2884 | return error; |
| 2885 | /* |
| 2886 | * Refuse to truncate files with mandatory locks held on them. |
| 2887 | */ |
Jeff Layton | d7a0698 | 2014-03-10 09:54:15 -0400 | [diff] [blame] | 2888 | error = locks_verify_locked(filp); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2889 | if (!error) |
Tetsuo Handa | ea0d3ab | 2010-06-02 13:24:43 +0900 | [diff] [blame] | 2890 | error = security_path_truncate(path); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2891 | if (!error) { |
| 2892 | error = do_truncate(path->dentry, 0, |
| 2893 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2894 | filp); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2895 | } |
| 2896 | put_write_access(inode); |
Mimi Zohar | acd0c93 | 2009-09-04 13:08:46 -0400 | [diff] [blame] | 2897 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | } |
| 2899 | |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 2900 | static inline int open_to_namei_flags(int flag) |
| 2901 | { |
Al Viro | 8a5e929 | 2011-06-25 19:15:54 -0400 | [diff] [blame] | 2902 | if ((flag & O_ACCMODE) == 3) |
| 2903 | flag--; |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 2904 | return flag; |
| 2905 | } |
| 2906 | |
Al Viro | d360775 | 2016-03-25 15:21:09 -0400 | [diff] [blame] | 2907 | static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2908 | { |
Seth Forshee | 1328c72 | 2017-01-26 14:33:46 -0600 | [diff] [blame] | 2909 | struct user_namespace *s_user_ns; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2910 | int error = security_path_mknod(dir, dentry, mode, 0); |
| 2911 | if (error) |
| 2912 | return error; |
| 2913 | |
Seth Forshee | 1328c72 | 2017-01-26 14:33:46 -0600 | [diff] [blame] | 2914 | s_user_ns = dir->dentry->d_sb->s_user_ns; |
| 2915 | if (!kuid_has_mapping(s_user_ns, current_fsuid()) || |
| 2916 | !kgid_has_mapping(s_user_ns, current_fsgid())) |
| 2917 | return -EOVERFLOW; |
| 2918 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2919 | error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); |
| 2920 | if (error) |
| 2921 | return error; |
| 2922 | |
| 2923 | return security_inode_create(dir->dentry->d_inode, dentry, mode); |
| 2924 | } |
| 2925 | |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2926 | /* |
| 2927 | * Attempt to atomically look up, create and open a file from a negative |
| 2928 | * dentry. |
| 2929 | * |
| 2930 | * Returns 0 if successful. The file will have been created and attached to |
| 2931 | * @file by the filesystem calling finish_open(). |
| 2932 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 2933 | * If the file was looked up only or didn't need creating, FMODE_OPENED won't |
| 2934 | * be set. The caller will need to perform the open themselves. @path will |
| 2935 | * have been updated to point to the new dentry. This may be negative. |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2936 | * |
| 2937 | * Returns an error code otherwise. |
| 2938 | */ |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 2939 | static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, |
| 2940 | struct file *file, |
| 2941 | const struct open_flags *op, |
| 2942 | int open_flag, umode_t mode) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2943 | { |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2944 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2945 | struct inode *dir = nd->path.dentry->d_inode; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2946 | int error; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2947 | |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 2948 | if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */ |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2949 | open_flag &= ~O_TRUNC; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2950 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2951 | if (nd->flags & LOOKUP_DIRECTORY) |
| 2952 | open_flag |= O_DIRECTORY; |
| 2953 | |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2954 | file->f_path.dentry = DENTRY_NOT_SET; |
| 2955 | file->f_path.mnt = nd->path.mnt; |
Al Viro | 0fb1ea0 | 2016-04-27 14:13:10 -0400 | [diff] [blame] | 2956 | error = dir->i_op->atomic_open(dir, dentry, file, |
Al Viro | 44907d7 | 2018-06-08 13:32:02 -0400 | [diff] [blame] | 2957 | open_to_namei_flags(open_flag), mode); |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 2958 | d_lookup_done(dentry); |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 2959 | if (!error) { |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 2960 | if (file->f_mode & FMODE_OPENED) { |
Al Viro | 6fb968c | 2020-01-26 09:53:19 -0500 | [diff] [blame] | 2961 | if (unlikely(dentry != file->f_path.dentry)) { |
| 2962 | dput(dentry); |
| 2963 | dentry = dget(file->f_path.dentry); |
| 2964 | } |
Al Viro | 7be219b | 2020-01-26 10:02:29 -0500 | [diff] [blame] | 2965 | if (file->f_mode & FMODE_CREATED) |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 2966 | fsnotify_create(dir, dentry); |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 2967 | } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2968 | error = -EIO; |
Al Viro | 03da633 | 2013-09-16 19:22:33 -0400 | [diff] [blame] | 2969 | } else { |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 2970 | if (file->f_path.dentry) { |
| 2971 | dput(dentry); |
| 2972 | dentry = file->f_path.dentry; |
Al Viro | 03da633 | 2013-09-16 19:22:33 -0400 | [diff] [blame] | 2973 | } |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 2974 | if (file->f_mode & FMODE_CREATED) |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 2975 | fsnotify_create(dir, dentry); |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 2976 | if (unlikely(d_is_negative(dentry))) |
Al Viro | a01e718 | 2016-06-07 21:53:51 -0400 | [diff] [blame] | 2977 | error = -ENOENT; |
Sage Weil | 62b2ce9 | 2012-08-15 13:30:12 -0700 | [diff] [blame] | 2978 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2979 | } |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 2980 | if (error) { |
| 2981 | dput(dentry); |
| 2982 | dentry = ERR_PTR(error); |
| 2983 | } |
| 2984 | return dentry; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2985 | } |
| 2986 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2987 | /* |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2988 | * Look up and maybe create and open the last component. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2989 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 2990 | * Must be called with parent locked (exclusive in O_CREAT case). |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2991 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 2992 | * Returns 0 on success, that is, if |
| 2993 | * the file was successfully atomically created (if necessary) and opened, or |
| 2994 | * the file was not completely opened at this time, though lookups and |
| 2995 | * creations were performed. |
| 2996 | * These case are distinguished by presence of FMODE_OPENED on file->f_mode. |
| 2997 | * In the latter case dentry returned in @path might be negative if O_CREAT |
| 2998 | * hadn't been specified. |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2999 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3000 | * An error code is returned on failure. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3001 | */ |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3002 | static struct dentry *lookup_open(struct nameidata *nd, struct file *file, |
| 3003 | const struct open_flags *op, |
| 3004 | bool got_write) |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3005 | { |
| 3006 | struct dentry *dir = nd->path.dentry; |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3007 | struct inode *dir_inode = dir->d_inode; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3008 | int open_flag = op->open_flag; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3009 | struct dentry *dentry; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3010 | int error, create_error = 0; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3011 | umode_t mode = op->mode; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3012 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3013 | |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3014 | if (unlikely(IS_DEADDIR(dir_inode))) |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3015 | return ERR_PTR(-ENOENT); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3016 | |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3017 | file->f_mode &= ~FMODE_CREATED; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3018 | dentry = d_lookup(dir, &nd->last); |
| 3019 | for (;;) { |
| 3020 | if (!dentry) { |
| 3021 | dentry = d_alloc_parallel(dir, &nd->last, &wq); |
| 3022 | if (IS_ERR(dentry)) |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3023 | return dentry; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3024 | } |
| 3025 | if (d_in_lookup(dentry)) |
| 3026 | break; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3027 | |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3028 | error = d_revalidate(dentry, nd->flags); |
| 3029 | if (likely(error > 0)) |
| 3030 | break; |
| 3031 | if (error) |
| 3032 | goto out_dput; |
| 3033 | d_invalidate(dentry); |
| 3034 | dput(dentry); |
| 3035 | dentry = NULL; |
| 3036 | } |
| 3037 | if (dentry->d_inode) { |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 3038 | /* Cached positive dentry: will open in f_op->open */ |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3039 | return dentry; |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 3040 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3041 | |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3042 | /* |
| 3043 | * Checking write permission is tricky, bacuse we don't know if we are |
| 3044 | * going to actually need it: O_CREAT opens should work as long as the |
| 3045 | * file exists. But checking existence breaks atomicity. The trick is |
| 3046 | * to check access and if not granted clear O_CREAT from the flags. |
| 3047 | * |
| 3048 | * Another problem is returing the "right" error value (e.g. for an |
| 3049 | * O_EXCL open we want to return EEXIST not EROFS). |
| 3050 | */ |
| 3051 | if (open_flag & O_CREAT) { |
| 3052 | if (!IS_POSIXACL(dir->d_inode)) |
| 3053 | mode &= ~current_umask(); |
| 3054 | if (unlikely(!got_write)) { |
| 3055 | create_error = -EROFS; |
| 3056 | open_flag &= ~O_CREAT; |
| 3057 | if (open_flag & (O_EXCL | O_TRUNC)) |
| 3058 | goto no_open; |
| 3059 | /* No side effects, safe to clear O_CREAT */ |
| 3060 | } else { |
| 3061 | create_error = may_o_create(&nd->path, dentry, mode); |
| 3062 | if (create_error) { |
| 3063 | open_flag &= ~O_CREAT; |
| 3064 | if (open_flag & O_EXCL) |
| 3065 | goto no_open; |
| 3066 | } |
| 3067 | } |
| 3068 | } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) && |
| 3069 | unlikely(!got_write)) { |
| 3070 | /* |
| 3071 | * No O_CREATE -> atomicity not a requirement -> fall |
| 3072 | * back to lookup + open |
| 3073 | */ |
| 3074 | goto no_open; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3075 | } |
| 3076 | |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3077 | if (dir_inode->i_op->atomic_open) { |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3078 | dentry = atomic_open(nd, dentry, file, op, open_flag, mode); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3079 | if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) |
| 3080 | dentry = ERR_PTR(create_error); |
| 3081 | return dentry; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3082 | } |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3083 | |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3084 | no_open: |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3085 | if (d_in_lookup(dentry)) { |
Al Viro | 12fa5e2 | 2016-04-28 11:19:43 -0400 | [diff] [blame] | 3086 | struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, |
| 3087 | nd->flags); |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3088 | d_lookup_done(dentry); |
Al Viro | 12fa5e2 | 2016-04-28 11:19:43 -0400 | [diff] [blame] | 3089 | if (unlikely(res)) { |
| 3090 | if (IS_ERR(res)) { |
| 3091 | error = PTR_ERR(res); |
| 3092 | goto out_dput; |
| 3093 | } |
| 3094 | dput(dentry); |
| 3095 | dentry = res; |
| 3096 | } |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3097 | } |
| 3098 | |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3099 | /* Negative dentry, just create the file */ |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3100 | if (!dentry->d_inode && (open_flag & O_CREAT)) { |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3101 | file->f_mode |= FMODE_CREATED; |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3102 | audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3103 | if (!dir_inode->i_op->create) { |
| 3104 | error = -EACCES; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3105 | goto out_dput; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3106 | } |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3107 | error = dir_inode->i_op->create(dir_inode, dentry, mode, |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3108 | open_flag & O_EXCL); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3109 | if (error) |
| 3110 | goto out_dput; |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3111 | fsnotify_create(dir_inode, dentry); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3112 | } |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3113 | if (unlikely(create_error) && !dentry->d_inode) { |
| 3114 | error = create_error; |
| 3115 | goto out_dput; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3116 | } |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3117 | return dentry; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3118 | |
| 3119 | out_dput: |
| 3120 | dput(dentry); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3121 | return ERR_PTR(error); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3122 | } |
| 3123 | |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3124 | static const char *open_last_lookups(struct nameidata *nd, |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3125 | struct file *file, const struct open_flags *op) |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3126 | { |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 3127 | struct dentry *dir = nd->path.dentry; |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 3128 | int open_flag = op->open_flag; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3129 | bool got_write = false; |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 3130 | unsigned seq; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 3131 | struct inode *inode; |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3132 | struct dentry *dentry; |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3133 | const char *res; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 3134 | int error; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3135 | |
Al Viro | c3e380b | 2011-02-23 13:39:45 -0500 | [diff] [blame] | 3136 | nd->flags |= op->intent; |
| 3137 | |
Al Viro | bc77daa | 2013-06-06 09:12:33 -0400 | [diff] [blame] | 3138 | if (nd->last_type != LAST_NORM) { |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 3139 | if (nd->depth) |
| 3140 | put_link(nd); |
Al Viro | 7521f22 | 2020-02-26 12:22:58 -0500 | [diff] [blame] | 3141 | res = handle_dots(nd, nd->last_type); |
| 3142 | if (likely(!res)) |
| 3143 | res = ERR_PTR(complete_walk(nd)); |
| 3144 | return res; |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 3145 | } |
Al Viro | 67ee3ad | 2009-12-26 07:01:01 -0500 | [diff] [blame] | 3146 | |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 3147 | if (!(open_flag & O_CREAT)) { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3148 | if (nd->last.name[nd->last.len]) |
| 3149 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 3150 | /* we _can_ be in RCU mode here */ |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3151 | dentry = lookup_fast(nd, &inode, &seq); |
| 3152 | if (IS_ERR(dentry)) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3153 | return ERR_CAST(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3154 | if (likely(dentry)) |
Miklos Szeredi | 7157486 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 3155 | goto finish_lookup; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 3156 | |
Al Viro | 6583fe2 | 2016-03-05 18:14:03 -0500 | [diff] [blame] | 3157 | BUG_ON(nd->flags & LOOKUP_RCU); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3158 | } else { |
| 3159 | /* create side of things */ |
| 3160 | /* |
| 3161 | * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED |
| 3162 | * has been cleared when we got to the last component we are |
| 3163 | * about to look up |
| 3164 | */ |
| 3165 | error = complete_walk(nd); |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3166 | if (unlikely(error)) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3167 | return ERR_PTR(error); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3168 | |
Al Viro | c9b07ea | 2019-07-14 13:22:27 -0400 | [diff] [blame] | 3169 | audit_inode(nd->name, dir, AUDIT_INODE_PARENT); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3170 | /* trailing slashes? */ |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 3171 | if (unlikely(nd->last.name[nd->last.len])) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3172 | return ERR_PTR(-EISDIR); |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3173 | } |
| 3174 | |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3175 | if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3176 | error = mnt_want_write(nd->path.mnt); |
| 3177 | if (!error) |
| 3178 | got_write = true; |
| 3179 | /* |
| 3180 | * do _not_ fail yet - we might not need that or fail with |
| 3181 | * a different error; let lookup_open() decide; we'll be |
| 3182 | * dropping this one anyway. |
| 3183 | */ |
| 3184 | } |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3185 | if (open_flag & O_CREAT) |
| 3186 | inode_lock(dir->d_inode); |
| 3187 | else |
| 3188 | inode_lock_shared(dir->d_inode); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3189 | dentry = lookup_open(nd, file, op, got_write); |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3190 | if (open_flag & O_CREAT) |
| 3191 | inode_unlock(dir->d_inode); |
| 3192 | else |
| 3193 | inode_unlock_shared(dir->d_inode); |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 3194 | |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3195 | if (got_write) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3196 | mnt_drop_write(nd->path.mnt); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3197 | |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3198 | if (IS_ERR(dentry)) |
| 3199 | return ERR_CAST(dentry); |
| 3200 | |
Al Viro | 973d4b7 | 2020-01-26 10:48:16 -0500 | [diff] [blame] | 3201 | if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { |
Al Viro | e73cabf | 2020-01-09 14:30:08 -0500 | [diff] [blame] | 3202 | dput(nd->path.dentry); |
| 3203 | nd->path.dentry = dentry; |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3204 | return NULL; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3205 | } |
| 3206 | |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3207 | finish_lookup: |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 3208 | if (nd->depth) |
| 3209 | put_link(nd); |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 3210 | res = step_into(nd, WALK_TRAILING, dentry, inode, seq); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3211 | if (unlikely(res)) { |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3212 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3213 | return res; |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3214 | } |
Al Viro | 31d1726 | 2020-01-08 20:19:38 -0500 | [diff] [blame] | 3215 | |
| 3216 | if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) { |
| 3217 | audit_inode(nd->name, nd->path.dentry, 0); |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3218 | return ERR_PTR(-EEXIST); |
Al Viro | 31d1726 | 2020-01-08 20:19:38 -0500 | [diff] [blame] | 3219 | } |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3220 | |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 3221 | /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ |
Al Viro | c981a48 | 2020-01-26 11:06:21 -0500 | [diff] [blame] | 3222 | return ERR_PTR(complete_walk(nd)); |
| 3223 | } |
| 3224 | |
| 3225 | /* |
| 3226 | * Handle the last step of open() |
| 3227 | */ |
| 3228 | static const char *do_last(struct nameidata *nd, |
| 3229 | struct file *file, const struct open_flags *op) |
| 3230 | { |
| 3231 | kuid_t dir_uid = nd->inode->i_uid; |
| 3232 | umode_t dir_mode = nd->inode->i_mode; |
| 3233 | int open_flag = op->open_flag; |
| 3234 | bool do_truncate; |
| 3235 | int acc_mode; |
| 3236 | const char *link; |
| 3237 | int error; |
| 3238 | |
| 3239 | link = open_last_lookups(nd, file, op); |
| 3240 | if (unlikely(link)) |
| 3241 | return link; |
| 3242 | |
Al Viro | 973d4b7 | 2020-01-26 10:48:16 -0500 | [diff] [blame] | 3243 | if (!(file->f_mode & FMODE_CREATED)) |
| 3244 | audit_inode(nd->name, nd->path.dentry, 0); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3245 | if (open_flag & O_CREAT) { |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3246 | if (d_is_dir(nd->path.dentry)) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3247 | return ERR_PTR(-EISDIR); |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 3248 | error = may_create_in_sticky(dir_mode, dir_uid, |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3249 | d_backing_inode(nd->path.dentry)); |
| 3250 | if (unlikely(error)) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3251 | return ERR_PTR(error); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3252 | } |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 3253 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3254 | return ERR_PTR(-ENOTDIR); |
Al Viro | 6c0d46c | 2011-03-09 00:59:59 -0500 | [diff] [blame] | 3255 | |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3256 | do_truncate = false; |
| 3257 | acc_mode = op->acc_mode; |
Al Viro | 5a2d3ed | 2020-01-26 10:32:22 -0500 | [diff] [blame] | 3258 | if (file->f_mode & FMODE_CREATED) { |
| 3259 | /* Don't check for write permission, don't truncate */ |
| 3260 | open_flag &= ~O_TRUNC; |
Al Viro | 5a2d3ed | 2020-01-26 10:32:22 -0500 | [diff] [blame] | 3261 | acc_mode = 0; |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3262 | } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3263 | error = mnt_want_write(nd->path.mnt); |
| 3264 | if (error) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3265 | return ERR_PTR(error); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3266 | do_truncate = true; |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3267 | } |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3268 | error = may_open(&nd->path, acc_mode, open_flag); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3269 | if (!error && !(file->f_mode & FMODE_OPENED)) |
Al Viro | 3ad5615 | 2020-01-26 10:06:13 -0500 | [diff] [blame] | 3270 | error = vfs_open(&nd->path, file); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3271 | if (!error) |
| 3272 | error = ima_file_check(file, op->acc_mode); |
| 3273 | if (!error && do_truncate) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3274 | error = handle_truncate(file); |
Al Viro | c80567c | 2016-02-27 19:17:33 -0500 | [diff] [blame] | 3275 | if (unlikely(error > 0)) { |
| 3276 | WARN_ON(1); |
| 3277 | error = -EINVAL; |
| 3278 | } |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame] | 3279 | if (do_truncate) |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3280 | mnt_drop_write(nd->path.mnt); |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3281 | return ERR_PTR(error); |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3282 | } |
| 3283 | |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3284 | struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag) |
| 3285 | { |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3286 | struct dentry *child = NULL; |
| 3287 | struct inode *dir = dentry->d_inode; |
| 3288 | struct inode *inode; |
| 3289 | int error; |
| 3290 | |
| 3291 | /* we want directory to be writable */ |
| 3292 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 3293 | if (error) |
| 3294 | goto out_err; |
| 3295 | error = -EOPNOTSUPP; |
| 3296 | if (!dir->i_op->tmpfile) |
| 3297 | goto out_err; |
| 3298 | error = -ENOMEM; |
David Howells | cdf0122 | 2017-07-04 17:25:22 +0100 | [diff] [blame] | 3299 | child = d_alloc(dentry, &slash_name); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3300 | if (unlikely(!child)) |
| 3301 | goto out_err; |
| 3302 | error = dir->i_op->tmpfile(dir, child, mode); |
| 3303 | if (error) |
| 3304 | goto out_err; |
| 3305 | error = -ENOENT; |
| 3306 | inode = child->d_inode; |
| 3307 | if (unlikely(!inode)) |
| 3308 | goto out_err; |
| 3309 | if (!(open_flag & O_EXCL)) { |
| 3310 | spin_lock(&inode->i_lock); |
| 3311 | inode->i_state |= I_LINKABLE; |
| 3312 | spin_unlock(&inode->i_lock); |
| 3313 | } |
Mimi Zohar | fdb2410 | 2019-01-22 14:06:49 -0600 | [diff] [blame] | 3314 | ima_post_create_tmpfile(inode); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3315 | return child; |
| 3316 | |
| 3317 | out_err: |
| 3318 | dput(child); |
| 3319 | return ERR_PTR(error); |
| 3320 | } |
| 3321 | EXPORT_SYMBOL(vfs_tmpfile); |
| 3322 | |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3323 | static int do_tmpfile(struct nameidata *nd, unsigned flags, |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3324 | const struct open_flags *op, |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3325 | struct file *file) |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3326 | { |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3327 | struct dentry *child; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3328 | struct path path; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3329 | int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3330 | if (unlikely(error)) |
| 3331 | return error; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3332 | error = mnt_want_write(path.mnt); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3333 | if (unlikely(error)) |
| 3334 | goto out; |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3335 | child = vfs_tmpfile(path.dentry, op->mode, op->open_flag); |
| 3336 | error = PTR_ERR(child); |
Hirofumi Nakagawa | 684e73b | 2017-09-26 03:21:26 +0900 | [diff] [blame] | 3337 | if (IS_ERR(child)) |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3338 | goto out2; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3339 | dput(path.dentry); |
| 3340 | path.dentry = child; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3341 | audit_inode(nd->name, child, 0); |
Eric Rannaud | 69a91c2 | 2014-10-30 01:51:01 -0700 | [diff] [blame] | 3342 | /* Don't check for other permissions, the inode was just created */ |
Al Viro | 62fb4a1 | 2015-12-26 22:33:24 -0500 | [diff] [blame] | 3343 | error = may_open(&path, 0, op->open_flag); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3344 | if (error) |
| 3345 | goto out2; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3346 | file->f_path.mnt = path.mnt; |
Al Viro | be12af3 | 2018-06-08 11:44:56 -0400 | [diff] [blame] | 3347 | error = finish_open(file, child, NULL); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3348 | out2: |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3349 | mnt_drop_write(path.mnt); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3350 | out: |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3351 | path_put(&path); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3352 | return error; |
| 3353 | } |
| 3354 | |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3355 | static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) |
| 3356 | { |
| 3357 | struct path path; |
| 3358 | int error = path_lookupat(nd, flags, &path); |
| 3359 | if (!error) { |
| 3360 | audit_inode(nd->name, path.dentry, 0); |
Al Viro | ae2bb29 | 2018-07-10 13:22:28 -0400 | [diff] [blame] | 3361 | error = vfs_open(&path, file); |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3362 | path_put(&path); |
| 3363 | } |
| 3364 | return error; |
| 3365 | } |
| 3366 | |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3367 | static struct file *path_openat(struct nameidata *nd, |
| 3368 | const struct open_flags *op, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | { |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3370 | struct file *file; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3371 | int error; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3372 | |
Al Viro | ea73ea7 | 2018-07-11 15:00:04 -0400 | [diff] [blame] | 3373 | file = alloc_empty_file(op->open_flag, current_cred()); |
Al Viro | 1afc99b | 2013-02-14 20:41:04 -0500 | [diff] [blame] | 3374 | if (IS_ERR(file)) |
| 3375 | return file; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3376 | |
Al Viro | bb458c6 | 2013-07-13 13:26:37 +0400 | [diff] [blame] | 3377 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3378 | error = do_tmpfile(nd, flags, op, file); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3379 | } else if (unlikely(file->f_flags & O_PATH)) { |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3380 | error = do_o_path(nd, flags, file); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3381 | } else { |
| 3382 | const char *s = path_init(nd, flags); |
| 3383 | while (!(error = link_path_walk(s, nd)) && |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3384 | (s = do_last(nd, file, op)) != NULL) |
| 3385 | ; |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3386 | terminate_walk(nd); |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3387 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3388 | if (likely(!error)) { |
Al Viro | aad888f | 2018-06-08 12:58:04 -0400 | [diff] [blame] | 3389 | if (likely(file->f_mode & FMODE_OPENED)) |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3390 | return file; |
| 3391 | WARN_ON(1); |
| 3392 | error = -EINVAL; |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 3393 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3394 | fput(file); |
| 3395 | if (error == -EOPENSTALE) { |
| 3396 | if (flags & LOOKUP_RCU) |
| 3397 | error = -ECHILD; |
| 3398 | else |
| 3399 | error = -ESTALE; |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3400 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3401 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | } |
| 3403 | |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3404 | struct file *do_filp_open(int dfd, struct filename *pathname, |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3405 | const struct open_flags *op) |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3406 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3407 | struct nameidata nd; |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3408 | int flags = op->lookup_flags; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3409 | struct file *filp; |
| 3410 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3411 | set_nameidata(&nd, dfd, pathname); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3412 | filp = path_openat(&nd, op, flags | LOOKUP_RCU); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3413 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3414 | filp = path_openat(&nd, op, flags); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3415 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3416 | filp = path_openat(&nd, op, flags | LOOKUP_REVAL); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3417 | restore_nameidata(); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3418 | return filp; |
| 3419 | } |
| 3420 | |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3421 | struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3422 | const char *name, const struct open_flags *op) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3423 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3424 | struct nameidata nd; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3425 | struct file *file; |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3426 | struct filename *filename; |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3427 | int flags = op->lookup_flags | LOOKUP_ROOT; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3428 | |
| 3429 | nd.root.mnt = mnt; |
| 3430 | nd.root.dentry = dentry; |
| 3431 | |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3432 | if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3433 | return ERR_PTR(-ELOOP); |
| 3434 | |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3435 | filename = getname_kernel(name); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 3436 | if (IS_ERR(filename)) |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3437 | return ERR_CAST(filename); |
| 3438 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3439 | set_nameidata(&nd, -1, filename); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3440 | file = path_openat(&nd, op, flags | LOOKUP_RCU); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3441 | if (unlikely(file == ERR_PTR(-ECHILD))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3442 | file = path_openat(&nd, op, flags); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3443 | if (unlikely(file == ERR_PTR(-ESTALE))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3444 | file = path_openat(&nd, op, flags | LOOKUP_REVAL); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3445 | restore_nameidata(); |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3446 | putname(filename); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3447 | return file; |
| 3448 | } |
| 3449 | |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3450 | static struct dentry *filename_create(int dfd, struct filename *name, |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3451 | struct path *path, unsigned int lookup_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | { |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3453 | struct dentry *dentry = ERR_PTR(-EEXIST); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3454 | struct qstr last; |
| 3455 | int type; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3456 | int err2; |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3457 | int error; |
| 3458 | bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); |
| 3459 | |
| 3460 | /* |
| 3461 | * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any |
| 3462 | * other flags passed in are ignored! |
| 3463 | */ |
| 3464 | lookup_flags &= LOOKUP_REVAL; |
| 3465 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 3466 | name = filename_parentat(dfd, name, lookup_flags, path, &last, &type); |
| 3467 | if (IS_ERR(name)) |
| 3468 | return ERR_CAST(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3470 | /* |
| 3471 | * Yucky last component or no last component at all? |
| 3472 | * (foo/., foo/.., /////) |
| 3473 | */ |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 3474 | if (unlikely(type != LAST_NORM)) |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3475 | goto out; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3476 | |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3477 | /* don't fail immediately if it's r/o, at least try to report other errors */ |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3478 | err2 = mnt_want_write(path->mnt); |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3479 | /* |
| 3480 | * Do the final lookup. |
| 3481 | */ |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3482 | lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3483 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3484 | dentry = __lookup_hash(&last, path->dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | if (IS_ERR(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3486 | goto unlock; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3487 | |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3488 | error = -EEXIST; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3489 | if (d_is_positive(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3490 | goto fail; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3491 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3492 | /* |
| 3493 | * Special case - lookup gave negative, but... we had foo/bar/ |
| 3494 | * From the vfs_mknod() POV we just have a negative dentry - |
| 3495 | * all is fine. Let's be bastards - you had / on the end, you've |
| 3496 | * been asking for (non-existent) directory. -ENOENT for you. |
| 3497 | */ |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3498 | if (unlikely(!is_dir && last.name[last.len])) { |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3499 | error = -ENOENT; |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3500 | goto fail; |
Al Viro | e9baf6e | 2008-05-15 04:49:12 -0400 | [diff] [blame] | 3501 | } |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3502 | if (unlikely(err2)) { |
| 3503 | error = err2; |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3504 | goto fail; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3505 | } |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3506 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | fail: |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3509 | dput(dentry); |
| 3510 | dentry = ERR_PTR(error); |
| 3511 | unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3512 | inode_unlock(path->dentry->d_inode); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3513 | if (!err2) |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3514 | mnt_drop_write(path->mnt); |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3515 | out: |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3516 | path_put(path); |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3517 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | return dentry; |
| 3519 | } |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3520 | |
| 3521 | struct dentry *kern_path_create(int dfd, const char *pathname, |
| 3522 | struct path *path, unsigned int lookup_flags) |
| 3523 | { |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3524 | return filename_create(dfd, getname_kernel(pathname), |
| 3525 | path, lookup_flags); |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3526 | } |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3527 | EXPORT_SYMBOL(kern_path_create); |
| 3528 | |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3529 | void done_path_create(struct path *path, struct dentry *dentry) |
| 3530 | { |
| 3531 | dput(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3532 | inode_unlock(path->dentry->d_inode); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3533 | mnt_drop_write(path->mnt); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3534 | path_put(path); |
| 3535 | } |
| 3536 | EXPORT_SYMBOL(done_path_create); |
| 3537 | |
Al Viro | 520ae68 | 2015-05-13 07:00:28 -0400 | [diff] [blame] | 3538 | inline struct dentry *user_path_create(int dfd, const char __user *pathname, |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3539 | struct path *path, unsigned int lookup_flags) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3540 | { |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3541 | return filename_create(dfd, getname(pathname), path, lookup_flags); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3542 | } |
| 3543 | EXPORT_SYMBOL(user_path_create); |
| 3544 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 3545 | int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3547 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | |
| 3549 | if (error) |
| 3550 | return error; |
| 3551 | |
Christian Brauner | 94f8200 | 2018-07-05 17:51:20 +0200 | [diff] [blame] | 3552 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | return -EPERM; |
| 3554 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3555 | if (!dir->i_op->mknod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3556 | return -EPERM; |
| 3557 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 3558 | error = devcgroup_inode_mknod(mode, dev); |
| 3559 | if (error) |
| 3560 | return error; |
| 3561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | error = security_inode_mknod(dir, dentry, mode, dev); |
| 3563 | if (error) |
| 3564 | return error; |
| 3565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | error = dir->i_op->mknod(dir, dentry, mode, dev); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3567 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3568 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3569 | return error; |
| 3570 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3571 | EXPORT_SYMBOL(vfs_mknod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | |
Al Viro | f69aac0 | 2011-07-26 04:31:40 -0400 | [diff] [blame] | 3573 | static int may_mknod(umode_t mode) |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3574 | { |
| 3575 | switch (mode & S_IFMT) { |
| 3576 | case S_IFREG: |
| 3577 | case S_IFCHR: |
| 3578 | case S_IFBLK: |
| 3579 | case S_IFIFO: |
| 3580 | case S_IFSOCK: |
| 3581 | case 0: /* zero mode translates to S_IFREG */ |
| 3582 | return 0; |
| 3583 | case S_IFDIR: |
| 3584 | return -EPERM; |
| 3585 | default: |
| 3586 | return -EINVAL; |
| 3587 | } |
| 3588 | } |
| 3589 | |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3590 | long do_mknodat(int dfd, const char __user *filename, umode_t mode, |
| 3591 | unsigned int dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3593 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3594 | struct path path; |
| 3595 | int error; |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3596 | unsigned int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3597 | |
Al Viro | 8e4bfca | 2012-07-20 01:17:26 +0400 | [diff] [blame] | 3598 | error = may_mknod(mode); |
| 3599 | if (error) |
| 3600 | return error; |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3601 | retry: |
| 3602 | dentry = user_path_create(dfd, filename, &path, lookup_flags); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3603 | if (IS_ERR(dentry)) |
| 3604 | return PTR_ERR(dentry); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3605 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3606 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3607 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3608 | error = security_path_mknod(&path, dentry, mode, dev); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3609 | if (error) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3610 | goto out; |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3611 | switch (mode & S_IFMT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | case 0: case S_IFREG: |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 3613 | error = vfs_create(path.dentry->d_inode,dentry,mode,true); |
Mimi Zohar | 05d1a71 | 2016-02-29 19:52:05 -0500 | [diff] [blame] | 3614 | if (!error) |
| 3615 | ima_post_path_mknod(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | break; |
| 3617 | case S_IFCHR: case S_IFBLK: |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3618 | error = vfs_mknod(path.dentry->d_inode,dentry,mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | new_decode_dev(dev)); |
| 3620 | break; |
| 3621 | case S_IFIFO: case S_IFSOCK: |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3622 | error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | } |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3625 | out: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3626 | done_path_create(&path, dentry); |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3627 | if (retry_estale(error, lookup_flags)) { |
| 3628 | lookup_flags |= LOOKUP_REVAL; |
| 3629 | goto retry; |
| 3630 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | return error; |
| 3632 | } |
| 3633 | |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3634 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
| 3635 | unsigned int, dev) |
| 3636 | { |
| 3637 | return do_mknodat(dfd, filename, mode, dev); |
| 3638 | } |
| 3639 | |
Al Viro | 8208a22 | 2011-07-25 17:32:17 -0400 | [diff] [blame] | 3640 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3641 | { |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3642 | return do_mknodat(AT_FDCWD, filename, mode, dev); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3643 | } |
| 3644 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 3645 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3647 | int error = may_create(dir, dentry); |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3648 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | |
| 3650 | if (error) |
| 3651 | return error; |
| 3652 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3653 | if (!dir->i_op->mkdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | return -EPERM; |
| 3655 | |
| 3656 | mode &= (S_IRWXUGO|S_ISVTX); |
| 3657 | error = security_inode_mkdir(dir, dentry, mode); |
| 3658 | if (error) |
| 3659 | return error; |
| 3660 | |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3661 | if (max_links && dir->i_nlink >= max_links) |
| 3662 | return -EMLINK; |
| 3663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | error = dir->i_op->mkdir(dir, dentry, mode); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3665 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3666 | fsnotify_mkdir(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | return error; |
| 3668 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3669 | EXPORT_SYMBOL(vfs_mkdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3671 | long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | { |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3673 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3674 | struct path path; |
| 3675 | int error; |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3676 | unsigned int lookup_flags = LOOKUP_DIRECTORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3678 | retry: |
| 3679 | dentry = user_path_create(dfd, pathname, &path, lookup_flags); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3680 | if (IS_ERR(dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3681 | return PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3682 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3683 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3684 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3685 | error = security_path_mkdir(&path, dentry, mode); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3686 | if (!error) |
| 3687 | error = vfs_mkdir(path.dentry->d_inode, dentry, mode); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3688 | done_path_create(&path, dentry); |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3689 | if (retry_estale(error, lookup_flags)) { |
| 3690 | lookup_flags |= LOOKUP_REVAL; |
| 3691 | goto retry; |
| 3692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | return error; |
| 3694 | } |
| 3695 | |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3696 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
| 3697 | { |
| 3698 | return do_mkdirat(dfd, pathname, mode); |
| 3699 | } |
| 3700 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 3701 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3702 | { |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3703 | return do_mkdirat(AT_FDCWD, pathname, mode); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3704 | } |
| 3705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3707 | { |
| 3708 | int error = may_delete(dir, dentry, 1); |
| 3709 | |
| 3710 | if (error) |
| 3711 | return error; |
| 3712 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3713 | if (!dir->i_op->rmdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | return -EPERM; |
| 3715 | |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3716 | dget(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3717 | inode_lock(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3719 | error = -EBUSY; |
Eric W. Biederman | 7af1364 | 2013-10-04 19:15:13 -0700 | [diff] [blame] | 3720 | if (is_local_mountpoint(dentry)) |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3721 | goto out; |
| 3722 | |
| 3723 | error = security_inode_rmdir(dir, dentry); |
| 3724 | if (error) |
| 3725 | goto out; |
| 3726 | |
| 3727 | error = dir->i_op->rmdir(dir, dentry); |
| 3728 | if (error) |
| 3729 | goto out; |
| 3730 | |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 3731 | shrink_dcache_parent(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3732 | dentry->d_inode->i_flags |= S_DEAD; |
| 3733 | dont_mount(dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3734 | detach_mounts(dentry); |
Amir Goldstein | 116b973 | 2019-05-26 17:34:02 +0300 | [diff] [blame] | 3735 | fsnotify_rmdir(dir, dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3736 | |
| 3737 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3738 | inode_unlock(dentry->d_inode); |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3739 | dput(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3740 | if (!error) |
| 3741 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | return error; |
| 3743 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3744 | EXPORT_SYMBOL(vfs_rmdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | |
Dominik Brodowski | f459dffa | 2018-03-11 11:34:48 +0100 | [diff] [blame] | 3746 | long do_rmdir(int dfd, const char __user *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3747 | { |
| 3748 | int error = 0; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3749 | struct filename *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | struct dentry *dentry; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3751 | struct path path; |
| 3752 | struct qstr last; |
| 3753 | int type; |
Jeff Layton | c6ee920 | 2012-12-20 16:28:33 -0500 | [diff] [blame] | 3754 | unsigned int lookup_flags = 0; |
| 3755 | retry: |
Al Viro | c1d4dd2 | 2016-06-05 16:38:18 -0400 | [diff] [blame] | 3756 | name = filename_parentat(dfd, getname(pathname), lookup_flags, |
| 3757 | &path, &last, &type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3758 | if (IS_ERR(name)) |
| 3759 | return PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3761 | switch (type) { |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3762 | case LAST_DOTDOT: |
| 3763 | error = -ENOTEMPTY; |
| 3764 | goto exit1; |
| 3765 | case LAST_DOT: |
| 3766 | error = -EINVAL; |
| 3767 | goto exit1; |
| 3768 | case LAST_ROOT: |
| 3769 | error = -EBUSY; |
| 3770 | goto exit1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3771 | } |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3772 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3773 | error = mnt_want_write(path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3774 | if (error) |
| 3775 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3776 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3777 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3778 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3779 | error = PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3780 | if (IS_ERR(dentry)) |
| 3781 | goto exit2; |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 3782 | if (!dentry->d_inode) { |
| 3783 | error = -ENOENT; |
| 3784 | goto exit3; |
| 3785 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3786 | error = security_path_rmdir(&path, dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3787 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3788 | goto exit3; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3789 | error = vfs_rmdir(path.dentry->d_inode, dentry); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 3790 | exit3: |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3791 | dput(dentry); |
| 3792 | exit2: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3793 | inode_unlock(path.dentry->d_inode); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3794 | mnt_drop_write(path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3796 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3797 | putname(name); |
Jeff Layton | c6ee920 | 2012-12-20 16:28:33 -0500 | [diff] [blame] | 3798 | if (retry_estale(error, lookup_flags)) { |
| 3799 | lookup_flags |= LOOKUP_REVAL; |
| 3800 | goto retry; |
| 3801 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3802 | return error; |
| 3803 | } |
| 3804 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 3805 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3806 | { |
| 3807 | return do_rmdir(AT_FDCWD, pathname); |
| 3808 | } |
| 3809 | |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3810 | /** |
| 3811 | * vfs_unlink - unlink a filesystem object |
| 3812 | * @dir: parent directory |
| 3813 | * @dentry: victim |
| 3814 | * @delegated_inode: returns victim inode, if the inode is delegated. |
| 3815 | * |
| 3816 | * The caller must hold dir->i_mutex. |
| 3817 | * |
| 3818 | * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and |
| 3819 | * return a reference to the inode in delegated_inode. The caller |
| 3820 | * should then break the delegation on that inode and retry. Because |
| 3821 | * breaking a delegation may take a long time, the caller should drop |
| 3822 | * dir->i_mutex before doing so. |
| 3823 | * |
| 3824 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 3825 | * be appropriate for callers that expect the underlying filesystem not |
| 3826 | * to be NFS exported. |
| 3827 | */ |
| 3828 | int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | { |
J. Bruce Fields | 9accbb9 | 2012-08-28 07:03:24 -0400 | [diff] [blame] | 3830 | struct inode *target = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | int error = may_delete(dir, dentry, 0); |
| 3832 | |
| 3833 | if (error) |
| 3834 | return error; |
| 3835 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3836 | if (!dir->i_op->unlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | return -EPERM; |
| 3838 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3839 | inode_lock(target); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3840 | if (is_local_mountpoint(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3841 | error = -EBUSY; |
| 3842 | else { |
| 3843 | error = security_inode_unlink(dir, dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3844 | if (!error) { |
J. Bruce Fields | 5a14696 | 2012-08-28 07:50:40 -0700 | [diff] [blame] | 3845 | error = try_break_deleg(target, delegated_inode); |
| 3846 | if (error) |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3847 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | error = dir->i_op->unlink(dir, dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3849 | if (!error) { |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 3850 | dont_mount(dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3851 | detach_mounts(dentry); |
Amir Goldstein | 116b973 | 2019-05-26 17:34:02 +0300 | [diff] [blame] | 3852 | fsnotify_unlink(dir, dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3853 | } |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | } |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3856 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3857 | inode_unlock(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | |
| 3859 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ |
| 3860 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { |
J. Bruce Fields | 9accbb9 | 2012-08-28 07:03:24 -0400 | [diff] [blame] | 3861 | fsnotify_link_count(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3863 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 3864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | return error; |
| 3866 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3867 | EXPORT_SYMBOL(vfs_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | |
| 3869 | /* |
| 3870 | * Make sure that the actual truncation of the file will occur outside its |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3871 | * directory's i_mutex. Truncate can take a long time if there is a lot of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | * writeout happening, and we don't want to prevent access to the directory |
| 3873 | * while waiting on the I/O. |
| 3874 | */ |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3875 | long do_unlinkat(int dfd, struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3876 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3877 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3878 | struct dentry *dentry; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3879 | struct path path; |
| 3880 | struct qstr last; |
| 3881 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | struct inode *inode = NULL; |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3883 | struct inode *delegated_inode = NULL; |
Jeff Layton | 5d18f81 | 2012-12-20 16:38:04 -0500 | [diff] [blame] | 3884 | unsigned int lookup_flags = 0; |
| 3885 | retry: |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3886 | name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3887 | if (IS_ERR(name)) |
| 3888 | return PTR_ERR(name); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3890 | error = -EISDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3891 | if (type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3892 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3893 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3894 | error = mnt_want_write(path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3895 | if (error) |
| 3896 | goto exit1; |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3897 | retry_deleg: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3898 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3899 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | error = PTR_ERR(dentry); |
| 3901 | if (!IS_ERR(dentry)) { |
| 3902 | /* Why not before? Because we want correct error value */ |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3903 | if (last.name[last.len]) |
Török Edwin | 50338b8 | 2011-06-16 00:06:14 +0300 | [diff] [blame] | 3904 | goto slashes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3905 | inode = dentry->d_inode; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3906 | if (d_is_negative(dentry)) |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 3907 | goto slashes; |
| 3908 | ihold(inode); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3909 | error = security_path_unlink(&path, dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3910 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3911 | goto exit2; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3912 | error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3913 | exit2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | dput(dentry); |
| 3915 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3916 | inode_unlock(path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3917 | if (inode) |
| 3918 | iput(inode); /* truncate the inode here */ |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3919 | inode = NULL; |
| 3920 | if (delegated_inode) { |
J. Bruce Fields | 5a14696 | 2012-08-28 07:50:40 -0700 | [diff] [blame] | 3921 | error = break_deleg_wait(&delegated_inode); |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3922 | if (!error) |
| 3923 | goto retry_deleg; |
| 3924 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3925 | mnt_drop_write(path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3927 | path_put(&path); |
Jeff Layton | 5d18f81 | 2012-12-20 16:38:04 -0500 | [diff] [blame] | 3928 | if (retry_estale(error, lookup_flags)) { |
| 3929 | lookup_flags |= LOOKUP_REVAL; |
| 3930 | inode = NULL; |
| 3931 | goto retry; |
| 3932 | } |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3933 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | return error; |
| 3935 | |
| 3936 | slashes: |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3937 | if (d_is_negative(dentry)) |
| 3938 | error = -ENOENT; |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 3939 | else if (d_is_dir(dentry)) |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3940 | error = -EISDIR; |
| 3941 | else |
| 3942 | error = -ENOTDIR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3943 | goto exit2; |
| 3944 | } |
| 3945 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3946 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3947 | { |
| 3948 | if ((flag & ~AT_REMOVEDIR) != 0) |
| 3949 | return -EINVAL; |
| 3950 | |
| 3951 | if (flag & AT_REMOVEDIR) |
| 3952 | return do_rmdir(dfd, pathname); |
| 3953 | |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3954 | return do_unlinkat(dfd, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3955 | } |
| 3956 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 3957 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3958 | { |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3959 | return do_unlinkat(AT_FDCWD, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3960 | } |
| 3961 | |
Miklos Szeredi | db2e747 | 2008-06-24 16:50:16 +0200 | [diff] [blame] | 3962 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3964 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | |
| 3966 | if (error) |
| 3967 | return error; |
| 3968 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3969 | if (!dir->i_op->symlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3970 | return -EPERM; |
| 3971 | |
| 3972 | error = security_inode_symlink(dir, dentry, oldname); |
| 3973 | if (error) |
| 3974 | return error; |
| 3975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | error = dir->i_op->symlink(dir, dentry, oldname); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3977 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3978 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | return error; |
| 3980 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3981 | EXPORT_SYMBOL(vfs_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3982 | |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3983 | long do_symlinkat(const char __user *oldname, int newdfd, |
| 3984 | const char __user *newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3985 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3986 | int error; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3987 | struct filename *from; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3988 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3989 | struct path path; |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 3990 | unsigned int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | |
| 3992 | from = getname(oldname); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3993 | if (IS_ERR(from)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3994 | return PTR_ERR(from); |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 3995 | retry: |
| 3996 | dentry = user_path_create(newdfd, newname, &path, lookup_flags); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3997 | error = PTR_ERR(dentry); |
| 3998 | if (IS_ERR(dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3999 | goto out_putname; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4000 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4001 | error = security_path_symlink(&path, dentry, from->name); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 4002 | if (!error) |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4003 | error = vfs_symlink(path.dentry->d_inode, dentry, from->name); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 4004 | done_path_create(&path, dentry); |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 4005 | if (retry_estale(error, lookup_flags)) { |
| 4006 | lookup_flags |= LOOKUP_REVAL; |
| 4007 | goto retry; |
| 4008 | } |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4009 | out_putname: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | putname(from); |
| 4011 | return error; |
| 4012 | } |
| 4013 | |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4014 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
| 4015 | int, newdfd, const char __user *, newname) |
| 4016 | { |
| 4017 | return do_symlinkat(oldname, newdfd, newname); |
| 4018 | } |
| 4019 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4020 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4021 | { |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4022 | return do_symlinkat(oldname, AT_FDCWD, newname); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4023 | } |
| 4024 | |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4025 | /** |
| 4026 | * vfs_link - create a new link |
| 4027 | * @old_dentry: object to be linked |
| 4028 | * @dir: new parent |
| 4029 | * @new_dentry: where to create the new link |
| 4030 | * @delegated_inode: returns inode needing a delegation break |
| 4031 | * |
| 4032 | * The caller must hold dir->i_mutex |
| 4033 | * |
| 4034 | * If vfs_link discovers a delegation on the to-be-linked file in need |
| 4035 | * of breaking, it will return -EWOULDBLOCK and return a reference to the |
| 4036 | * inode in delegated_inode. The caller should then break the delegation |
| 4037 | * and retry. Because breaking a delegation may take a long time, the |
| 4038 | * caller should drop the i_mutex before doing so. |
| 4039 | * |
| 4040 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4041 | * be appropriate for callers that expect the underlying filesystem not |
| 4042 | * to be NFS exported. |
| 4043 | */ |
| 4044 | int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | { |
| 4046 | struct inode *inode = old_dentry->d_inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 4047 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4048 | int error; |
| 4049 | |
| 4050 | if (!inode) |
| 4051 | return -ENOENT; |
| 4052 | |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 4053 | error = may_create(dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | if (error) |
| 4055 | return error; |
| 4056 | |
| 4057 | if (dir->i_sb != inode->i_sb) |
| 4058 | return -EXDEV; |
| 4059 | |
| 4060 | /* |
| 4061 | * A link to an append-only or immutable file cannot be created. |
| 4062 | */ |
| 4063 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 4064 | return -EPERM; |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 4065 | /* |
| 4066 | * Updating the link count will likely cause i_uid and i_gid to |
| 4067 | * be writen back improperly if their true value is unknown to |
| 4068 | * the vfs. |
| 4069 | */ |
| 4070 | if (HAS_UNMAPPED_ID(inode)) |
| 4071 | return -EPERM; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4072 | if (!dir->i_op->link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4073 | return -EPERM; |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 4074 | if (S_ISDIR(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | return -EPERM; |
| 4076 | |
| 4077 | error = security_inode_link(old_dentry, dir, new_dentry); |
| 4078 | if (error) |
| 4079 | return error; |
| 4080 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4081 | inode_lock(inode); |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 4082 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
Al Viro | f4e0c30 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 4083 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 4084 | error = -ENOENT; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 4085 | else if (max_links && inode->i_nlink >= max_links) |
| 4086 | error = -EMLINK; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4087 | else { |
| 4088 | error = try_break_deleg(inode, delegated_inode); |
| 4089 | if (!error) |
| 4090 | error = dir->i_op->link(old_dentry, dir, new_dentry); |
| 4091 | } |
Al Viro | f4e0c30 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 4092 | |
| 4093 | if (!error && (inode->i_state & I_LINKABLE)) { |
| 4094 | spin_lock(&inode->i_lock); |
| 4095 | inode->i_state &= ~I_LINKABLE; |
| 4096 | spin_unlock(&inode->i_lock); |
| 4097 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4098 | inode_unlock(inode); |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 4099 | if (!error) |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 4100 | fsnotify_link(dir, inode, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | return error; |
| 4102 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4103 | EXPORT_SYMBOL(vfs_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 | |
| 4105 | /* |
| 4106 | * Hardlinks are often used in delicate situations. We avoid |
| 4107 | * security-related surprises by not following symlinks on the |
| 4108 | * newname. --KAB |
| 4109 | * |
| 4110 | * We don't follow them on the oldname either to be compatible |
| 4111 | * with linux 2.0, and to avoid hard-linking to directories |
| 4112 | * and other special files. --ADM |
| 4113 | */ |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4114 | int do_linkat(int olddfd, const char __user *oldname, int newdfd, |
| 4115 | const char __user *newname, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | { |
| 4117 | struct dentry *new_dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4118 | struct path old_path, new_path; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4119 | struct inode *delegated_inode = NULL; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4120 | int how = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4123 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 4124 | return -EINVAL; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4125 | /* |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4126 | * To use null names we require CAP_DAC_READ_SEARCH |
| 4127 | * This ensures that not everyone will be able to create |
| 4128 | * handlink using the passed filedescriptor. |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4129 | */ |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4130 | if (flags & AT_EMPTY_PATH) { |
| 4131 | if (!capable(CAP_DAC_READ_SEARCH)) |
| 4132 | return -ENOENT; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4133 | how = LOOKUP_EMPTY; |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4134 | } |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 4135 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4136 | if (flags & AT_SYMLINK_FOLLOW) |
| 4137 | how |= LOOKUP_FOLLOW; |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4138 | retry: |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4139 | error = user_path_at(olddfd, oldname, how, &old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | if (error) |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4141 | return error; |
| 4142 | |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4143 | new_dentry = user_path_create(newdfd, newname, &new_path, |
| 4144 | (how & LOOKUP_REVAL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | error = PTR_ERR(new_dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4146 | if (IS_ERR(new_dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4147 | goto out; |
| 4148 | |
| 4149 | error = -EXDEV; |
| 4150 | if (old_path.mnt != new_path.mnt) |
| 4151 | goto out_dput; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 4152 | error = may_linkat(&old_path); |
| 4153 | if (unlikely(error)) |
| 4154 | goto out_dput; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4155 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4156 | if (error) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 4157 | goto out_dput; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4158 | error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode); |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 4159 | out_dput: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 4160 | done_path_create(&new_path, new_dentry); |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4161 | if (delegated_inode) { |
| 4162 | error = break_deleg_wait(&delegated_inode); |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4163 | if (!error) { |
| 4164 | path_put(&old_path); |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4165 | goto retry; |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4166 | } |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4167 | } |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4168 | if (retry_estale(error, how)) { |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4169 | path_put(&old_path); |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4170 | how |= LOOKUP_REVAL; |
| 4171 | goto retry; |
| 4172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4173 | out: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 4174 | path_put(&old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4175 | |
| 4176 | return error; |
| 4177 | } |
| 4178 | |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4179 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
| 4180 | int, newdfd, const char __user *, newname, int, flags) |
| 4181 | { |
| 4182 | return do_linkat(olddfd, oldname, newdfd, newname, flags); |
| 4183 | } |
| 4184 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4185 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4186 | { |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4187 | return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4188 | } |
| 4189 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4190 | /** |
| 4191 | * vfs_rename - rename a filesystem object |
| 4192 | * @old_dir: parent of source |
| 4193 | * @old_dentry: source |
| 4194 | * @new_dir: parent of destination |
| 4195 | * @new_dentry: destination |
| 4196 | * @delegated_inode: returns an inode needing a delegation break |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4197 | * @flags: rename flags |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4198 | * |
| 4199 | * The caller must hold multiple mutexes--see lock_rename()). |
| 4200 | * |
| 4201 | * If vfs_rename discovers a delegation in need of breaking at either |
| 4202 | * the source or destination, it will return -EWOULDBLOCK and return a |
| 4203 | * reference to the inode in delegated_inode. The caller should then |
| 4204 | * break the delegation and retry. Because breaking a delegation may |
| 4205 | * take a long time, the caller should drop all locks before doing |
| 4206 | * so. |
| 4207 | * |
| 4208 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4209 | * be appropriate for callers that expect the underlying filesystem not |
| 4210 | * to be NFS exported. |
| 4211 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | * The worst of all namespace operations - renaming directory. "Perverted" |
| 4213 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... |
| 4214 | * Problems: |
Mauro Carvalho Chehab | 0117d42 | 2017-05-12 07:45:42 -0300 | [diff] [blame] | 4215 | * |
J. Bruce Fields | d03b29a | 2014-02-17 16:52:33 -0500 | [diff] [blame] | 4216 | * a) we can get into loop creation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | * b) race potential - two innocent renames can create a loop together. |
| 4218 | * That's where 4.4 screws up. Current fix: serialization on |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4219 | * sb->s_vfs_rename_mutex. We might be more accurate, but that's another |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | * story. |
J. Bruce Fields | 6cedba8 | 2012-03-05 11:40:41 -0500 | [diff] [blame] | 4221 | * c) we have to lock _four_ objects - parents and victim (if it exists), |
| 4222 | * and source (if it is not a directory). |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4223 | * And that - after we got ->i_mutex on parents (until then we don't know |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | * whether the target exists). Solution: try to be smart with locking |
| 4225 | * order for inodes. We rely on the fact that tree topology may change |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4226 | * only under ->s_vfs_rename_mutex _and_ that parent of the object we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | * move will be locked. Thus we can rank directories by the tree |
| 4228 | * (ancestors first) and rank all non-directories after them. |
| 4229 | * That works since everybody except rename does "lock parent, lookup, |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4230 | * lock child" and rename is under ->s_vfs_rename_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
| 4232 | * has no more than 1 dentry. If "hybrid" objects will ever appear, |
| 4233 | * we'd better make sure that there's no link(2) for them. |
Sage Weil | e4eaac0 | 2011-05-24 13:06:07 -0700 | [diff] [blame] | 4234 | * d) conversion from fhandle to dentry may come in the wrong moment - when |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4235 | * we are removing the target. Solution: we will have to grab ->i_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 4237 | * ->i_mutex on parents, which works but leads to some truly excessive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | * locking]. |
| 4239 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4241 | struct inode *new_dir, struct dentry *new_dentry, |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4242 | struct inode **delegated_inode, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | { |
| 4244 | int error; |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4245 | bool is_dir = d_is_dir(old_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4246 | struct inode *source = old_dentry->d_inode; |
| 4247 | struct inode *target = new_dentry->d_inode; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4248 | bool new_is_dir = false; |
| 4249 | unsigned max_links = new_dir->i_sb->s_max_links; |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4250 | struct name_snapshot old_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | |
Miklos Szeredi | 8d3e293 | 2016-12-16 11:02:54 +0100 | [diff] [blame] | 4252 | if (source == target) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4253 | return 0; |
| 4254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | error = may_delete(old_dir, old_dentry, is_dir); |
| 4256 | if (error) |
| 4257 | return error; |
| 4258 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4259 | if (!target) { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 4260 | error = may_create(new_dir, new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4261 | } else { |
| 4262 | new_is_dir = d_is_dir(new_dentry); |
| 4263 | |
| 4264 | if (!(flags & RENAME_EXCHANGE)) |
| 4265 | error = may_delete(new_dir, new_dentry, is_dir); |
| 4266 | else |
| 4267 | error = may_delete(new_dir, new_dentry, new_is_dir); |
| 4268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | if (error) |
| 4270 | return error; |
| 4271 | |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4272 | if (!old_dir->i_op->rename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | return -EPERM; |
| 4274 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4275 | /* |
| 4276 | * If we are going to change the parent - check write permissions, |
| 4277 | * we'll need to flip '..'. |
| 4278 | */ |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4279 | if (new_dir != old_dir) { |
| 4280 | if (is_dir) { |
| 4281 | error = inode_permission(source, MAY_WRITE); |
| 4282 | if (error) |
| 4283 | return error; |
| 4284 | } |
| 4285 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { |
| 4286 | error = inode_permission(target, MAY_WRITE); |
| 4287 | if (error) |
| 4288 | return error; |
| 4289 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4290 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4291 | |
Miklos Szeredi | 0b3974e | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4292 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, |
| 4293 | flags); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4294 | if (error) |
| 4295 | return error; |
| 4296 | |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4297 | take_dentry_name_snapshot(&old_name, old_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4298 | dget(new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4299 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4300 | lock_two_nondirectories(source, target); |
| 4301 | else if (target) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4302 | inode_lock(target); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4303 | |
| 4304 | error = -EBUSY; |
Eric W. Biederman | 7af1364 | 2013-10-04 19:15:13 -0700 | [diff] [blame] | 4305 | if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4306 | goto out; |
| 4307 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4308 | if (max_links && new_dir != old_dir) { |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4309 | error = -EMLINK; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4310 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4311 | goto out; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4312 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && |
| 4313 | old_dir->i_nlink >= max_links) |
| 4314 | goto out; |
| 4315 | } |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4316 | if (!is_dir) { |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4317 | error = try_break_deleg(source, delegated_inode); |
| 4318 | if (error) |
| 4319 | goto out; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4320 | } |
| 4321 | if (target && !new_is_dir) { |
| 4322 | error = try_break_deleg(target, delegated_inode); |
| 4323 | if (error) |
| 4324 | goto out; |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4325 | } |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4326 | error = old_dir->i_op->rename(old_dir, old_dentry, |
Miklos Szeredi | 18fc84d | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4327 | new_dir, new_dentry, flags); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4328 | if (error) |
| 4329 | goto out; |
| 4330 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4331 | if (!(flags & RENAME_EXCHANGE) && target) { |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4332 | if (is_dir) { |
| 4333 | shrink_dcache_parent(new_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4334 | target->i_flags |= S_DEAD; |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4335 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4336 | dont_mount(new_dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4337 | detach_mounts(new_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4338 | } |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4339 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { |
| 4340 | if (!(flags & RENAME_EXCHANGE)) |
| 4341 | d_move(old_dentry, new_dentry); |
| 4342 | else |
| 4343 | d_exchange(old_dentry, new_dentry); |
| 4344 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4345 | out: |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4346 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4347 | unlock_two_nondirectories(source, target); |
| 4348 | else if (target) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4349 | inode_unlock(target); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4350 | dput(new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4351 | if (!error) { |
Al Viro | f4ec3a3 | 2019-04-26 13:21:24 -0400 | [diff] [blame] | 4352 | fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4353 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); |
| 4354 | if (flags & RENAME_EXCHANGE) { |
Al Viro | f4ec3a3 | 2019-04-26 13:21:24 -0400 | [diff] [blame] | 4355 | fsnotify_move(new_dir, old_dir, &old_dentry->d_name, |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4356 | new_is_dir, NULL, new_dentry); |
| 4357 | } |
| 4358 | } |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4359 | release_dentry_name_snapshot(&old_name); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | return error; |
| 4362 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4363 | EXPORT_SYMBOL(vfs_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4365 | static int do_renameat2(int olddfd, const char __user *oldname, int newdfd, |
| 4366 | const char __user *newname, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4368 | struct dentry *old_dentry, *new_dentry; |
| 4369 | struct dentry *trap; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4370 | struct path old_path, new_path; |
| 4371 | struct qstr old_last, new_last; |
| 4372 | int old_type, new_type; |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4373 | struct inode *delegated_inode = NULL; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4374 | struct filename *from; |
| 4375 | struct filename *to; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4376 | unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4377 | bool should_retry = false; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4378 | int error; |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4379 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4380 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4381 | return -EINVAL; |
| 4382 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4383 | if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && |
| 4384 | (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4385 | return -EINVAL; |
| 4386 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4387 | if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD)) |
| 4388 | return -EPERM; |
| 4389 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4390 | if (flags & RENAME_EXCHANGE) |
| 4391 | target_flags = 0; |
| 4392 | |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4393 | retry: |
Al Viro | c1d4dd2 | 2016-06-05 16:38:18 -0400 | [diff] [blame] | 4394 | from = filename_parentat(olddfd, getname(oldname), lookup_flags, |
| 4395 | &old_path, &old_last, &old_type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4396 | if (IS_ERR(from)) { |
| 4397 | error = PTR_ERR(from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | goto exit; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | |
Al Viro | c1d4dd2 | 2016-06-05 16:38:18 -0400 | [diff] [blame] | 4401 | to = filename_parentat(newdfd, getname(newname), lookup_flags, |
| 4402 | &new_path, &new_last, &new_type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4403 | if (IS_ERR(to)) { |
| 4404 | error = PTR_ERR(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | goto exit1; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | |
| 4408 | error = -EXDEV; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4409 | if (old_path.mnt != new_path.mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | goto exit2; |
| 4411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | error = -EBUSY; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4413 | if (old_type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | goto exit2; |
| 4415 | |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4416 | if (flags & RENAME_NOREPLACE) |
| 4417 | error = -EEXIST; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4418 | if (new_type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | goto exit2; |
| 4420 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4421 | error = mnt_want_write(old_path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4422 | if (error) |
| 4423 | goto exit2; |
| 4424 | |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4425 | retry_deleg: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4426 | trap = lock_rename(new_path.dentry, old_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4428 | old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | error = PTR_ERR(old_dentry); |
| 4430 | if (IS_ERR(old_dentry)) |
| 4431 | goto exit3; |
| 4432 | /* source must exist */ |
| 4433 | error = -ENOENT; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4434 | if (d_is_negative(old_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | goto exit4; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4436 | new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | error = PTR_ERR(new_dentry); |
| 4438 | if (IS_ERR(new_dentry)) |
| 4439 | goto exit4; |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4440 | error = -EEXIST; |
| 4441 | if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) |
| 4442 | goto exit5; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4443 | if (flags & RENAME_EXCHANGE) { |
| 4444 | error = -ENOENT; |
| 4445 | if (d_is_negative(new_dentry)) |
| 4446 | goto exit5; |
| 4447 | |
| 4448 | if (!d_is_dir(new_dentry)) { |
| 4449 | error = -ENOTDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4450 | if (new_last.name[new_last.len]) |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4451 | goto exit5; |
| 4452 | } |
| 4453 | } |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4454 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
| 4455 | if (!d_is_dir(old_dentry)) { |
| 4456 | error = -ENOTDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4457 | if (old_last.name[old_last.len]) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4458 | goto exit5; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4459 | if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4460 | goto exit5; |
| 4461 | } |
| 4462 | /* source should not be ancestor of target */ |
| 4463 | error = -EINVAL; |
| 4464 | if (old_dentry == trap) |
| 4465 | goto exit5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | /* target should not be an ancestor of source */ |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4467 | if (!(flags & RENAME_EXCHANGE)) |
| 4468 | error = -ENOTEMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | if (new_dentry == trap) |
| 4470 | goto exit5; |
| 4471 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4472 | error = security_path_rename(&old_path, old_dentry, |
| 4473 | &new_path, new_dentry, flags); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4474 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4475 | goto exit5; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4476 | error = vfs_rename(old_path.dentry->d_inode, old_dentry, |
| 4477 | new_path.dentry->d_inode, new_dentry, |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4478 | &delegated_inode, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | exit5: |
| 4480 | dput(new_dentry); |
| 4481 | exit4: |
| 4482 | dput(old_dentry); |
| 4483 | exit3: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4484 | unlock_rename(new_path.dentry, old_path.dentry); |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4485 | if (delegated_inode) { |
| 4486 | error = break_deleg_wait(&delegated_inode); |
| 4487 | if (!error) |
| 4488 | goto retry_deleg; |
| 4489 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4490 | mnt_drop_write(old_path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | exit2: |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4492 | if (retry_estale(error, lookup_flags)) |
| 4493 | should_retry = true; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4494 | path_put(&new_path); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4495 | putname(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4497 | path_put(&old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | putname(from); |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4499 | if (should_retry) { |
| 4500 | should_retry = false; |
| 4501 | lookup_flags |= LOOKUP_REVAL; |
| 4502 | goto retry; |
| 4503 | } |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4504 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | return error; |
| 4506 | } |
| 4507 | |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4508 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, |
| 4509 | int, newdfd, const char __user *, newname, unsigned int, flags) |
| 4510 | { |
| 4511 | return do_renameat2(olddfd, oldname, newdfd, newname, flags); |
| 4512 | } |
| 4513 | |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4514 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
| 4515 | int, newdfd, const char __user *, newname) |
| 4516 | { |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4517 | return do_renameat2(olddfd, oldname, newdfd, newname, 0); |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4518 | } |
| 4519 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 4520 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4521 | { |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4522 | return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4523 | } |
| 4524 | |
Miklos Szeredi | 787fb6b | 2014-10-24 00:14:36 +0200 | [diff] [blame] | 4525 | int vfs_whiteout(struct inode *dir, struct dentry *dentry) |
| 4526 | { |
| 4527 | int error = may_create(dir, dentry); |
| 4528 | if (error) |
| 4529 | return error; |
| 4530 | |
| 4531 | if (!dir->i_op->mknod) |
| 4532 | return -EPERM; |
| 4533 | |
| 4534 | return dir->i_op->mknod(dir, dentry, |
| 4535 | S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); |
| 4536 | } |
| 4537 | EXPORT_SYMBOL(vfs_whiteout); |
| 4538 | |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 4539 | int readlink_copy(char __user *buffer, int buflen, const char *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | { |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 4541 | int len = PTR_ERR(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | if (IS_ERR(link)) |
| 4543 | goto out; |
| 4544 | |
| 4545 | len = strlen(link); |
| 4546 | if (len > (unsigned) buflen) |
| 4547 | len = buflen; |
| 4548 | if (copy_to_user(buffer, link, len)) |
| 4549 | len = -EFAULT; |
| 4550 | out: |
| 4551 | return len; |
| 4552 | } |
| 4553 | |
Miklos Szeredi | d60874c | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 4554 | /** |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4555 | * vfs_readlink - copy symlink body into userspace buffer |
| 4556 | * @dentry: dentry on which to get symbolic link |
| 4557 | * @buffer: user memory pointer |
| 4558 | * @buflen: size of buffer |
| 4559 | * |
| 4560 | * Does not touch atime. That's up to the caller if necessary |
| 4561 | * |
| 4562 | * Does not call security hook. |
| 4563 | */ |
| 4564 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4565 | { |
| 4566 | struct inode *inode = d_inode(dentry); |
Al Viro | f2df5da | 2018-07-19 17:35:51 -0400 | [diff] [blame] | 4567 | DEFINE_DELAYED_CALL(done); |
| 4568 | const char *link; |
| 4569 | int res; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4570 | |
Miklos Szeredi | 76fca90 | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4571 | if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { |
| 4572 | if (unlikely(inode->i_op->readlink)) |
| 4573 | return inode->i_op->readlink(dentry, buffer, buflen); |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4574 | |
Miklos Szeredi | 76fca90 | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4575 | if (!d_is_symlink(dentry)) |
| 4576 | return -EINVAL; |
| 4577 | |
| 4578 | spin_lock(&inode->i_lock); |
| 4579 | inode->i_opflags |= IOP_DEFAULT_READLINK; |
| 4580 | spin_unlock(&inode->i_lock); |
| 4581 | } |
| 4582 | |
Eric Biggers | 4c4f7c1 | 2019-04-10 13:21:14 -0700 | [diff] [blame] | 4583 | link = READ_ONCE(inode->i_link); |
Al Viro | f2df5da | 2018-07-19 17:35:51 -0400 | [diff] [blame] | 4584 | if (!link) { |
| 4585 | link = inode->i_op->get_link(dentry, inode, &done); |
| 4586 | if (IS_ERR(link)) |
| 4587 | return PTR_ERR(link); |
| 4588 | } |
| 4589 | res = readlink_copy(buffer, buflen, link); |
| 4590 | do_delayed_call(&done); |
| 4591 | return res; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4592 | } |
| 4593 | EXPORT_SYMBOL(vfs_readlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | |
Miklos Szeredi | d60874c | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 4595 | /** |
| 4596 | * vfs_get_link - get symlink body |
| 4597 | * @dentry: dentry on which to get symbolic link |
| 4598 | * @done: caller needs to free returned data with this |
| 4599 | * |
| 4600 | * Calls security hook and i_op->get_link() on the supplied inode. |
| 4601 | * |
| 4602 | * It does not touch atime. That's up to the caller if necessary. |
| 4603 | * |
| 4604 | * Does not work on "special" symlinks like /proc/$$/fd/N |
| 4605 | */ |
| 4606 | const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) |
| 4607 | { |
| 4608 | const char *res = ERR_PTR(-EINVAL); |
| 4609 | struct inode *inode = d_inode(dentry); |
| 4610 | |
| 4611 | if (d_is_symlink(dentry)) { |
| 4612 | res = ERR_PTR(security_inode_readlink(dentry)); |
| 4613 | if (!res) |
| 4614 | res = inode->i_op->get_link(dentry, inode, done); |
| 4615 | } |
| 4616 | return res; |
| 4617 | } |
| 4618 | EXPORT_SYMBOL(vfs_get_link); |
| 4619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | /* get the link contents into pagecache */ |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4621 | const char *page_get_link(struct dentry *dentry, struct inode *inode, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4622 | struct delayed_call *callback) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | { |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 4624 | char *kaddr; |
| 4625 | struct page *page; |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4626 | struct address_space *mapping = inode->i_mapping; |
| 4627 | |
Al Viro | d3883d4 | 2015-11-17 10:41:04 -0500 | [diff] [blame] | 4628 | if (!dentry) { |
| 4629 | page = find_get_page(mapping, 0); |
| 4630 | if (!page) |
| 4631 | return ERR_PTR(-ECHILD); |
| 4632 | if (!PageUptodate(page)) { |
| 4633 | put_page(page); |
| 4634 | return ERR_PTR(-ECHILD); |
| 4635 | } |
| 4636 | } else { |
| 4637 | page = read_mapping_page(mapping, 0, NULL); |
| 4638 | if (IS_ERR(page)) |
| 4639 | return (char*)page; |
| 4640 | } |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4641 | set_delayed_call(callback, page_put_link, page); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 4642 | BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); |
| 4643 | kaddr = page_address(page); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4644 | nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 4645 | return kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | } |
| 4647 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4648 | EXPORT_SYMBOL(page_get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4650 | void page_put_link(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4651 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4652 | put_page(arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4654 | EXPORT_SYMBOL(page_put_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | |
Al Viro | aa80dea | 2015-11-16 18:26:34 -0500 | [diff] [blame] | 4656 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4657 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4658 | DEFINE_DELAYED_CALL(done); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4659 | int res = readlink_copy(buffer, buflen, |
| 4660 | page_get_link(dentry, d_inode(dentry), |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4661 | &done)); |
| 4662 | do_delayed_call(&done); |
Al Viro | aa80dea | 2015-11-16 18:26:34 -0500 | [diff] [blame] | 4663 | return res; |
| 4664 | } |
| 4665 | EXPORT_SYMBOL(page_readlink); |
| 4666 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4667 | /* |
| 4668 | * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS |
| 4669 | */ |
| 4670 | int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | { |
| 4672 | struct address_space *mapping = inode->i_mapping; |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4673 | struct page *page; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4674 | void *fsdata; |
Dmitriy Monakhov | beb497a | 2007-02-16 01:27:18 -0800 | [diff] [blame] | 4675 | int err; |
Tetsuo Handa | c718a97 | 2017-05-08 15:58:59 -0700 | [diff] [blame] | 4676 | unsigned int flags = 0; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4677 | if (nofs) |
| 4678 | flags |= AOP_FLAG_NOFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 4680 | retry: |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4681 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4682 | flags, &page, &fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | if (err) |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4684 | goto fail; |
| 4685 | |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 4686 | memcpy(page_address(page), symname, len-1); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4687 | |
| 4688 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, |
| 4689 | page, fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | if (err < 0) |
| 4691 | goto fail; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4692 | if (err < len-1) |
| 4693 | goto retry; |
| 4694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | mark_inode_dirty(inode); |
| 4696 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | fail: |
| 4698 | return err; |
| 4699 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4700 | EXPORT_SYMBOL(__page_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4702 | int page_symlink(struct inode *inode, const char *symname, int len) |
| 4703 | { |
| 4704 | return __page_symlink(inode, symname, len, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 4705 | !mapping_gfp_constraint(inode->i_mapping, __GFP_FS)); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4706 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4707 | EXPORT_SYMBOL(page_symlink); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4708 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 4709 | const struct inode_operations page_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4710 | .get_link = page_get_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | EXPORT_SYMBOL(page_symlink_inode_operations); |