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 | /** |
| 553 | * path_connected - Verify that a path->dentry is below path->mnt.mnt_root |
| 554 | * @path: nameidate to verify |
| 555 | * |
| 556 | * Rename can sometimes move a file or directory outside of a bind |
| 557 | * mount, path_connected allows those cases to be detected. |
| 558 | */ |
| 559 | static bool path_connected(const struct path *path) |
| 560 | { |
| 561 | struct vfsmount *mnt = path->mnt; |
Eric W. Biederman | 95dd775 | 2018-03-14 18:20:29 -0500 | [diff] [blame] | 562 | struct super_block *sb = mnt->mnt_sb; |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 563 | |
Eric W. Biederman | 95dd775 | 2018-03-14 18:20:29 -0500 | [diff] [blame] | 564 | /* Bind mounts and multi-root filesystems can have disconnected paths */ |
| 565 | 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] | 566 | return true; |
| 567 | |
| 568 | return is_subdir(path->dentry, mnt->mnt_root); |
| 569 | } |
| 570 | |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 571 | static inline int nd_alloc_stack(struct nameidata *nd) |
| 572 | { |
Al Viro | da4e0be | 2015-05-03 20:52:15 -0400 | [diff] [blame] | 573 | if (likely(nd->depth != EMBEDDED_LEVELS)) |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 574 | return 0; |
| 575 | if (likely(nd->stack != nd->internal)) |
| 576 | return 0; |
| 577 | return __nd_alloc_stack(nd); |
| 578 | } |
| 579 | |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 580 | static void drop_links(struct nameidata *nd) |
| 581 | { |
| 582 | int i = nd->depth; |
| 583 | while (i--) { |
| 584 | struct saved *last = nd->stack + i; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 585 | do_delayed_call(&last->done); |
| 586 | clear_delayed_call(&last->done); |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | static void terminate_walk(struct nameidata *nd) |
| 591 | { |
| 592 | drop_links(nd); |
| 593 | if (!(nd->flags & LOOKUP_RCU)) { |
| 594 | int i; |
| 595 | path_put(&nd->path); |
| 596 | for (i = 0; i < nd->depth; i++) |
| 597 | path_put(&nd->stack[i].link); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 598 | if (nd->flags & LOOKUP_ROOT_GRABBED) { |
Al Viro | 102b8af | 2015-05-12 17:35:52 -0400 | [diff] [blame] | 599 | path_put(&nd->root); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 600 | nd->flags &= ~LOOKUP_ROOT_GRABBED; |
Al Viro | 102b8af | 2015-05-12 17:35:52 -0400 | [diff] [blame] | 601 | } |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 602 | } else { |
| 603 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 604 | rcu_read_unlock(); |
| 605 | } |
| 606 | nd->depth = 0; |
| 607 | } |
| 608 | |
| 609 | /* path_put is needed afterwards regardless of success or failure */ |
| 610 | static bool legitimize_path(struct nameidata *nd, |
| 611 | struct path *path, unsigned seq) |
| 612 | { |
| 613 | int res = __legitimize_mnt(path->mnt, nd->m_seq); |
| 614 | if (unlikely(res)) { |
| 615 | if (res > 0) |
| 616 | path->mnt = NULL; |
| 617 | path->dentry = NULL; |
| 618 | return false; |
| 619 | } |
| 620 | if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { |
| 621 | path->dentry = NULL; |
| 622 | return false; |
| 623 | } |
| 624 | return !read_seqcount_retry(&path->dentry->d_seq, seq); |
| 625 | } |
| 626 | |
| 627 | static bool legitimize_links(struct nameidata *nd) |
| 628 | { |
| 629 | int i; |
| 630 | for (i = 0; i < nd->depth; i++) { |
| 631 | struct saved *last = nd->stack + i; |
| 632 | if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { |
| 633 | drop_links(nd); |
| 634 | nd->depth = i + 1; |
| 635 | return false; |
| 636 | } |
| 637 | } |
| 638 | return true; |
| 639 | } |
| 640 | |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 641 | static bool legitimize_root(struct nameidata *nd) |
| 642 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 643 | /* |
| 644 | * For scoped-lookups (where nd->root has been zeroed), we need to |
| 645 | * restart the whole lookup from scratch -- because set_root() is wrong |
| 646 | * for these lookups (nd->dfd is the root, not the filesystem root). |
| 647 | */ |
| 648 | if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED)) |
| 649 | return false; |
| 650 | /* 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] | 651 | if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT)) |
| 652 | return true; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 653 | nd->flags |= LOOKUP_ROOT_GRABBED; |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 654 | return legitimize_path(nd, &nd->root, nd->root_seq); |
| 655 | } |
| 656 | |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 657 | /* |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 658 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 659 | * Documentation/filesystems/path-lookup.txt). In situations when we can't |
| 660 | * 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] | 661 | * normal reference counts on dentries and vfsmounts to transition to ref-walk |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 662 | * mode. Refcounts are grabbed at the last known good point before rcu-walk |
| 663 | * got stuck, so ref-walk may continue from there. If this is not successful |
| 664 | * (eg. a seqcount has changed), then failure is returned and it's up to caller |
| 665 | * to restart the path walk from the beginning in ref-walk mode. |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 666 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 667 | |
| 668 | /** |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 669 | * unlazy_walk - try to switch to ref-walk mode. |
| 670 | * @nd: nameidata pathwalk data |
Randy Dunlap | 3919162 | 2011-01-08 19:36:21 -0800 | [diff] [blame] | 671 | * Returns: 0 on success, -ECHILD on failure |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 672 | * |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 673 | * unlazy_walk attempts to legitimize the current nd->path and nd->root |
| 674 | * for ref-walk mode. |
| 675 | * Must be called from rcu-walk context. |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 676 | * Nothing should touch nameidata between unlazy_walk() failure and |
| 677 | * terminate_walk(). |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 678 | */ |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 679 | static int unlazy_walk(struct nameidata *nd) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 680 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 681 | struct dentry *parent = nd->path.dentry; |
| 682 | |
| 683 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 684 | |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 685 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 686 | if (unlikely(!legitimize_links(nd))) |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 687 | goto out1; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 688 | if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) |
| 689 | goto out; |
Al Viro | ee594bf | 2019-07-16 21:05:36 -0400 | [diff] [blame] | 690 | if (unlikely(!legitimize_root(nd))) |
| 691 | goto out; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 692 | rcu_read_unlock(); |
| 693 | BUG_ON(nd->inode != parent->d_inode); |
| 694 | return 0; |
| 695 | |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 696 | out1: |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 697 | nd->path.mnt = NULL; |
| 698 | nd->path.dentry = NULL; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 699 | out: |
| 700 | rcu_read_unlock(); |
| 701 | return -ECHILD; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * unlazy_child - try to switch to ref-walk mode. |
| 706 | * @nd: nameidata pathwalk data |
| 707 | * @dentry: child of nd->path.dentry |
| 708 | * @seq: seq number to check dentry against |
| 709 | * Returns: 0 on success, -ECHILD on failure |
| 710 | * |
| 711 | * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry |
| 712 | * for ref-walk mode. @dentry must be a path found by a do_lookup call on |
| 713 | * @nd. Must be called from rcu-walk context. |
| 714 | * Nothing should touch nameidata between unlazy_child() failure and |
| 715 | * terminate_walk(). |
| 716 | */ |
| 717 | static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq) |
| 718 | { |
| 719 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
| 720 | |
| 721 | nd->flags &= ~LOOKUP_RCU; |
| 722 | if (unlikely(!legitimize_links(nd))) |
| 723 | goto out2; |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 724 | if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) |
| 725 | goto out2; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 726 | if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 727 | goto out1; |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 728 | |
Linus Torvalds | 1557008 | 2013-09-02 11:38:06 -0700 | [diff] [blame] | 729 | /* |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 730 | * We need to move both the parent and the dentry from the RCU domain |
| 731 | * to be properly refcounted. And the sequence number in the dentry |
| 732 | * validates *both* dentry counters, since we checked the sequence |
| 733 | * number of the parent after we got the child sequence number. So we |
| 734 | * 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] | 735 | */ |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 736 | if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) |
| 737 | goto out; |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 738 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
| 739 | goto out_dput; |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 740 | /* |
| 741 | * Sequence counts matched. Now make sure that the root is |
| 742 | * still valid and get it if required. |
| 743 | */ |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 744 | if (unlikely(!legitimize_root(nd))) |
| 745 | goto out_dput; |
Al Viro | 8b61e74 | 2013-11-08 12:45:01 -0500 | [diff] [blame] | 746 | rcu_read_unlock(); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 747 | return 0; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 748 | |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 749 | out2: |
| 750 | nd->path.mnt = NULL; |
| 751 | out1: |
| 752 | nd->path.dentry = NULL; |
Linus Torvalds | e5c832d | 2013-09-08 18:13:49 -0700 | [diff] [blame] | 753 | out: |
Al Viro | 8b61e74 | 2013-11-08 12:45:01 -0500 | [diff] [blame] | 754 | rcu_read_unlock(); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 755 | return -ECHILD; |
| 756 | out_dput: |
| 757 | rcu_read_unlock(); |
| 758 | dput(dentry); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 759 | return -ECHILD; |
| 760 | } |
| 761 | |
Al Viro | 4ce16ef3 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 762 | static inline int d_revalidate(struct dentry *dentry, unsigned int flags) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 763 | { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 764 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) |
| 765 | return dentry->d_op->d_revalidate(dentry, flags); |
| 766 | else |
| 767 | return 1; |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 768 | } |
| 769 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 770 | /** |
| 771 | * complete_walk - successful completion of path walk |
| 772 | * @nd: pointer nameidata |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 773 | * |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 774 | * If we had been in RCU mode, drop out of it and legitimize nd->path. |
| 775 | * Revalidate the final result, unless we'd already done that during |
| 776 | * the path walk or the filesystem doesn't ask for it. Return 0 on |
| 777 | * success, -error on failure. In case of failure caller does not |
| 778 | * need to drop nd->path. |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 779 | */ |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 780 | static int complete_walk(struct nameidata *nd) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 781 | { |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 782 | struct dentry *dentry = nd->path.dentry; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 783 | int status; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 784 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 785 | if (nd->flags & LOOKUP_RCU) { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 786 | /* |
| 787 | * We don't want to zero nd->root for scoped-lookups or |
| 788 | * externally-managed nd->root. |
| 789 | */ |
| 790 | if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED))) |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 791 | nd->root.mnt = NULL; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 792 | if (unlikely(unlazy_walk(nd))) |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 793 | return -ECHILD; |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 794 | } |
| 795 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 796 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
| 797 | /* |
| 798 | * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't |
| 799 | * ever step outside the root during lookup" and should already |
| 800 | * be guaranteed by the rest of namei, we want to avoid a namei |
| 801 | * BUG resulting in userspace being given a path that was not |
| 802 | * scoped within the root at some point during the lookup. |
| 803 | * |
| 804 | * So, do a final sanity-check to make sure that in the |
| 805 | * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) |
| 806 | * we won't silently return an fd completely outside of the |
| 807 | * requested root to userspace. |
| 808 | * |
| 809 | * Userspace could move the path outside the root after this |
| 810 | * check, but as discussed elsewhere this is not a concern (the |
| 811 | * resolved file was inside the root at some point). |
| 812 | */ |
| 813 | if (!path_is_under(&nd->path, &nd->root)) |
| 814 | return -EXDEV; |
| 815 | } |
| 816 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 817 | if (likely(!(nd->flags & LOOKUP_JUMPED))) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 818 | return 0; |
| 819 | |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 820 | if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 821 | return 0; |
| 822 | |
Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 823 | status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 824 | if (status > 0) |
| 825 | return 0; |
| 826 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 827 | if (!status) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 828 | status = -ESTALE; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 829 | |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 830 | return status; |
| 831 | } |
| 832 | |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 833 | static int set_root(struct nameidata *nd) |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 834 | { |
Al Viro | 7bd8837 | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 835 | struct fs_struct *fs = current->fs; |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 836 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 837 | /* |
| 838 | * Jumping to the real root in a scoped-lookup is a BUG in namei, but we |
| 839 | * still have to ensure it doesn't happen because it will cause a breakout |
| 840 | * from the dirfd. |
| 841 | */ |
| 842 | if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) |
| 843 | return -ENOTRECOVERABLE; |
| 844 | |
Al Viro | 9e6697e | 2015-12-05 20:07:21 -0500 | [diff] [blame] | 845 | if (nd->flags & LOOKUP_RCU) { |
| 846 | unsigned seq; |
| 847 | |
| 848 | do { |
| 849 | seq = read_seqcount_begin(&fs->seq); |
| 850 | nd->root = fs->root; |
| 851 | nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); |
| 852 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 853 | } else { |
| 854 | get_fs_root(fs, &nd->root); |
Al Viro | 84a2bd3 | 2019-07-16 21:20:17 -0400 | [diff] [blame] | 855 | nd->flags |= LOOKUP_ROOT_GRABBED; |
Al Viro | 9e6697e | 2015-12-05 20:07:21 -0500 | [diff] [blame] | 856 | } |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 857 | return 0; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 858 | } |
| 859 | |
Nick Piggin | 7b9337a | 2011-01-14 08:42:43 +0000 | [diff] [blame] | 860 | static inline void path_to_nameidata(const struct path *path, |
| 861 | struct nameidata *nd) |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 862 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 863 | if (!(nd->flags & LOOKUP_RCU)) { |
| 864 | dput(nd->path.dentry); |
| 865 | if (nd->path.mnt != path->mnt) |
| 866 | mntput(nd->path.mnt); |
Huang Shijie | 9a22968 | 2010-04-02 17:37:13 +0800 | [diff] [blame] | 867 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 868 | nd->path.mnt = path->mnt; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 869 | nd->path.dentry = path->dentry; |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 872 | static int nd_jump_root(struct nameidata *nd) |
| 873 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 874 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 875 | return -EXDEV; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 876 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
| 877 | /* Absolute path arguments to path_init() are allowed. */ |
| 878 | if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) |
| 879 | return -EXDEV; |
| 880 | } |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 881 | if (!nd->root.mnt) { |
| 882 | int error = set_root(nd); |
| 883 | if (error) |
| 884 | return error; |
| 885 | } |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 886 | if (nd->flags & LOOKUP_RCU) { |
| 887 | struct dentry *d; |
| 888 | nd->path = nd->root; |
| 889 | d = nd->path.dentry; |
| 890 | nd->inode = d->d_inode; |
| 891 | nd->seq = nd->root_seq; |
| 892 | if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq))) |
| 893 | return -ECHILD; |
| 894 | } else { |
| 895 | path_put(&nd->path); |
| 896 | nd->path = nd->root; |
| 897 | path_get(&nd->path); |
| 898 | nd->inode = nd->path.dentry->d_inode; |
| 899 | } |
| 900 | nd->flags |= LOOKUP_JUMPED; |
| 901 | return 0; |
| 902 | } |
| 903 | |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 904 | /* |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 905 | * Helper to directly jump to a known parsed path from ->get_link, |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 906 | * caller must have taken a reference to path beforehand. |
| 907 | */ |
Aleksa Sarai | 1bc8207 | 2019-12-07 01:13:28 +1100 | [diff] [blame] | 908 | int nd_jump_link(struct path *path) |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 909 | { |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 910 | int error = -ELOOP; |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 911 | struct nameidata *nd = current->nameidata; |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 912 | |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 913 | if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) |
| 914 | goto err; |
| 915 | |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 916 | error = -EXDEV; |
| 917 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
| 918 | if (nd->path.mnt != path->mnt) |
| 919 | goto err; |
| 920 | } |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 921 | /* Not currently safe for scoped-lookups. */ |
| 922 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) |
| 923 | goto err; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 924 | |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 925 | path_put(&nd->path); |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 926 | nd->path = *path; |
| 927 | nd->inode = nd->path.dentry->d_inode; |
| 928 | nd->flags |= LOOKUP_JUMPED; |
Aleksa Sarai | 1bc8207 | 2019-12-07 01:13:28 +1100 | [diff] [blame] | 929 | return 0; |
Aleksa Sarai | 4b99d49 | 2019-12-07 01:13:31 +1100 | [diff] [blame] | 930 | |
| 931 | err: |
| 932 | path_put(path); |
| 933 | return error; |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 934 | } |
| 935 | |
Al Viro | b9ff442 | 2015-05-02 20:19:23 -0400 | [diff] [blame] | 936 | static inline void put_link(struct nameidata *nd) |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 937 | { |
Al Viro | 21c3003 | 2015-05-03 21:06:24 -0400 | [diff] [blame] | 938 | struct saved *last = nd->stack + --nd->depth; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 939 | do_delayed_call(&last->done); |
Al Viro | 6548fae | 2015-05-07 20:32:22 -0400 | [diff] [blame] | 940 | if (!(nd->flags & LOOKUP_RCU)) |
| 941 | path_put(&last->link); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 942 | } |
| 943 | |
Linus Torvalds | 561ec64 | 2012-10-26 10:05:07 -0700 | [diff] [blame] | 944 | int sysctl_protected_symlinks __read_mostly = 0; |
| 945 | int sysctl_protected_hardlinks __read_mostly = 0; |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 946 | int sysctl_protected_fifos __read_mostly; |
| 947 | int sysctl_protected_regular __read_mostly; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 948 | |
| 949 | /** |
| 950 | * may_follow_link - Check symlink following for unsafe situations |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 951 | * @nd: nameidata pathwalk data |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 952 | * |
| 953 | * In the case of the sysctl_protected_symlinks sysctl being enabled, |
| 954 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is |
| 955 | * in a sticky world-writable directory. This is to protect privileged |
| 956 | * processes from failing races against path names that may change out |
| 957 | * from under them by way of other users creating malicious symlinks. |
| 958 | * It will permit symlinks to be followed only when outside a sticky |
| 959 | * world-writable directory, or when the uid of the symlink and follower |
| 960 | * match, or when the directory owner matches the symlink's owner. |
| 961 | * |
| 962 | * Returns 0 if following the symlink is allowed, -ve on error. |
| 963 | */ |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 964 | 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] | 965 | { |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 966 | const struct inode *parent; |
Seth Forshee | 2d7f9e2 | 2016-04-26 14:36:23 -0500 | [diff] [blame] | 967 | kuid_t puid; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 968 | |
| 969 | if (!sysctl_protected_symlinks) |
| 970 | return 0; |
| 971 | |
| 972 | /* Allowed if owner and follower match. */ |
Eric W. Biederman | 81abe27 | 2012-08-03 09:38:08 -0700 | [diff] [blame] | 973 | if (uid_eq(current_cred()->fsuid, inode->i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 974 | return 0; |
| 975 | |
| 976 | /* Allowed if parent directory not sticky and world-writable. */ |
Al Viro | aa65fa3 | 2015-08-04 23:23:50 -0400 | [diff] [blame] | 977 | parent = nd->inode; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 978 | if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
| 979 | return 0; |
| 980 | |
| 981 | /* Allowed if parent directory and link owner match. */ |
Seth Forshee | 2d7f9e2 | 2016-04-26 14:36:23 -0500 | [diff] [blame] | 982 | puid = parent->i_uid; |
| 983 | if (uid_valid(puid) && uid_eq(puid, inode->i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 984 | return 0; |
| 985 | |
Al Viro | 31956502 | 2015-05-07 20:37:40 -0400 | [diff] [blame] | 986 | if (nd->flags & LOOKUP_RCU) |
| 987 | return -ECHILD; |
| 988 | |
Richard Guy Briggs | ea841ba | 2018-03-21 04:42:21 -0400 | [diff] [blame] | 989 | audit_inode(nd->name, nd->stack[0].link.dentry, 0); |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 990 | audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 991 | return -EACCES; |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * safe_hardlink_source - Check for safe hardlink conditions |
| 996 | * @inode: the source inode to hardlink from |
| 997 | * |
| 998 | * Return false if at least one of the following conditions: |
| 999 | * - inode is not a regular file |
| 1000 | * - inode is setuid |
| 1001 | * - inode is setgid and group-exec |
| 1002 | * - access failure for read and write |
| 1003 | * |
| 1004 | * Otherwise returns true. |
| 1005 | */ |
| 1006 | static bool safe_hardlink_source(struct inode *inode) |
| 1007 | { |
| 1008 | umode_t mode = inode->i_mode; |
| 1009 | |
| 1010 | /* Special files should not get pinned to the filesystem. */ |
| 1011 | if (!S_ISREG(mode)) |
| 1012 | return false; |
| 1013 | |
| 1014 | /* Setuid files should not get pinned to the filesystem. */ |
| 1015 | if (mode & S_ISUID) |
| 1016 | return false; |
| 1017 | |
| 1018 | /* Executable setgid files should not get pinned to the filesystem. */ |
| 1019 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) |
| 1020 | return false; |
| 1021 | |
| 1022 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ |
| 1023 | if (inode_permission(inode, MAY_READ | MAY_WRITE)) |
| 1024 | return false; |
| 1025 | |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * may_linkat - Check permissions for creating a hardlink |
| 1031 | * @link: the source to hardlink from |
| 1032 | * |
| 1033 | * Block hardlink when all of: |
| 1034 | * - sysctl_protected_hardlinks enabled |
| 1035 | * - fsuid does not match inode |
| 1036 | * - hardlink source is unsafe (see safe_hardlink_source() above) |
Dirk Steinmetz | f2ca379 | 2015-10-20 16:09:19 +0200 | [diff] [blame] | 1037 | * - not CAP_FOWNER in a namespace with the inode owner uid mapped |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1038 | * |
| 1039 | * Returns 0 if successful, -ve on error. |
| 1040 | */ |
| 1041 | static int may_linkat(struct path *link) |
| 1042 | { |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 1043 | struct inode *inode = link->dentry->d_inode; |
| 1044 | |
| 1045 | /* Inode writeback is not safe when the uid or gid are invalid. */ |
| 1046 | if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid)) |
| 1047 | return -EOVERFLOW; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1048 | |
| 1049 | if (!sysctl_protected_hardlinks) |
| 1050 | return 0; |
| 1051 | |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1052 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, |
| 1053 | * otherwise, it must be a safe source. |
| 1054 | */ |
Kees Cook | cc658db | 2017-06-21 09:53:06 -0700 | [diff] [blame] | 1055 | if (safe_hardlink_source(inode) || inode_owner_or_capable(inode)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1056 | return 0; |
| 1057 | |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1058 | audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1059 | return -EPERM; |
| 1060 | } |
| 1061 | |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1062 | /** |
| 1063 | * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory |
| 1064 | * should be allowed, or not, on files that already |
| 1065 | * exist. |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1066 | * @dir_mode: mode bits of directory |
| 1067 | * @dir_uid: owner of directory |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1068 | * @inode: the inode of the file to open |
| 1069 | * |
| 1070 | * Block an O_CREAT open of a FIFO (or a regular file) when: |
| 1071 | * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled |
| 1072 | * - the file already exists |
| 1073 | * - we are in a sticky directory |
| 1074 | * - we don't own the file |
| 1075 | * - the owner of the directory doesn't own the file |
| 1076 | * - the directory is world writable |
| 1077 | * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 |
| 1078 | * the directory doesn't have to be world writable: being group writable will |
| 1079 | * be enough. |
| 1080 | * |
| 1081 | * Returns 0 if the open is allowed, -ve on error. |
| 1082 | */ |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1083 | 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] | 1084 | struct inode * const inode) |
| 1085 | { |
| 1086 | if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || |
| 1087 | (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1088 | likely(!(dir_mode & S_ISVTX)) || |
| 1089 | uid_eq(inode->i_uid, dir_uid) || |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1090 | uid_eq(current_fsuid(), inode->i_uid)) |
| 1091 | return 0; |
| 1092 | |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 1093 | if (likely(dir_mode & 0002) || |
| 1094 | (dir_mode & 0020 && |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1095 | ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || |
| 1096 | (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { |
Kees Cook | 245d736 | 2019-10-02 16:41:58 -0700 | [diff] [blame] | 1097 | const char *operation = S_ISFIFO(inode->i_mode) ? |
| 1098 | "sticky_create_fifo" : |
| 1099 | "sticky_create_regular"; |
| 1100 | audit_log_path_denied(AUDIT_ANOM_CREAT, operation); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 1101 | return -EACCES; |
| 1102 | } |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
David Howells | f015f126 | 2012-06-25 12:55:28 +0100 | [diff] [blame] | 1106 | /* |
| 1107 | * follow_up - Find the mountpoint of path's vfsmount |
| 1108 | * |
| 1109 | * Given a path, find the mountpoint of its source file system. |
| 1110 | * Replace @path with the path of the mountpoint in the parent mount. |
| 1111 | * Up is towards /. |
| 1112 | * |
| 1113 | * Return 1 if we went up a level and 0 if we were already at the |
| 1114 | * root. |
| 1115 | */ |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 1116 | int follow_up(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | { |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1118 | struct mount *mnt = real_mount(path->mnt); |
| 1119 | struct mount *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | struct dentry *mountpoint; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1121 | |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1122 | read_seqlock_excl(&mount_lock); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1123 | parent = mnt->mnt_parent; |
Al Viro | 3c0a616 | 2012-07-18 17:32:50 +0400 | [diff] [blame] | 1124 | if (parent == mnt) { |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1125 | read_sequnlock_excl(&mount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | return 0; |
| 1127 | } |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1128 | mntget(&parent->mnt); |
Al Viro | a73324d | 2011-11-24 22:25:07 -0500 | [diff] [blame] | 1129 | mountpoint = dget(mnt->mnt_mountpoint); |
Al Viro | 48a066e | 2013-09-29 22:06:07 -0400 | [diff] [blame] | 1130 | read_sequnlock_excl(&mount_lock); |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 1131 | dput(path->dentry); |
| 1132 | path->dentry = mountpoint; |
| 1133 | mntput(path->mnt); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 1134 | path->mnt = &parent->mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | return 1; |
| 1136 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 1137 | EXPORT_SYMBOL(follow_up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 1139 | /* |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1140 | * Perform an automount |
| 1141 | * - return -EISDIR to tell follow_managed() to stop and return the path we |
| 1142 | * were called with. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | */ |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1144 | static int follow_automount(struct path *path, int *count, unsigned lookup_flags) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1145 | { |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1146 | struct dentry *dentry = path->dentry; |
Al Viro | 463ffb2 | 2005-06-06 13:36:05 -0700 | [diff] [blame] | 1147 | |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 1148 | /* We don't want to mount if someone's just doing a stat - |
| 1149 | * unless they're stat'ing a directory and appended a '/' to |
| 1150 | * the name. |
| 1151 | * |
| 1152 | * We do, however, want to mount if someone wants to open or |
| 1153 | * create a file of any type under the mountpoint, wants to |
| 1154 | * traverse through the mountpoint or wants to open the |
| 1155 | * mounted directory. Also, autofs may mark negative dentries |
| 1156 | * as being automount points. These will need the attentions |
| 1157 | * of the daemon to instantiate them before they can be used. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1158 | */ |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1159 | if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
Ian Kent | 5d38f04 | 2017-11-29 16:11:26 -0800 | [diff] [blame] | 1160 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1161 | dentry->d_inode) |
Ian Kent | 5d38f04 | 2017-11-29 16:11:26 -0800 | [diff] [blame] | 1162 | return -EISDIR; |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 1163 | |
Al Viro | 1c9f5e0 | 2020-01-16 22:05:18 -0500 | [diff] [blame] | 1164 | if (count && (*count)++ >= MAXSYMLINKS) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1165 | return -ELOOP; |
| 1166 | |
Al Viro | 25e195a | 2020-01-11 11:27:46 -0500 | [diff] [blame] | 1167 | return finish_automount(dentry->d_op->d_automount(path), path); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | /* |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1171 | * mount traversal - out-of-line part. One note on ->d_flags accesses - |
| 1172 | * dentries are pinned but not locked here, so negative dentry can go |
| 1173 | * positive right under us. Use of smp_load_acquire() provides a barrier |
| 1174 | * sufficient for ->d_inode and ->d_flags consistency. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1175 | */ |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1176 | static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, |
| 1177 | int *count, unsigned lookup_flags) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1178 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1179 | struct vfsmount *mnt = path->mnt; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1180 | bool need_mntput = false; |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1181 | int ret = 0; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1182 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1183 | while (flags & DCACHE_MANAGED_DENTRY) { |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1184 | /* Allow the filesystem to manage the transit without i_mutex |
| 1185 | * being held. */ |
Al Viro | d41efb5 | 2019-11-04 22:30:52 -0500 | [diff] [blame] | 1186 | if (flags & DCACHE_MANAGE_TRANSIT) { |
Ian Kent | fb5f51c | 2016-11-24 08:03:41 +1100 | [diff] [blame] | 1187 | ret = path->dentry->d_op->d_manage(path, false); |
Al Viro | 508c877 | 2020-01-14 22:09:57 -0500 | [diff] [blame] | 1188 | flags = smp_load_acquire(&path->dentry->d_flags); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1189 | if (ret < 0) |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1190 | break; |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1193 | if (flags & DCACHE_MOUNTED) { // something's mounted on it.. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1194 | struct vfsmount *mounted = lookup_mnt(path); |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1195 | if (mounted) { // ... in our namespace |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1196 | dput(path->dentry); |
| 1197 | if (need_mntput) |
| 1198 | mntput(path->mnt); |
| 1199 | path->mnt = mounted; |
| 1200 | path->dentry = dget(mounted->mnt_root); |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1201 | // here we know it's positive |
| 1202 | flags = path->dentry->d_flags; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1203 | need_mntput = true; |
| 1204 | continue; |
| 1205 | } |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1208 | if (!(flags & DCACHE_NEED_AUTOMOUNT)) |
| 1209 | break; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1210 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1211 | // uncovered automount point |
| 1212 | ret = follow_automount(path, count, lookup_flags); |
| 1213 | flags = smp_load_acquire(&path->dentry->d_flags); |
| 1214 | if (ret < 0) |
| 1215 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | } |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1217 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1218 | if (ret == -EISDIR) |
| 1219 | ret = 0; |
| 1220 | // possible if you race with several mount --move |
| 1221 | if (need_mntput && path->mnt == mnt) |
| 1222 | mntput(path->mnt); |
| 1223 | if (!ret && unlikely(d_flags_negative(flags))) |
Al Viro | d41efb5 | 2019-11-04 22:30:52 -0500 | [diff] [blame] | 1224 | ret = -ENOENT; |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1225 | *jumped = need_mntput; |
Al Viro | 8402752 | 2015-04-22 10:30:08 -0400 | [diff] [blame] | 1226 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1229 | static inline int traverse_mounts(struct path *path, bool *jumped, |
| 1230 | int *count, unsigned lookup_flags) |
| 1231 | { |
| 1232 | unsigned flags = smp_load_acquire(&path->dentry->d_flags); |
| 1233 | |
| 1234 | /* fastpath */ |
| 1235 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { |
| 1236 | *jumped = false; |
| 1237 | if (unlikely(d_flags_negative(flags))) |
| 1238 | return -ENOENT; |
| 1239 | return 0; |
| 1240 | } |
| 1241 | return __traverse_mounts(path, flags, jumped, count, lookup_flags); |
| 1242 | } |
| 1243 | |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1244 | int follow_down_one(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | { |
| 1246 | struct vfsmount *mounted; |
| 1247 | |
Al Viro | 1c755af | 2009-04-18 14:06:57 -0400 | [diff] [blame] | 1248 | mounted = lookup_mnt(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | if (mounted) { |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 1250 | dput(path->dentry); |
| 1251 | mntput(path->mnt); |
| 1252 | path->mnt = mounted; |
| 1253 | path->dentry = dget(mounted->mnt_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | return 1; |
| 1255 | } |
| 1256 | return 0; |
| 1257 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 1258 | EXPORT_SYMBOL(follow_down_one); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1260 | /* |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1261 | * Follow down to the covering mount currently visible to userspace. At each |
| 1262 | * point, the filesystem owning that dentry may be queried as to whether the |
| 1263 | * caller is permitted to proceed or not. |
| 1264 | */ |
| 1265 | int follow_down(struct path *path) |
| 1266 | { |
| 1267 | struct vfsmount *mnt = path->mnt; |
| 1268 | bool jumped; |
| 1269 | int ret = traverse_mounts(path, &jumped, NULL, 0); |
| 1270 | |
| 1271 | if (path->mnt != mnt) |
| 1272 | mntput(mnt); |
| 1273 | return ret; |
| 1274 | } |
| 1275 | EXPORT_SYMBOL(follow_down); |
| 1276 | |
| 1277 | /* |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1278 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
| 1279 | * we meet a managed dentry that would need blocking. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1280 | */ |
| 1281 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1282 | struct inode **inode, unsigned *seqp) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1283 | { |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1284 | struct dentry *dentry = path->dentry; |
| 1285 | unsigned int flags = dentry->d_flags; |
| 1286 | |
| 1287 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) |
| 1288 | return true; |
| 1289 | |
| 1290 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1291 | return false; |
| 1292 | |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1293 | for (;;) { |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1294 | /* |
| 1295 | * Don't forget we might have a non-mountpoint managed dentry |
| 1296 | * that wants to block transit. |
| 1297 | */ |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1298 | if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { |
| 1299 | int res = dentry->d_op->d_manage(path, true); |
| 1300 | if (res) |
| 1301 | return res == -EISDIR; |
| 1302 | flags = dentry->d_flags; |
NeilBrown | b8faf03 | 2014-08-04 17:06:29 +1000 | [diff] [blame] | 1303 | } |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1304 | |
Al Viro | ea936ae | 2020-01-16 09:52:04 -0500 | [diff] [blame] | 1305 | if (flags & DCACHE_MOUNTED) { |
| 1306 | struct mount *mounted = __lookup_mnt(path->mnt, dentry); |
| 1307 | if (mounted) { |
| 1308 | path->mnt = &mounted->mnt; |
| 1309 | dentry = path->dentry = mounted->mnt.mnt_root; |
| 1310 | nd->flags |= LOOKUP_JUMPED; |
| 1311 | *seqp = read_seqcount_begin(&dentry->d_seq); |
| 1312 | *inode = dentry->d_inode; |
| 1313 | /* |
| 1314 | * We don't need to re-check ->d_seq after this |
| 1315 | * ->d_inode read - there will be an RCU delay |
| 1316 | * between mount hash removal and ->mnt_root |
| 1317 | * becoming unpinned. |
| 1318 | */ |
| 1319 | flags = dentry->d_flags; |
| 1320 | continue; |
| 1321 | } |
| 1322 | if (read_seqretry(&mount_lock, nd->m_seq)) |
| 1323 | return false; |
| 1324 | } |
| 1325 | return !(flags & DCACHE_NEED_AUTOMOUNT); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1326 | } |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1327 | } |
| 1328 | |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1329 | static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, |
| 1330 | struct path *path, struct inode **inode, |
| 1331 | unsigned int *seqp) |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1332 | { |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1333 | bool jumped; |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1334 | int ret; |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1335 | |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1336 | path->mnt = nd->path.mnt; |
| 1337 | path->dentry = dentry; |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1338 | if (nd->flags & LOOKUP_RCU) { |
| 1339 | unsigned int seq = *seqp; |
| 1340 | if (unlikely(!*inode)) |
| 1341 | return -ENOENT; |
| 1342 | if (likely(__follow_mount_rcu(nd, path, inode, seqp))) |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1343 | return 0; |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1344 | if (unlazy_child(nd, dentry, seq)) |
| 1345 | return -ECHILD; |
| 1346 | // *path might've been clobbered by __follow_mount_rcu() |
| 1347 | path->mnt = nd->path.mnt; |
| 1348 | path->dentry = dentry; |
| 1349 | } |
Al Viro | 9deed3e | 2020-01-17 08:45:08 -0500 | [diff] [blame] | 1350 | ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); |
| 1351 | if (jumped) { |
| 1352 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1353 | ret = -EXDEV; |
| 1354 | else |
| 1355 | nd->flags |= LOOKUP_JUMPED; |
| 1356 | } |
| 1357 | if (unlikely(ret)) { |
| 1358 | dput(path->dentry); |
| 1359 | if (path->mnt != nd->path.mnt) |
| 1360 | mntput(path->mnt); |
| 1361 | } else { |
Al Viro | bd7c4b5 | 2020-01-08 20:37:23 -0500 | [diff] [blame] | 1362 | *inode = d_backing_inode(path->dentry); |
| 1363 | *seqp = 0; /* out of RCU mode, so the value doesn't matter */ |
| 1364 | } |
| 1365 | return ret; |
| 1366 | } |
| 1367 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1368 | static int follow_dotdot_rcu(struct nameidata *nd) |
| 1369 | { |
Al Viro | 4023bfc | 2014-09-13 21:59:43 -0400 | [diff] [blame] | 1370 | struct inode *inode = nd->inode; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1371 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1372 | while (1) { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 1373 | if (path_equal(&nd->path, &nd->root)) { |
| 1374 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 1375 | return -ECHILD; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1376 | break; |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 1377 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1378 | if (nd->path.dentry != nd->path.mnt->mnt_root) { |
| 1379 | struct dentry *old = nd->path.dentry; |
| 1380 | struct dentry *parent = old->d_parent; |
| 1381 | unsigned seq; |
| 1382 | |
Al Viro | 4023bfc | 2014-09-13 21:59:43 -0400 | [diff] [blame] | 1383 | inode = parent->d_inode; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1384 | seq = read_seqcount_begin(&parent->d_seq); |
Al Viro | aed434a | 2015-05-12 12:22:47 -0400 | [diff] [blame] | 1385 | if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) |
| 1386 | return -ECHILD; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1387 | nd->path.dentry = parent; |
| 1388 | nd->seq = seq; |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 1389 | if (unlikely(!path_connected(&nd->path))) |
Aleksa Sarai | 2b98149 | 2019-12-07 01:13:26 +1100 | [diff] [blame] | 1390 | return -ECHILD; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1391 | break; |
Al Viro | aed434a | 2015-05-12 12:22:47 -0400 | [diff] [blame] | 1392 | } else { |
| 1393 | struct mount *mnt = real_mount(nd->path.mnt); |
| 1394 | struct mount *mparent = mnt->mnt_parent; |
| 1395 | struct dentry *mountpoint = mnt->mnt_mountpoint; |
| 1396 | struct inode *inode2 = mountpoint->d_inode; |
| 1397 | unsigned seq = read_seqcount_begin(&mountpoint->d_seq); |
| 1398 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
| 1399 | return -ECHILD; |
| 1400 | if (&mparent->mnt == nd->path.mnt) |
| 1401 | break; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 1402 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1403 | return -ECHILD; |
Al Viro | aed434a | 2015-05-12 12:22:47 -0400 | [diff] [blame] | 1404 | /* we know that mountpoint was pinned */ |
| 1405 | nd->path.dentry = mountpoint; |
| 1406 | nd->path.mnt = &mparent->mnt; |
| 1407 | inode = inode2; |
| 1408 | nd->seq = seq; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1409 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1410 | } |
Al Viro | aed434a | 2015-05-12 12:22:47 -0400 | [diff] [blame] | 1411 | while (unlikely(d_mountpoint(nd->path.dentry))) { |
Al Viro | b37199e | 2014-03-20 15:18:22 -0400 | [diff] [blame] | 1412 | struct mount *mounted; |
| 1413 | mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry); |
Al Viro | aed434a | 2015-05-12 12:22:47 -0400 | [diff] [blame] | 1414 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
| 1415 | return -ECHILD; |
Al Viro | b37199e | 2014-03-20 15:18:22 -0400 | [diff] [blame] | 1416 | if (!mounted) |
| 1417 | break; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 1418 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1419 | return -ECHILD; |
Al Viro | b37199e | 2014-03-20 15:18:22 -0400 | [diff] [blame] | 1420 | nd->path.mnt = &mounted->mnt; |
| 1421 | nd->path.dentry = mounted->mnt.mnt_root; |
Al Viro | 4023bfc | 2014-09-13 21:59:43 -0400 | [diff] [blame] | 1422 | inode = nd->path.dentry->d_inode; |
Al Viro | b37199e | 2014-03-20 15:18:22 -0400 | [diff] [blame] | 1423 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | b37199e | 2014-03-20 15:18:22 -0400 | [diff] [blame] | 1424 | } |
Al Viro | 4023bfc | 2014-09-13 21:59:43 -0400 | [diff] [blame] | 1425 | nd->inode = inode; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1426 | return 0; |
| 1427 | } |
| 1428 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1429 | /* |
| 1430 | * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() |
| 1431 | */ |
| 1432 | static void follow_mount(struct path *path) |
| 1433 | { |
| 1434 | while (d_mountpoint(path->dentry)) { |
| 1435 | struct vfsmount *mounted = lookup_mnt(path); |
| 1436 | if (!mounted) |
| 1437 | break; |
| 1438 | dput(path->dentry); |
| 1439 | mntput(path->mnt); |
| 1440 | path->mnt = mounted; |
| 1441 | path->dentry = dget(mounted->mnt_root); |
| 1442 | } |
| 1443 | } |
| 1444 | |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 1445 | static int path_parent_directory(struct path *path) |
| 1446 | { |
| 1447 | struct dentry *old = path->dentry; |
| 1448 | /* rare case of legitimate dget_parent()... */ |
| 1449 | path->dentry = dget_parent(path->dentry); |
| 1450 | dput(old); |
| 1451 | if (unlikely(!path_connected(path))) |
| 1452 | return -ENOENT; |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 1456 | static int follow_dotdot(struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | { |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 1458 | while (1) { |
| 1459 | if (path_equal(&nd->path, &nd->root)) { |
| 1460 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
| 1461 | return -EXDEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | break; |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 1463 | } |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1464 | if (nd->path.dentry != nd->path.mnt->mnt_root) { |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 1465 | int ret = path_parent_directory(&nd->path); |
| 1466 | if (ret) |
| 1467 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | break; |
| 1469 | } |
Al Viro | 3088dd7 | 2010-01-30 15:47:29 -0500 | [diff] [blame] | 1470 | if (!follow_up(&nd->path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | break; |
Aleksa Sarai | 72ba292 | 2019-12-07 01:13:32 +1100 | [diff] [blame] | 1472 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
| 1473 | return -EXDEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | } |
Al Viro | 79ed022 | 2009-04-18 13:59:41 -0400 | [diff] [blame] | 1475 | follow_mount(&nd->path); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1476 | nd->inode = nd->path.dentry->d_inode; |
Eric W. Biederman | 397d425 | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 1477 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | /* |
Oleg Drokin | f4fdace | 2016-07-07 22:04:04 -0400 | [diff] [blame] | 1481 | * This looks up the name in dcache and possibly revalidates the found dentry. |
| 1482 | * NULL is returned if the dentry does not exist in the cache. |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1483 | */ |
Al Viro | e3c1392 | 2016-03-06 14:03:27 -0500 | [diff] [blame] | 1484 | static struct dentry *lookup_dcache(const struct qstr *name, |
| 1485 | struct dentry *dir, |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 1486 | unsigned int flags) |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1487 | { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1488 | struct dentry *dentry = d_lookup(dir, name); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1489 | if (dentry) { |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1490 | int error = d_revalidate(dentry, flags); |
| 1491 | if (unlikely(error <= 0)) { |
| 1492 | if (!error) |
| 1493 | d_invalidate(dentry); |
| 1494 | dput(dentry); |
| 1495 | return ERR_PTR(error); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1496 | } |
| 1497 | } |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1498 | return dentry; |
| 1499 | } |
| 1500 | |
| 1501 | /* |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1502 | * Parent directory has inode locked exclusive. This is one |
| 1503 | * and only case when ->lookup() gets called on non in-lookup |
| 1504 | * dentries - as the matter of fact, this only gets called |
| 1505 | * when directory is guaranteed to have no in-lookup children |
| 1506 | * at all. |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1507 | */ |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1508 | static struct dentry *__lookup_hash(const struct qstr *name, |
| 1509 | struct dentry *base, unsigned int flags) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1510 | { |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1511 | struct dentry *dentry = lookup_dcache(name, base, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1512 | struct dentry *old; |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1513 | struct inode *dir = base->d_inode; |
| 1514 | |
| 1515 | if (dentry) |
| 1516 | return dentry; |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1517 | |
| 1518 | /* Don't create child dentry for a dead directory. */ |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1519 | if (unlikely(IS_DEADDIR(dir))) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1520 | return ERR_PTR(-ENOENT); |
Al Viro | a03ece5 | 2018-03-08 11:00:45 -0500 | [diff] [blame] | 1521 | |
| 1522 | dentry = d_alloc(base, name); |
| 1523 | if (unlikely(!dentry)) |
| 1524 | return ERR_PTR(-ENOMEM); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1525 | |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1526 | old = dir->i_op->lookup(dir, dentry, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1527 | if (unlikely(old)) { |
| 1528 | dput(dentry); |
| 1529 | dentry = old; |
| 1530 | } |
| 1531 | return dentry; |
| 1532 | } |
| 1533 | |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1534 | static struct dentry *lookup_fast(struct nameidata *nd, |
| 1535 | struct inode **inode, |
| 1536 | unsigned *seqp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1538 | struct dentry *dentry, *parent = nd->path.dentry; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1539 | int status = 1; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1540 | |
Al Viro | 3cac260 | 2009-08-13 18:27:43 +0400 | [diff] [blame] | 1541 | /* |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1542 | * Rename seqlock is not required here because in the off chance |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1543 | * of a false negative due to a concurrent rename, the caller is |
| 1544 | * going to fall back to non-racy lookup. |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1545 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1546 | if (nd->flags & LOOKUP_RCU) { |
| 1547 | unsigned seq; |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 1548 | dentry = __d_lookup_rcu(parent, &nd->last, &seq); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1549 | if (unlikely(!dentry)) { |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 1550 | if (unlazy_walk(nd)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1551 | return ERR_PTR(-ECHILD); |
| 1552 | return NULL; |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1553 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1554 | |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1555 | /* |
| 1556 | * This sequence count validates that the inode matches |
| 1557 | * the dentry name information from lookup. |
| 1558 | */ |
David Howells | 63afdfc | 2015-05-06 15:59:00 +0100 | [diff] [blame] | 1559 | *inode = d_backing_inode(dentry); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1560 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1561 | return ERR_PTR(-ECHILD); |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1562 | |
| 1563 | /* |
| 1564 | * This sequence count validates that the parent had no |
| 1565 | * changes while we did the lookup of the dentry above. |
| 1566 | * |
| 1567 | * The memory barrier in read_seqcount_begin of child is |
| 1568 | * enough, we can use __read_seqcount_retry here. |
| 1569 | */ |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1570 | if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq))) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1571 | return ERR_PTR(-ECHILD); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1572 | |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1573 | *seqp = seq; |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1574 | status = d_revalidate(dentry, nd->flags); |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 1575 | if (likely(status > 0)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1576 | return dentry; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 1577 | if (unlazy_child(nd, dentry, seq)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1578 | return ERR_PTR(-ECHILD); |
Al Viro | 209a7fb | 2017-01-09 01:35:39 -0500 | [diff] [blame] | 1579 | if (unlikely(status == -ECHILD)) |
| 1580 | /* we'd been told to redo it in non-rcu mode */ |
| 1581 | status = d_revalidate(dentry, nd->flags); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1582 | } else { |
Al Viro | e97cdc8 | 2013-01-24 18:16:00 -0500 | [diff] [blame] | 1583 | dentry = __d_lookup(parent, &nd->last); |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1584 | if (unlikely(!dentry)) |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1585 | return NULL; |
Al Viro | a89f833 | 2017-01-09 22:25:28 -0500 | [diff] [blame] | 1586 | status = d_revalidate(dentry, nd->flags); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1587 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1588 | if (unlikely(status <= 0)) { |
Al Viro | e9742b5 | 2016-03-05 22:04:59 -0500 | [diff] [blame] | 1589 | if (!status) |
Al Viro | 5d0f49c | 2016-03-05 21:32:53 -0500 | [diff] [blame] | 1590 | d_invalidate(dentry); |
Eric W. Biederman | 5542aa2 | 2014-02-13 09:46:25 -0800 | [diff] [blame] | 1591 | dput(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1592 | return ERR_PTR(status); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1593 | } |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1594 | return dentry; |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | /* Fast lookup failed, do it the slow way */ |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1598 | static struct dentry *__lookup_slow(const struct qstr *name, |
| 1599 | struct dentry *dir, |
| 1600 | unsigned int flags) |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1601 | { |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1602 | struct dentry *dentry, *old; |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1603 | struct inode *inode = dir->d_inode; |
Al Viro | d9171b9 | 2016-04-15 03:33:13 -0400 | [diff] [blame] | 1604 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1605 | |
Al Viro | 1936386 | 2016-04-14 19:33:34 -0400 | [diff] [blame] | 1606 | /* Don't go there if it's already dead */ |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1607 | if (unlikely(IS_DEADDIR(inode))) |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1608 | return ERR_PTR(-ENOENT); |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1609 | again: |
Al Viro | d9171b9 | 2016-04-15 03:33:13 -0400 | [diff] [blame] | 1610 | dentry = d_alloc_parallel(dir, name, &wq); |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1611 | if (IS_ERR(dentry)) |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1612 | return dentry; |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1613 | if (unlikely(!d_in_lookup(dentry))) { |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1614 | int error = d_revalidate(dentry, flags); |
| 1615 | if (unlikely(error <= 0)) { |
| 1616 | if (!error) { |
| 1617 | d_invalidate(dentry); |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1618 | dput(dentry); |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1619 | goto again; |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1620 | } |
Al Viro | c64cd6e | 2020-01-10 17:17:19 -0500 | [diff] [blame] | 1621 | dput(dentry); |
| 1622 | dentry = ERR_PTR(error); |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1623 | } |
Al Viro | 94bdd65 | 2016-04-15 02:42:04 -0400 | [diff] [blame] | 1624 | } else { |
| 1625 | old = inode->i_op->lookup(inode, dentry, flags); |
| 1626 | d_lookup_done(dentry); |
| 1627 | if (unlikely(old)) { |
| 1628 | dput(dentry); |
| 1629 | dentry = old; |
Al Viro | 949a852 | 2016-03-06 14:20:52 -0500 | [diff] [blame] | 1630 | } |
| 1631 | } |
Al Viro | e3c1392 | 2016-03-06 14:03:27 -0500 | [diff] [blame] | 1632 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Al Viro | 88d8331 | 2018-04-06 16:43:47 -0400 | [diff] [blame] | 1635 | static struct dentry *lookup_slow(const struct qstr *name, |
| 1636 | struct dentry *dir, |
| 1637 | unsigned int flags) |
| 1638 | { |
| 1639 | struct inode *inode = dir->d_inode; |
| 1640 | struct dentry *res; |
| 1641 | inode_lock_shared(inode); |
| 1642 | res = __lookup_slow(name, dir, flags); |
| 1643 | inode_unlock_shared(inode); |
| 1644 | return res; |
| 1645 | } |
| 1646 | |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1647 | static inline int may_lookup(struct nameidata *nd) |
| 1648 | { |
| 1649 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | 4ad5abb | 2011-06-20 19:57:03 -0400 | [diff] [blame] | 1650 | int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1651 | if (err != -ECHILD) |
| 1652 | return err; |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 1653 | if (unlazy_walk(nd)) |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1654 | return -ECHILD; |
| 1655 | } |
Al Viro | 4ad5abb | 2011-06-20 19:57:03 -0400 | [diff] [blame] | 1656 | return inode_permission(nd->inode, MAY_EXEC); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1657 | } |
| 1658 | |
Al Viro | 9856fa1 | 2011-03-04 14:22:06 -0500 | [diff] [blame] | 1659 | static inline int handle_dots(struct nameidata *nd, int type) |
| 1660 | { |
| 1661 | if (type == LAST_DOTDOT) { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 1662 | int error = 0; |
| 1663 | |
| 1664 | if (!nd->root.mnt) { |
| 1665 | error = set_root(nd); |
| 1666 | if (error) |
| 1667 | return error; |
| 1668 | } |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 1669 | if (nd->flags & LOOKUP_RCU) |
| 1670 | error = follow_dotdot_rcu(nd); |
| 1671 | else |
| 1672 | error = follow_dotdot(nd); |
| 1673 | if (error) |
| 1674 | return error; |
| 1675 | |
| 1676 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
| 1677 | /* |
| 1678 | * If there was a racing rename or mount along our |
| 1679 | * path, then we can't be sure that ".." hasn't jumped |
| 1680 | * above nd->root (and so userspace should retry or use |
| 1681 | * some fallback). |
| 1682 | */ |
| 1683 | smp_rmb(); |
| 1684 | if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))) |
| 1685 | return -EAGAIN; |
| 1686 | if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))) |
| 1687 | return -EAGAIN; |
| 1688 | } |
Al Viro | 9856fa1 | 2011-03-04 14:22:06 -0500 | [diff] [blame] | 1689 | } |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1693 | enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; |
| 1694 | |
Al Viro | 06708ad | 2020-01-14 14:26:57 -0500 | [diff] [blame] | 1695 | static const char *pick_link(struct nameidata *nd, struct path *link, |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1696 | struct inode *inode, unsigned seq, int flags) |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1697 | { |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 1698 | struct saved *last; |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1699 | const char *res; |
| 1700 | int error; |
| 1701 | |
NeilBrown | 756daf2 | 2015-03-23 13:37:38 +1100 | [diff] [blame] | 1702 | if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) { |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1703 | path_to_nameidata(link, nd); |
Al Viro | 06708ad | 2020-01-14 14:26:57 -0500 | [diff] [blame] | 1704 | return ERR_PTR(-ELOOP); |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1705 | } |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 1706 | if (!(nd->flags & LOOKUP_RCU)) { |
Al Viro | 7973387 | 2015-05-09 12:55:43 -0400 | [diff] [blame] | 1707 | if (link->mnt == nd->path.mnt) |
| 1708 | mntget(link->mnt); |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1709 | } |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1710 | error = nd_alloc_stack(nd); |
| 1711 | if (unlikely(error)) { |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 1712 | if (error == -ECHILD) { |
Al Viro | ad1633a | 2017-01-08 22:35:31 -0500 | [diff] [blame] | 1713 | if (unlikely(!legitimize_path(nd, link, seq))) { |
| 1714 | drop_links(nd); |
| 1715 | nd->depth = 0; |
| 1716 | nd->flags &= ~LOOKUP_RCU; |
| 1717 | nd->path.mnt = NULL; |
| 1718 | nd->path.dentry = NULL; |
Al Viro | ad1633a | 2017-01-08 22:35:31 -0500 | [diff] [blame] | 1719 | rcu_read_unlock(); |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 1720 | } else if (likely(unlazy_walk(nd)) == 0) |
Al Viro | ad1633a | 2017-01-08 22:35:31 -0500 | [diff] [blame] | 1721 | error = nd_alloc_stack(nd); |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 1722 | } |
| 1723 | if (error) { |
| 1724 | path_put(link); |
Al Viro | 06708ad | 2020-01-14 14:26:57 -0500 | [diff] [blame] | 1725 | return ERR_PTR(error); |
Al Viro | bc40aee | 2015-05-09 13:04:24 -0400 | [diff] [blame] | 1726 | } |
Al Viro | 626de99 | 2015-05-04 18:26:59 -0400 | [diff] [blame] | 1727 | } |
| 1728 | |
Al Viro | ab10492 | 2015-05-10 11:50:01 -0400 | [diff] [blame] | 1729 | last = nd->stack + nd->depth++; |
Al Viro | 1cf2665 | 2015-05-06 16:01:56 -0400 | [diff] [blame] | 1730 | last->link = *link; |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 1731 | clear_delayed_call(&last->done); |
Al Viro | 0450b2d | 2015-05-08 13:23:53 -0400 | [diff] [blame] | 1732 | last->seq = seq; |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1733 | |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1734 | if (flags & WALK_TRAILING) { |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1735 | error = may_follow_link(nd, inode); |
| 1736 | if (unlikely(error)) |
| 1737 | return ERR_PTR(error); |
| 1738 | } |
| 1739 | |
| 1740 | if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS)) |
| 1741 | return ERR_PTR(-ELOOP); |
| 1742 | |
| 1743 | if (!(nd->flags & LOOKUP_RCU)) { |
| 1744 | touch_atime(&last->link); |
| 1745 | cond_resched(); |
| 1746 | } else if (atime_needs_update(&last->link, inode)) { |
| 1747 | if (unlikely(unlazy_walk(nd))) |
| 1748 | return ERR_PTR(-ECHILD); |
| 1749 | touch_atime(&last->link); |
| 1750 | } |
| 1751 | |
| 1752 | error = security_inode_follow_link(link->dentry, inode, |
| 1753 | nd->flags & LOOKUP_RCU); |
| 1754 | if (unlikely(error)) |
| 1755 | return ERR_PTR(error); |
| 1756 | |
Al Viro | ad6cc4c | 2020-01-14 14:41:39 -0500 | [diff] [blame] | 1757 | res = READ_ONCE(inode->i_link); |
| 1758 | if (!res) { |
| 1759 | const char * (*get)(struct dentry *, struct inode *, |
| 1760 | struct delayed_call *); |
| 1761 | get = inode->i_op->get_link; |
| 1762 | if (nd->flags & LOOKUP_RCU) { |
| 1763 | res = get(NULL, inode, &last->done); |
| 1764 | if (res == ERR_PTR(-ECHILD)) { |
| 1765 | if (unlikely(unlazy_walk(nd))) |
| 1766 | return ERR_PTR(-ECHILD); |
| 1767 | res = get(link->dentry, inode, &last->done); |
| 1768 | } |
| 1769 | } else { |
| 1770 | res = get(link->dentry, inode, &last->done); |
| 1771 | } |
| 1772 | if (!res) |
| 1773 | goto all_done; |
| 1774 | if (IS_ERR(res)) |
| 1775 | return res; |
| 1776 | } |
| 1777 | if (*res == '/') { |
| 1778 | error = nd_jump_root(nd); |
| 1779 | if (unlikely(error)) |
| 1780 | return ERR_PTR(error); |
| 1781 | while (unlikely(*++res == '/')) |
| 1782 | ; |
| 1783 | } |
| 1784 | if (*res) |
| 1785 | return res; |
| 1786 | all_done: // pure jump |
| 1787 | put_link(nd); |
| 1788 | return NULL; |
Al Viro | d63ff28 | 2015-05-04 18:13:23 -0400 | [diff] [blame] | 1789 | } |
| 1790 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1791 | /* |
| 1792 | * Do we need to follow links? We _really_ want to be able |
| 1793 | * to do this check without having to look at inode->i_op, |
| 1794 | * so we keep a cache of "no, this doesn't need follow_link" |
| 1795 | * for the common case. |
| 1796 | */ |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1797 | static const char *step_into(struct nameidata *nd, int flags, |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1798 | struct dentry *dentry, struct inode *inode, unsigned seq) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1799 | { |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1800 | struct path path; |
| 1801 | int err = handle_mounts(nd, dentry, &path, &inode, &seq); |
| 1802 | |
| 1803 | if (err < 0) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1804 | return ERR_PTR(err); |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1805 | if (likely(!d_is_symlink(path.dentry)) || |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 1806 | ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || |
Al Viro | aca2903 | 2020-01-09 15:17:57 -0500 | [diff] [blame] | 1807 | (flags & WALK_NOFOLLOW)) { |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1808 | /* not a symlink or should not follow */ |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1809 | path_to_nameidata(&path, nd); |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1810 | nd->inode = inode; |
| 1811 | nd->seq = seq; |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1812 | return NULL; |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 1813 | } |
Al Viro | a7f7754 | 2016-02-27 19:31:01 -0500 | [diff] [blame] | 1814 | /* make sure that d_is_symlink above matches inode */ |
| 1815 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | cbae4d1 | 2020-01-12 13:40:02 -0500 | [diff] [blame] | 1816 | if (read_seqcount_retry(&path.dentry->d_seq, seq)) |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1817 | return ERR_PTR(-ECHILD); |
Al Viro | a7f7754 | 2016-02-27 19:31:01 -0500 | [diff] [blame] | 1818 | } |
Al Viro | b1a8197 | 2020-01-19 12:48:44 -0500 | [diff] [blame] | 1819 | return pick_link(nd, &path, inode, seq, flags); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1822 | static const char *walk_component(struct nameidata *nd, int flags) |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1823 | { |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1824 | struct dentry *dentry; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1825 | struct inode *inode; |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 1826 | unsigned seq; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1827 | int err; |
| 1828 | /* |
| 1829 | * "." and ".." are special - ".." especially so because it has |
| 1830 | * to be able to know about the current root directory and |
| 1831 | * parent relationships. |
| 1832 | */ |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 1833 | if (unlikely(nd->last_type != LAST_NORM)) { |
Al Viro | 1c4ff1a | 2016-11-14 01:39:36 -0500 | [diff] [blame] | 1834 | if (!(flags & WALK_MORE) && nd->depth) |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 1835 | put_link(nd); |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 1836 | err = handle_dots(nd, nd->last_type); |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1837 | return ERR_PTR(err); |
Al Viro | 4693a54 | 2015-05-04 17:47:11 -0400 | [diff] [blame] | 1838 | } |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1839 | dentry = lookup_fast(nd, &inode, &seq); |
| 1840 | if (IS_ERR(dentry)) |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1841 | return ERR_CAST(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 1842 | if (unlikely(!dentry)) { |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 1843 | dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); |
| 1844 | if (IS_ERR(dentry)) |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 1845 | return ERR_CAST(dentry); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1846 | } |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 1847 | if (!(flags & WALK_MORE) && nd->depth) |
| 1848 | put_link(nd); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 1849 | return step_into(nd, flags, dentry, inode, seq); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1850 | } |
| 1851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | /* |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1853 | * We can do the critical dentry name comparison and hashing |
| 1854 | * operations one word at a time, but we are limited to: |
| 1855 | * |
| 1856 | * - Architectures with fast unaligned word accesses. We could |
| 1857 | * do a "get_unaligned()" if this helps and is sufficiently |
| 1858 | * fast. |
| 1859 | * |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1860 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
| 1861 | * do not trap on the (extremely unlikely) case of a page |
| 1862 | * crossing operation. |
| 1863 | * |
| 1864 | * - Furthermore, we need an efficient 64-bit compile for the |
| 1865 | * 64-bit case in order to generate the "number of bytes in |
| 1866 | * the final mask". Again, that could be replaced with a |
| 1867 | * efficient population count instruction or similar. |
| 1868 | */ |
| 1869 | #ifdef CONFIG_DCACHE_WORD_ACCESS |
| 1870 | |
Linus Torvalds | f68e556 | 2012-04-06 13:54:56 -0700 | [diff] [blame] | 1871 | #include <asm/word-at-a-time.h> |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1872 | |
George Spelvin | 468a942 | 2016-05-26 22:11:51 -0400 | [diff] [blame] | 1873 | #ifdef HASH_MIX |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1874 | |
George Spelvin | 468a942 | 2016-05-26 22:11:51 -0400 | [diff] [blame] | 1875 | /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ |
| 1876 | |
| 1877 | #elif defined(CONFIG_64BIT) |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1878 | /* |
| 1879 | * Register pressure in the mixing function is an issue, particularly |
| 1880 | * on 32-bit x86, but almost any function requires one state value and |
| 1881 | * one temporary. Instead, use a function designed for two state values |
| 1882 | * and no temporaries. |
| 1883 | * |
| 1884 | * This function cannot create a collision in only two iterations, so |
| 1885 | * we have two iterations to achieve avalanche. In those two iterations, |
| 1886 | * we have six layers of mixing, which is enough to spread one bit's |
| 1887 | * influence out to 2^6 = 64 state bits. |
| 1888 | * |
| 1889 | * Rotate constants are scored by considering either 64 one-bit input |
| 1890 | * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the |
| 1891 | * probability of that delta causing a change to each of the 128 output |
| 1892 | * bits, using a sample of random initial states. |
| 1893 | * |
| 1894 | * The Shannon entropy of the computed probabilities is then summed |
| 1895 | * to produce a score. Ideally, any input change has a 50% chance of |
| 1896 | * toggling any given output bit. |
| 1897 | * |
| 1898 | * Mixing scores (in bits) for (12,45): |
| 1899 | * Input delta: 1-bit 2-bit |
| 1900 | * 1 round: 713.3 42542.6 |
| 1901 | * 2 rounds: 2753.7 140389.8 |
| 1902 | * 3 rounds: 5954.1 233458.2 |
| 1903 | * 4 rounds: 7862.6 256672.2 |
| 1904 | * Perfect: 8192 258048 |
| 1905 | * (64*128) (64*63/2 * 128) |
| 1906 | */ |
| 1907 | #define HASH_MIX(x, y, a) \ |
| 1908 | ( x ^= (a), \ |
| 1909 | y ^= x, x = rol64(x,12),\ |
| 1910 | x += y, y = rol64(y,45),\ |
| 1911 | y *= 9 ) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1912 | |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1913 | /* |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1914 | * Fold two longs into one 32-bit hash value. This must be fast, but |
| 1915 | * latency isn't quite as critical, as there is a fair bit of additional |
| 1916 | * work done before the hash value is used. |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1917 | */ |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1918 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1919 | { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1920 | y ^= x * GOLDEN_RATIO_64; |
| 1921 | y *= GOLDEN_RATIO_64; |
| 1922 | return y >> 32; |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1923 | } |
| 1924 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1925 | #else /* 32-bit case */ |
| 1926 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1927 | /* |
| 1928 | * Mixing scores (in bits) for (7,20): |
| 1929 | * Input delta: 1-bit 2-bit |
| 1930 | * 1 round: 330.3 9201.6 |
| 1931 | * 2 rounds: 1246.4 25475.4 |
| 1932 | * 3 rounds: 1907.1 31295.1 |
| 1933 | * 4 rounds: 2042.3 31718.6 |
| 1934 | * Perfect: 2048 31744 |
| 1935 | * (32*64) (32*31/2 * 64) |
| 1936 | */ |
| 1937 | #define HASH_MIX(x, y, a) \ |
| 1938 | ( x ^= (a), \ |
| 1939 | y ^= x, x = rol32(x, 7),\ |
| 1940 | x += y, y = rol32(y,20),\ |
| 1941 | y *= 9 ) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1942 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1943 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1944 | { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1945 | /* Use arch-optimized multiply if one exists */ |
| 1946 | return __hash_32(y ^ __hash_32(x)); |
George Spelvin | 0fed3ac | 2016-05-02 06:31:01 -0400 | [diff] [blame] | 1947 | } |
| 1948 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1949 | #endif |
| 1950 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1951 | /* |
| 1952 | * Return the hash of a string of known length. This is carfully |
| 1953 | * designed to match hash_name(), which is the more critical function. |
| 1954 | * In particular, we must end by hashing a final word containing 0..7 |
| 1955 | * payload bytes, to match the way that hash_name() iterates until it |
| 1956 | * finds the delimiter after the name. |
| 1957 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1958 | 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] | 1959 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1960 | unsigned long a, x = 0, y = (unsigned long)salt; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1961 | |
| 1962 | for (;;) { |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1963 | if (!len) |
| 1964 | goto done; |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 1965 | a = load_unaligned_zeropad(name); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1966 | if (len < sizeof(unsigned long)) |
| 1967 | break; |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1968 | HASH_MIX(x, y, a); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1969 | name += sizeof(unsigned long); |
| 1970 | len -= sizeof(unsigned long); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1971 | } |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1972 | x ^= a & bytemask_from_count(len); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1973 | done: |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1974 | return fold_hash(x, y); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1975 | } |
| 1976 | EXPORT_SYMBOL(full_name_hash); |
| 1977 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1978 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1979 | u64 hashlen_string(const void *salt, const char *name) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1980 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1981 | unsigned long a = 0, x = 0, y = (unsigned long)salt; |
| 1982 | unsigned long adata, mask, len; |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1983 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
| 1984 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1985 | len = 0; |
| 1986 | goto inside; |
| 1987 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1988 | do { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1989 | HASH_MIX(x, y, a); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1990 | len += sizeof(unsigned long); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 1991 | inside: |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1992 | a = load_unaligned_zeropad(name+len); |
| 1993 | } while (!has_zero(a, &adata, &constants)); |
| 1994 | |
| 1995 | adata = prep_zero_mask(a, adata, &constants); |
| 1996 | mask = create_zero_mask(adata); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1997 | x ^= a & zero_bytemask(mask); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 1998 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 1999 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2000 | } |
| 2001 | EXPORT_SYMBOL(hashlen_string); |
| 2002 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2003 | /* |
| 2004 | * Calculate the length and hash of the path component, and |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2005 | * return the "hash_len" as the result. |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2006 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2007 | static inline u64 hash_name(const void *salt, const char *name) |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2008 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2009 | unsigned long a = 0, b, x = 0, y = (unsigned long)salt; |
| 2010 | unsigned long adata, bdata, mask, len; |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2011 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2012 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2013 | len = 0; |
| 2014 | goto inside; |
| 2015 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2016 | do { |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2017 | HASH_MIX(x, y, a); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2018 | len += sizeof(unsigned long); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2019 | inside: |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 2020 | a = load_unaligned_zeropad(name+len); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2021 | b = a ^ REPEAT_BYTE('/'); |
| 2022 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2023 | |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2024 | adata = prep_zero_mask(a, adata, &constants); |
| 2025 | bdata = prep_zero_mask(b, bdata, &constants); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2026 | mask = create_zero_mask(adata | bdata); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2027 | x ^= a & zero_bytemask(mask); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 2028 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2029 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2030 | } |
| 2031 | |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2032 | #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2033 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2034 | /* Return the hash of a string of known length */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2035 | 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] | 2036 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2037 | unsigned long hash = init_name_hash(salt); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2038 | while (len--) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2039 | hash = partial_name_hash((unsigned char)*name++, hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2040 | return end_name_hash(hash); |
| 2041 | } |
Linus Torvalds | ae942ae | 2012-03-02 19:40:57 -0800 | [diff] [blame] | 2042 | EXPORT_SYMBOL(full_name_hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2043 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2044 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2045 | u64 hashlen_string(const void *salt, const char *name) |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2046 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2047 | unsigned long hash = init_name_hash(salt); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2048 | unsigned long len = 0, c; |
| 2049 | |
| 2050 | c = (unsigned char)*name; |
George Spelvin | e0ab7af | 2016-05-29 08:05:56 -0400 | [diff] [blame] | 2051 | while (c) { |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2052 | len++; |
| 2053 | hash = partial_name_hash(c, hash); |
| 2054 | c = (unsigned char)name[len]; |
George Spelvin | e0ab7af | 2016-05-29 08:05:56 -0400 | [diff] [blame] | 2055 | } |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2056 | return hashlen_create(end_name_hash(hash), len); |
| 2057 | } |
George Spelvin | f2a031b | 2016-05-29 01:26:41 -0400 | [diff] [blame] | 2058 | EXPORT_SYMBOL(hashlen_string); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 2059 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 2060 | /* |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2061 | * We know there's a real path component here of at least |
| 2062 | * one character. |
| 2063 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2064 | static inline u64 hash_name(const void *salt, const char *name) |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2065 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2066 | unsigned long hash = init_name_hash(salt); |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2067 | unsigned long len = 0, c; |
| 2068 | |
| 2069 | c = (unsigned char)*name; |
| 2070 | do { |
| 2071 | len++; |
| 2072 | hash = partial_name_hash(c, hash); |
| 2073 | c = (unsigned char)name[len]; |
| 2074 | } while (c && c != '/'); |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2075 | return hashlen_create(end_name_hash(hash), len); |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2076 | } |
| 2077 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 2078 | #endif |
| 2079 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2080 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | * Name resolution. |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 2082 | * This is the basic name resolution function, turning a pathname into |
| 2083 | * the final dentry. We expect 'base' to be positive and a directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | * |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 2085 | * Returns 0 and nd will have valid dentry and mnt on success. |
| 2086 | * Returns error and drops reference to input namei data on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | */ |
Al Viro | 6de88d7 | 2009-08-09 01:41:57 +0400 | [diff] [blame] | 2088 | static int link_path_walk(const char *name, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | { |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2090 | int depth = 0; // depth <= nd->depth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | int err; |
Al Viro | 32cd746 | 2015-04-18 20:30:49 -0400 | [diff] [blame] | 2092 | |
Al Viro | b4c0353 | 2020-01-19 11:44:51 -0500 | [diff] [blame] | 2093 | nd->last_type = LAST_ROOT; |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2094 | nd->flags |= LOOKUP_PARENT; |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2095 | if (IS_ERR(name)) |
| 2096 | return PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | while (*name=='/') |
| 2098 | name++; |
| 2099 | if (!*name) |
Al Viro | 9e18f10 | 2015-04-18 20:44:34 -0400 | [diff] [blame] | 2100 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | /* At this point we know we have a real path component. */ |
| 2103 | for(;;) { |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2104 | const char *link; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2105 | u64 hash_len; |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2106 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 2108 | err = may_lookup(nd); |
George Spelvin | 2a18da7a | 2016-05-23 07:43:58 -0400 | [diff] [blame] | 2109 | if (err) |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2110 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 2112 | hash_len = hash_name(nd->path.dentry, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2114 | type = LAST_NORM; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2115 | if (name[0] == '.') switch (hashlen_len(hash_len)) { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2116 | case 2: |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2117 | if (name[1] == '.') { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2118 | type = LAST_DOTDOT; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2119 | nd->flags |= LOOKUP_JUMPED; |
| 2120 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2121 | break; |
| 2122 | case 1: |
| 2123 | type = LAST_DOT; |
| 2124 | } |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2125 | if (likely(type == LAST_NORM)) { |
| 2126 | struct dentry *parent = nd->path.dentry; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2127 | nd->flags &= ~LOOKUP_JUMPED; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2128 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
James Hogan | a060dc5 | 2014-09-16 13:07:35 +0100 | [diff] [blame] | 2129 | struct qstr this = { { .hash_len = hash_len }, .name = name }; |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 2130 | err = parent->d_op->d_hash(parent, &this); |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2131 | if (err < 0) |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2132 | return err; |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2133 | hash_len = this.hash_len; |
| 2134 | name = this.name; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2135 | } |
| 2136 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 2137 | |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2138 | nd->last.hash_len = hash_len; |
| 2139 | nd->last.name = name; |
Al Viro | 5f4a6a6 | 2013-01-24 18:04:22 -0500 | [diff] [blame] | 2140 | nd->last_type = type; |
| 2141 | |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2142 | name += hashlen_len(hash_len); |
| 2143 | if (!*name) |
Al Viro | bdf6cbf | 2015-04-18 20:21:40 -0400 | [diff] [blame] | 2144 | goto OK; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 2145 | /* |
| 2146 | * If it wasn't NUL, we know it was '/'. Skip that |
| 2147 | * slash, and continue until no more slashes. |
| 2148 | */ |
| 2149 | do { |
Linus Torvalds | d6bb3e9 | 2014-09-15 10:51:07 -0700 | [diff] [blame] | 2150 | name++; |
| 2151 | } while (unlikely(*name == '/')); |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2152 | if (unlikely(!*name)) { |
| 2153 | OK: |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2154 | /* pathname or trailing symlink, done */ |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2155 | if (!depth) { |
| 2156 | nd->flags &= ~LOOKUP_PARENT; |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2157 | return 0; |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2158 | } |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2159 | /* last component of nested symlink */ |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2160 | name = nd->stack[--depth].name; |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 2161 | link = walk_component(nd, 0); |
Al Viro | 1c4ff1a | 2016-11-14 01:39:36 -0500 | [diff] [blame] | 2162 | } else { |
| 2163 | /* not the last component */ |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 2164 | link = walk_component(nd, WALK_MORE); |
Al Viro | 8620c23 | 2015-05-04 08:58:35 -0400 | [diff] [blame] | 2165 | } |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2166 | if (unlikely(link)) { |
| 2167 | if (IS_ERR(link)) |
| 2168 | return PTR_ERR(link); |
| 2169 | /* a symlink to follow */ |
Al Viro | d8d4611 | 2020-02-23 22:04:15 -0500 | [diff] [blame] | 2170 | nd->stack[depth++].name = name; |
Al Viro | 92d2701 | 2020-01-14 13:24:17 -0500 | [diff] [blame] | 2171 | name = link; |
| 2172 | continue; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2173 | } |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2174 | if (unlikely(!d_can_lookup(nd->path.dentry))) { |
| 2175 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | 4675ac3 | 2017-01-09 22:29:15 -0500 | [diff] [blame] | 2176 | if (unlazy_walk(nd)) |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2177 | return -ECHILD; |
| 2178 | } |
Al Viro | 3595e23 | 2015-05-09 16:54:45 -0400 | [diff] [blame] | 2179 | return -ENOTDIR; |
Al Viro | 97242f9 | 2015-08-01 19:59:28 -0400 | [diff] [blame] | 2180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2184 | /* must be paired with terminate_walk() */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2185 | static const char *path_init(struct nameidata *nd, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 2187 | int error; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2188 | const char *s = nd->name->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | |
Linus Torvalds | c0eb027 | 2017-04-02 17:10:08 -0700 | [diff] [blame] | 2190 | if (!*s) |
| 2191 | flags &= ~LOOKUP_RCU; |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2192 | if (flags & LOOKUP_RCU) |
| 2193 | rcu_read_lock(); |
Linus Torvalds | c0eb027 | 2017-04-02 17:10:08 -0700 | [diff] [blame] | 2194 | |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2195 | nd->flags = flags | LOOKUP_JUMPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | nd->depth = 0; |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 2197 | |
| 2198 | nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); |
| 2199 | nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); |
| 2200 | smp_rmb(); |
| 2201 | |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2202 | if (flags & LOOKUP_ROOT) { |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2203 | struct dentry *root = nd->root.dentry; |
| 2204 | struct inode *inode = root->d_inode; |
Al Viro | 9389386 | 2017-04-15 17:29:14 -0400 | [diff] [blame] | 2205 | if (*s && unlikely(!d_can_lookup(root))) |
| 2206 | return ERR_PTR(-ENOTDIR); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2207 | nd->path = nd->root; |
| 2208 | nd->inode = inode; |
| 2209 | if (flags & LOOKUP_RCU) { |
Aleksa Sarai | ab87f9a | 2019-12-07 01:13:35 +1100 | [diff] [blame] | 2210 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | 8f47a016 | 2015-05-09 19:02:01 -0400 | [diff] [blame] | 2211 | nd->root_seq = nd->seq; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2212 | } else { |
| 2213 | path_get(&nd->path); |
| 2214 | } |
Al Viro | 368ee9b | 2015-05-08 17:19:59 -0400 | [diff] [blame] | 2215 | return s; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2216 | } |
| 2217 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 2218 | nd->root.mnt = NULL; |
Al Viro | 248fb5b | 2015-12-05 20:51:58 -0500 | [diff] [blame] | 2219 | nd->path.mnt = NULL; |
| 2220 | nd->path.dentry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2222 | /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ |
| 2223 | if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { |
Aleksa Sarai | 740a167 | 2019-12-07 01:13:29 +1100 | [diff] [blame] | 2224 | error = nd_jump_root(nd); |
| 2225 | if (unlikely(error)) |
| 2226 | return ERR_PTR(error); |
| 2227 | return s; |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | /* Relative pathname -- get the starting-point it is relative to. */ |
| 2231 | if (nd->dfd == AT_FDCWD) { |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2232 | if (flags & LOOKUP_RCU) { |
| 2233 | struct fs_struct *fs = current->fs; |
| 2234 | unsigned seq; |
| 2235 | |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2236 | do { |
| 2237 | seq = read_seqcount_begin(&fs->seq); |
| 2238 | nd->path = fs->pwd; |
Al Viro | ef55d91 | 2015-12-05 20:25:06 -0500 | [diff] [blame] | 2239 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2240 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
| 2241 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 2242 | } else { |
| 2243 | get_fs_pwd(current->fs, &nd->path); |
Al Viro | ef55d91 | 2015-12-05 20:25:06 -0500 | [diff] [blame] | 2244 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2245 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2246 | } else { |
Jeff Layton | 582aa64 | 2012-12-11 08:56:16 -0500 | [diff] [blame] | 2247 | /* Caller must check execute permissions on the starting path component */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2248 | struct fd f = fdget_raw(nd->dfd); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2249 | struct dentry *dentry; |
| 2250 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2251 | if (!f.file) |
Al Viro | 368ee9b | 2015-05-08 17:19:59 -0400 | [diff] [blame] | 2252 | return ERR_PTR(-EBADF); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2253 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2254 | dentry = f.file->f_path.dentry; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2255 | |
Al Viro | edc2b1d | 2018-07-09 16:27:23 -0400 | [diff] [blame] | 2256 | if (*s && unlikely(!d_can_lookup(dentry))) { |
| 2257 | fdput(f); |
| 2258 | return ERR_PTR(-ENOTDIR); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 2259 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2260 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2261 | nd->path = f.file->f_path; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2262 | if (flags & LOOKUP_RCU) { |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2263 | nd->inode = nd->path.dentry->d_inode; |
| 2264 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2265 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 2266 | path_get(&nd->path); |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2267 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 2268 | } |
Al Viro | 34a26b9 | 2015-05-11 08:05:05 -0400 | [diff] [blame] | 2269 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | } |
Aleksa Sarai | 8db52c7 | 2019-12-07 01:13:34 +1100 | [diff] [blame] | 2271 | |
Aleksa Sarai | adb21d2 | 2019-12-07 01:13:33 +1100 | [diff] [blame] | 2272 | /* For scoped-lookups we need to set the root to the dirfd as well. */ |
| 2273 | if (flags & LOOKUP_IS_SCOPED) { |
| 2274 | nd->root = nd->path; |
| 2275 | if (flags & LOOKUP_RCU) { |
| 2276 | nd->root_seq = nd->seq; |
| 2277 | } else { |
| 2278 | path_get(&nd->root); |
| 2279 | nd->flags |= LOOKUP_ROOT_GRABBED; |
| 2280 | } |
| 2281 | } |
| 2282 | return s; |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2283 | } |
| 2284 | |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 2285 | static inline const char *lookup_last(struct nameidata *nd) |
Al Viro | 95fa25d | 2015-04-22 13:46:57 -0400 | [diff] [blame] | 2286 | { |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2287 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) |
| 2288 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 2289 | |
Al Viro | c108837 | 2020-03-05 15:48:44 -0500 | [diff] [blame] | 2290 | return walk_component(nd, WALK_TRAILING); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2291 | } |
| 2292 | |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2293 | static int handle_lookup_down(struct nameidata *nd) |
| 2294 | { |
Al Viro | c153007 | 2020-01-09 14:50:18 -0500 | [diff] [blame] | 2295 | if (!(nd->flags & LOOKUP_RCU)) |
Al Viro | db3c9ad | 2020-01-09 14:41:00 -0500 | [diff] [blame] | 2296 | dget(nd->path.dentry); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 2297 | return PTR_ERR(step_into(nd, WALK_NOFOLLOW, |
| 2298 | nd->path.dentry, nd->inode, nd->seq)); |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2299 | } |
| 2300 | |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2301 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2302 | static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2303 | { |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2304 | const char *s = path_init(nd, flags); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2305 | int err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2306 | |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2307 | if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2308 | err = handle_lookup_down(nd); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 2309 | if (unlikely(err < 0)) |
| 2310 | s = ERR_PTR(err); |
Al Viro | 4f757f3 | 2017-04-15 17:31:22 -0400 | [diff] [blame] | 2311 | } |
| 2312 | |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 2313 | while (!(err = link_path_walk(s, nd)) && |
| 2314 | (s = lookup_last(nd)) != NULL) |
| 2315 | ; |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 2316 | if (!err) |
| 2317 | err = complete_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2318 | |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 2319 | if (!err && nd->flags & LOOKUP_DIRECTORY) |
| 2320 | if (!d_can_lookup(nd->path.dentry)) |
Al Viro | bd23a53 | 2011-03-23 09:56:30 -0400 | [diff] [blame] | 2321 | err = -ENOTDIR; |
Al Viro | 161aff1 | 2020-01-11 22:52:26 -0500 | [diff] [blame] | 2322 | if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { |
| 2323 | err = handle_lookup_down(nd); |
| 2324 | nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please... |
| 2325 | } |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 2326 | if (!err) { |
| 2327 | *path = nd->path; |
| 2328 | nd->path.mnt = NULL; |
| 2329 | nd->path.dentry = NULL; |
| 2330 | } |
| 2331 | terminate_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2332 | return err; |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2333 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2334 | |
David Howells | 31d921c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 2335 | int filename_lookup(int dfd, struct filename *name, unsigned flags, |
| 2336 | struct path *path, struct path *root) |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2337 | { |
Al Viro | 894bc8c | 2015-05-02 07:16:16 -0400 | [diff] [blame] | 2338 | int retval; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2339 | struct nameidata nd; |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2340 | if (IS_ERR(name)) |
| 2341 | return PTR_ERR(name); |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2342 | if (unlikely(root)) { |
| 2343 | nd.root = *root; |
| 2344 | flags |= LOOKUP_ROOT; |
| 2345 | } |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2346 | set_nameidata(&nd, dfd, name); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2347 | retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2348 | if (unlikely(retval == -ECHILD)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2349 | retval = path_lookupat(&nd, flags, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2350 | if (unlikely(retval == -ESTALE)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2351 | retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2352 | |
| 2353 | if (likely(!retval)) |
Al Viro | 161aff1 | 2020-01-11 22:52:26 -0500 | [diff] [blame] | 2354 | audit_inode(name, path->dentry, |
| 2355 | flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2356 | restore_nameidata(); |
Al Viro | e4bd1c1 | 2015-05-12 16:40:39 -0400 | [diff] [blame] | 2357 | putname(name); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2358 | return retval; |
| 2359 | } |
| 2360 | |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2361 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2362 | static int path_parentat(struct nameidata *nd, unsigned flags, |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2363 | struct path *parent) |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2364 | { |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2365 | const char *s = path_init(nd, flags); |
Al Viro | 9b5858e | 2018-07-09 16:33:23 -0400 | [diff] [blame] | 2366 | int err = link_path_walk(s, nd); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2367 | if (!err) |
| 2368 | err = complete_walk(nd); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2369 | if (!err) { |
| 2370 | *parent = nd->path; |
| 2371 | nd->path.mnt = NULL; |
| 2372 | nd->path.dentry = NULL; |
| 2373 | } |
| 2374 | terminate_walk(nd); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2375 | return err; |
| 2376 | } |
| 2377 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2378 | static struct filename *filename_parentat(int dfd, struct filename *name, |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2379 | unsigned int flags, struct path *parent, |
| 2380 | struct qstr *last, int *type) |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2381 | { |
| 2382 | int retval; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2383 | struct nameidata nd; |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2384 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2385 | if (IS_ERR(name)) |
| 2386 | return name; |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2387 | set_nameidata(&nd, dfd, name); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2388 | retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2389 | if (unlikely(retval == -ECHILD)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2390 | retval = path_parentat(&nd, flags, parent); |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2391 | if (unlikely(retval == -ESTALE)) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 2392 | retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2393 | if (likely(!retval)) { |
| 2394 | *last = nd.last; |
| 2395 | *type = nd.last_type; |
Al Viro | c9b07ea | 2019-07-14 13:22:27 -0400 | [diff] [blame] | 2396 | audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2397 | } else { |
| 2398 | putname(name); |
| 2399 | name = ERR_PTR(retval); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2400 | } |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 2401 | restore_nameidata(); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2402 | return name; |
Al Viro | 8bcb77f | 2015-05-08 16:59:20 -0400 | [diff] [blame] | 2403 | } |
| 2404 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2405 | /* does lookup, returns the object with parent locked */ |
| 2406 | struct dentry *kern_path_locked(const char *name, struct path *path) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2407 | { |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2408 | struct filename *filename; |
| 2409 | struct dentry *d; |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2410 | struct qstr last; |
| 2411 | int type; |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2412 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2413 | filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path, |
| 2414 | &last, &type); |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2415 | if (IS_ERR(filename)) |
| 2416 | return ERR_CAST(filename); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2417 | if (unlikely(type != LAST_NORM)) { |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2418 | path_put(path); |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 2419 | putname(filename); |
| 2420 | return ERR_PTR(-EINVAL); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2421 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2422 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2423 | d = __lookup_hash(&last, path->dentry, 0); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2424 | if (IS_ERR(d)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2425 | inode_unlock(path->dentry->d_inode); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 2426 | path_put(path); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2427 | } |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 2428 | putname(filename); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2429 | return d; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2430 | } |
| 2431 | |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2432 | int kern_path(const char *name, unsigned int flags, struct path *path) |
| 2433 | { |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2434 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
| 2435 | flags, path, NULL); |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2436 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2437 | EXPORT_SYMBOL(kern_path); |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2438 | |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2439 | /** |
| 2440 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair |
| 2441 | * @dentry: pointer to dentry of the base directory |
| 2442 | * @mnt: pointer to vfs mount of the base directory |
| 2443 | * @name: pointer to file name |
| 2444 | * @flags: lookup flags |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2445 | * @path: pointer to struct path to fill |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2446 | */ |
| 2447 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, |
| 2448 | const char *name, unsigned int flags, |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2449 | struct path *path) |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2450 | { |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2451 | struct path root = {.mnt = mnt, .dentry = dentry}; |
Al Viro | 9ad1aaa | 2015-05-12 16:44:39 -0400 | [diff] [blame] | 2452 | /* the first argument of filename_lookup() is ignored with root */ |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2453 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
| 2454 | flags , path, &root); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2455 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2456 | EXPORT_SYMBOL(vfs_path_lookup); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2457 | |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2458 | static int lookup_one_len_common(const char *name, struct dentry *base, |
| 2459 | int len, struct qstr *this) |
| 2460 | { |
| 2461 | this->name = name; |
| 2462 | this->len = len; |
| 2463 | this->hash = full_name_hash(base, name, len); |
| 2464 | if (!len) |
| 2465 | return -EACCES; |
| 2466 | |
| 2467 | if (unlikely(name[0] == '.')) { |
| 2468 | if (len < 2 || (len == 2 && name[1] == '.')) |
| 2469 | return -EACCES; |
| 2470 | } |
| 2471 | |
| 2472 | while (len--) { |
| 2473 | unsigned int c = *(const unsigned char *)name++; |
| 2474 | if (c == '/' || c == '\0') |
| 2475 | return -EACCES; |
| 2476 | } |
| 2477 | /* |
| 2478 | * See if the low-level filesystem might want |
| 2479 | * to use its own hash.. |
| 2480 | */ |
| 2481 | if (base->d_flags & DCACHE_OP_HASH) { |
| 2482 | int err = base->d_op->d_hash(base, this); |
| 2483 | if (err < 0) |
| 2484 | return err; |
| 2485 | } |
| 2486 | |
| 2487 | return inode_permission(base->d_inode, MAY_EXEC); |
| 2488 | } |
| 2489 | |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2490 | /** |
David Howells | 0da0b7f | 2018-06-15 15:19:22 +0100 | [diff] [blame] | 2491 | * try_lookup_one_len - filesystem helper to lookup single pathname component |
| 2492 | * @name: pathname component to lookup |
| 2493 | * @base: base directory to lookup from |
| 2494 | * @len: maximum length @len should be interpreted to |
| 2495 | * |
| 2496 | * Look up a dentry by name in the dcache, returning NULL if it does not |
| 2497 | * currently exist. The function does not try to create a dentry. |
| 2498 | * |
| 2499 | * Note that this routine is purely a helper for filesystem usage and should |
| 2500 | * not be called by generic code. |
| 2501 | * |
| 2502 | * The caller must hold base->i_mutex. |
| 2503 | */ |
| 2504 | struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) |
| 2505 | { |
| 2506 | struct qstr this; |
| 2507 | int err; |
| 2508 | |
| 2509 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
| 2510 | |
| 2511 | err = lookup_one_len_common(name, base, len, &this); |
| 2512 | if (err) |
| 2513 | return ERR_PTR(err); |
| 2514 | |
| 2515 | return lookup_dcache(&this, base, 0); |
| 2516 | } |
| 2517 | EXPORT_SYMBOL(try_lookup_one_len); |
| 2518 | |
| 2519 | /** |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2520 | * lookup_one_len - filesystem helper to lookup single pathname component |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2521 | * @name: pathname component to lookup |
| 2522 | * @base: base directory to lookup from |
| 2523 | * @len: maximum length @len should be interpreted to |
| 2524 | * |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2525 | * 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] | 2526 | * not be called by generic code. |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2527 | * |
| 2528 | * The caller must hold base->i_mutex. |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2529 | */ |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2530 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) |
| 2531 | { |
Al Viro | 8613a20 | 2018-04-06 16:45:33 -0400 | [diff] [blame] | 2532 | struct dentry *dentry; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2533 | struct qstr this; |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2534 | int err; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2535 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2536 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2537 | |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2538 | err = lookup_one_len_common(name, base, len, &this); |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2539 | if (err) |
| 2540 | return ERR_PTR(err); |
| 2541 | |
Al Viro | 8613a20 | 2018-04-06 16:45:33 -0400 | [diff] [blame] | 2542 | dentry = lookup_dcache(&this, base, 0); |
| 2543 | return dentry ? dentry : __lookup_slow(&this, base, 0); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2544 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2545 | EXPORT_SYMBOL(lookup_one_len); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2546 | |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2547 | /** |
| 2548 | * lookup_one_len_unlocked - filesystem helper to lookup single pathname component |
| 2549 | * @name: pathname component to lookup |
| 2550 | * @base: base directory to lookup from |
| 2551 | * @len: maximum length @len should be interpreted to |
| 2552 | * |
| 2553 | * Note that this routine is purely a helper for filesystem usage and should |
| 2554 | * not be called by generic code. |
| 2555 | * |
| 2556 | * Unlike lookup_one_len, it should be called without the parent |
| 2557 | * i_mutex held, and will take the i_mutex itself if necessary. |
| 2558 | */ |
| 2559 | struct dentry *lookup_one_len_unlocked(const char *name, |
| 2560 | struct dentry *base, int len) |
| 2561 | { |
| 2562 | struct qstr this; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2563 | int err; |
Linus Torvalds | 20d00ee | 2016-07-29 12:17:52 -0700 | [diff] [blame] | 2564 | struct dentry *ret; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2565 | |
Al Viro | 3c95f0d | 2018-04-06 16:32:38 -0400 | [diff] [blame] | 2566 | err = lookup_one_len_common(name, base, len, &this); |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2567 | if (err) |
| 2568 | return ERR_PTR(err); |
| 2569 | |
Linus Torvalds | 20d00ee | 2016-07-29 12:17:52 -0700 | [diff] [blame] | 2570 | ret = lookup_dcache(&this, base, 0); |
| 2571 | if (!ret) |
| 2572 | ret = lookup_slow(&this, base, 0); |
| 2573 | return ret; |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 2574 | } |
| 2575 | EXPORT_SYMBOL(lookup_one_len_unlocked); |
| 2576 | |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 2577 | /* |
| 2578 | * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) |
| 2579 | * on negatives. Returns known positive or ERR_PTR(); that's what |
| 2580 | * most of the users want. Note that pinned negative with unlocked parent |
| 2581 | * _can_ become positive at any time, so callers of lookup_one_len_unlocked() |
| 2582 | * need to be very careful; pinned positives have ->d_inode stable, so |
| 2583 | * this one avoids such problems. |
| 2584 | */ |
| 2585 | struct dentry *lookup_positive_unlocked(const char *name, |
| 2586 | struct dentry *base, int len) |
| 2587 | { |
| 2588 | struct dentry *ret = lookup_one_len_unlocked(name, base, len); |
Al Viro | 2fa6b1e | 2019-11-12 16:13:06 -0500 | [diff] [blame] | 2589 | 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] | 2590 | dput(ret); |
| 2591 | ret = ERR_PTR(-ENOENT); |
| 2592 | } |
| 2593 | return ret; |
| 2594 | } |
| 2595 | EXPORT_SYMBOL(lookup_positive_unlocked); |
| 2596 | |
Eric W. Biederman | eedf265 | 2016-06-02 10:29:47 -0500 | [diff] [blame] | 2597 | #ifdef CONFIG_UNIX98_PTYS |
| 2598 | int path_pts(struct path *path) |
| 2599 | { |
| 2600 | /* Find something mounted on "pts" in the same directory as |
| 2601 | * the input path. |
| 2602 | */ |
| 2603 | struct dentry *child, *parent; |
| 2604 | struct qstr this; |
| 2605 | int ret; |
| 2606 | |
| 2607 | ret = path_parent_directory(path); |
| 2608 | if (ret) |
| 2609 | return ret; |
| 2610 | |
| 2611 | parent = path->dentry; |
| 2612 | this.name = "pts"; |
| 2613 | this.len = 3; |
| 2614 | child = d_hash_and_lookup(parent, &this); |
| 2615 | if (!child) |
| 2616 | return -ENOENT; |
| 2617 | |
| 2618 | path->dentry = child; |
| 2619 | dput(parent); |
| 2620 | follow_mount(path); |
| 2621 | return 0; |
| 2622 | } |
| 2623 | #endif |
| 2624 | |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2625 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
| 2626 | struct path *path, int *empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | { |
Al Viro | abc9f5b | 2015-05-12 16:53:42 -0400 | [diff] [blame] | 2628 | return filename_lookup(dfd, getname_flags(name, flags, empty), |
| 2629 | flags, path, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | } |
Al Viro | b853a16 | 2015-05-13 09:12:02 -0400 | [diff] [blame] | 2631 | EXPORT_SYMBOL(user_path_at_empty); |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2632 | |
Miklos Szeredi | cbdf35b | 2014-10-24 00:14:36 +0200 | [diff] [blame] | 2633 | int __check_sticky(struct inode *dir, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | { |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2635 | kuid_t fsuid = current_fsuid(); |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 2636 | |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2637 | if (uid_eq(inode->i_uid, fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | return 0; |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2639 | if (uid_eq(dir->i_uid, fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | return 0; |
Andy Lutomirski | 23adbe1 | 2014-06-10 12:45:42 -0700 | [diff] [blame] | 2641 | return !capable_wrt_inode_uidgid(inode, CAP_FOWNER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | } |
Miklos Szeredi | cbdf35b | 2014-10-24 00:14:36 +0200 | [diff] [blame] | 2643 | EXPORT_SYMBOL(__check_sticky); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | |
| 2645 | /* |
| 2646 | * Check whether we can remove a link victim from directory dir, check |
| 2647 | * whether the type of victim is right. |
| 2648 | * 1. We can't do it if dir is read-only (done in permission()) |
| 2649 | * 2. We should have write and exec permissions on dir |
| 2650 | * 3. We can't remove anything from append-only dir |
| 2651 | * 4. We can't do anything with immutable dir (done in permission()) |
| 2652 | * 5. If the sticky bit on dir is set we should either |
| 2653 | * a. be owner of dir, or |
| 2654 | * b. be owner of victim, or |
| 2655 | * c. have CAP_FOWNER capability |
| 2656 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 2657 | * links pointing to it. |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 2658 | * 7. If the victim has an unknown uid or gid we can't change the inode. |
| 2659 | * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 2660 | * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 2661 | * 10. We can't remove a root or mountpoint. |
| 2662 | * 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] | 2663 | * nfs_async_unlink(). |
| 2664 | */ |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2665 | static int may_delete(struct inode *dir, struct dentry *victim, bool isdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | { |
David Howells | 63afdfc | 2015-05-06 15:59:00 +0100 | [diff] [blame] | 2667 | struct inode *inode = d_backing_inode(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | int error; |
| 2669 | |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2670 | if (d_is_negative(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | return -ENOENT; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2672 | BUG_ON(!inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | |
| 2674 | BUG_ON(victim->d_parent->d_inode != dir); |
Eric W. Biederman | 593d1ce | 2017-09-14 12:07:32 -0500 | [diff] [blame] | 2675 | |
| 2676 | /* Inode writeback is not safe when the uid or gid are invalid. */ |
| 2677 | if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid)) |
| 2678 | return -EOVERFLOW; |
| 2679 | |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 2680 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 2682 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | if (error) |
| 2684 | return error; |
| 2685 | if (IS_APPEND(dir)) |
| 2686 | return -EPERM; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 2687 | |
| 2688 | if (check_sticky(dir, inode) || IS_APPEND(inode) || |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 2689 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | return -EPERM; |
| 2691 | if (isdir) { |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 2692 | if (!d_is_dir(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | return -ENOTDIR; |
| 2694 | if (IS_ROOT(victim)) |
| 2695 | return -EBUSY; |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 2696 | } else if (d_is_dir(victim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | return -EISDIR; |
| 2698 | if (IS_DEADDIR(dir)) |
| 2699 | return -ENOENT; |
| 2700 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 2701 | return -EBUSY; |
| 2702 | return 0; |
| 2703 | } |
| 2704 | |
| 2705 | /* Check whether we can create an object with dentry child in directory |
| 2706 | * dir. |
| 2707 | * 1. We can't do it if child already exists (open has special treatment for |
| 2708 | * this case, but since we are inlined it's OK) |
| 2709 | * 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] | 2710 | * 3. We can't do it if the fs can't represent the fsuid or fsgid. |
| 2711 | * 4. We should have write and exec permissions on dir |
| 2712 | * 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] | 2713 | */ |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 2714 | static inline int may_create(struct inode *dir, struct dentry *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | { |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2716 | struct user_namespace *s_user_ns; |
Jeff Layton | 14e972b | 2013-05-08 10:25:58 -0400 | [diff] [blame] | 2717 | audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | if (child->d_inode) |
| 2719 | return -EEXIST; |
| 2720 | if (IS_DEADDIR(dir)) |
| 2721 | return -ENOENT; |
Eric W. Biederman | 036d523 | 2016-07-01 12:52:06 -0500 | [diff] [blame] | 2722 | s_user_ns = dir->i_sb->s_user_ns; |
| 2723 | if (!kuid_has_mapping(s_user_ns, current_fsuid()) || |
| 2724 | !kgid_has_mapping(s_user_ns, current_fsgid())) |
| 2725 | return -EOVERFLOW; |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 2726 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | } |
| 2728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | /* |
| 2730 | * p1 and p2 should be directories on the same fs. |
| 2731 | */ |
| 2732 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) |
| 2733 | { |
| 2734 | struct dentry *p; |
| 2735 | |
| 2736 | if (p1 == p2) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2737 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | return NULL; |
| 2739 | } |
| 2740 | |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 2741 | mutex_lock(&p1->d_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2743 | p = d_ancestor(p2, p1); |
| 2744 | if (p) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2745 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); |
| 2746 | inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2747 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | } |
| 2749 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2750 | p = d_ancestor(p1, p2); |
| 2751 | if (p) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2752 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
| 2753 | inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2754 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | } |
| 2756 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2757 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
| 2758 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | return NULL; |
| 2760 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2761 | EXPORT_SYMBOL(lock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | |
| 2763 | void unlock_rename(struct dentry *p1, struct dentry *p2) |
| 2764 | { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2765 | inode_unlock(p1->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | if (p1 != p2) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2767 | inode_unlock(p2->d_inode); |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 2768 | mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | } |
| 2770 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2771 | EXPORT_SYMBOL(unlock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2773 | int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2774 | bool want_excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 2776 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | if (error) |
| 2778 | return error; |
| 2779 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 2780 | if (!dir->i_op->create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | return -EACCES; /* shouldn't it be ENOSYS? */ |
| 2782 | mode &= S_IALLUGO; |
| 2783 | mode |= S_IFREG; |
| 2784 | error = security_inode_create(dir, dentry, mode); |
| 2785 | if (error) |
| 2786 | return error; |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2787 | error = dir->i_op->create(dir, dentry, mode, want_excl); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 2788 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 2789 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | return error; |
| 2791 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 2792 | EXPORT_SYMBOL(vfs_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | |
Al Viro | 8e6c848 | 2017-12-01 17:12:45 -0500 | [diff] [blame] | 2794 | int vfs_mkobj(struct dentry *dentry, umode_t mode, |
| 2795 | int (*f)(struct dentry *, umode_t, void *), |
| 2796 | void *arg) |
| 2797 | { |
| 2798 | struct inode *dir = dentry->d_parent->d_inode; |
| 2799 | int error = may_create(dir, dentry); |
| 2800 | if (error) |
| 2801 | return error; |
| 2802 | |
| 2803 | mode &= S_IALLUGO; |
| 2804 | mode |= S_IFREG; |
| 2805 | error = security_inode_create(dir, dentry, mode); |
| 2806 | if (error) |
| 2807 | return error; |
| 2808 | error = f(dentry, mode, arg); |
| 2809 | if (!error) |
| 2810 | fsnotify_create(dir, dentry); |
| 2811 | return error; |
| 2812 | } |
| 2813 | EXPORT_SYMBOL(vfs_mkobj); |
| 2814 | |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 2815 | bool may_open_dev(const struct path *path) |
| 2816 | { |
| 2817 | return !(path->mnt->mnt_flags & MNT_NODEV) && |
| 2818 | !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); |
| 2819 | } |
| 2820 | |
Al Viro | f0bb5aa | 2016-11-20 20:27:12 -0500 | [diff] [blame] | 2821 | 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] | 2822 | { |
Christoph Hellwig | 3fb6419 | 2008-10-24 09:58:10 +0200 | [diff] [blame] | 2823 | struct dentry *dentry = path->dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | struct inode *inode = dentry->d_inode; |
| 2825 | int error; |
| 2826 | |
| 2827 | if (!inode) |
| 2828 | return -ENOENT; |
| 2829 | |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2830 | switch (inode->i_mode & S_IFMT) { |
| 2831 | case S_IFLNK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | return -ELOOP; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2833 | case S_IFDIR: |
| 2834 | if (acc_mode & MAY_WRITE) |
| 2835 | return -EISDIR; |
| 2836 | break; |
| 2837 | case S_IFBLK: |
| 2838 | case S_IFCHR: |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 2839 | if (!may_open_dev(path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | return -EACCES; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2841 | /*FALLTHRU*/ |
| 2842 | case S_IFIFO: |
| 2843 | case S_IFSOCK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | flag &= ~O_TRUNC; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2845 | break; |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 2846 | } |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 2847 | |
Al Viro | 62fb4a1 | 2015-12-26 22:33:24 -0500 | [diff] [blame] | 2848 | error = inode_permission(inode, MAY_OPEN | acc_mode); |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 2849 | if (error) |
| 2850 | return error; |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 2851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | /* |
| 2853 | * An append-only file must be opened in append mode for writing. |
| 2854 | */ |
| 2855 | if (IS_APPEND(inode)) { |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 2856 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2857 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | if (flag & O_TRUNC) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2859 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | /* O_NOATIME can only be set by the owner or superuser */ |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 2863 | if (flag & O_NOATIME && !inode_owner_or_capable(inode)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2864 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | |
J. Bruce Fields | f3c7691e | 2011-09-21 10:58:13 -0400 | [diff] [blame] | 2866 | return 0; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2869 | static int handle_truncate(struct file *filp) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2870 | { |
Al Viro | f0bb5aa | 2016-11-20 20:27:12 -0500 | [diff] [blame] | 2871 | const struct path *path = &filp->f_path; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2872 | struct inode *inode = path->dentry->d_inode; |
| 2873 | int error = get_write_access(inode); |
| 2874 | if (error) |
| 2875 | return error; |
| 2876 | /* |
| 2877 | * Refuse to truncate files with mandatory locks held on them. |
| 2878 | */ |
Jeff Layton | d7a0698 | 2014-03-10 09:54:15 -0400 | [diff] [blame] | 2879 | error = locks_verify_locked(filp); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2880 | if (!error) |
Tetsuo Handa | ea0d3ab | 2010-06-02 13:24:43 +0900 | [diff] [blame] | 2881 | error = security_path_truncate(path); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2882 | if (!error) { |
| 2883 | error = do_truncate(path->dentry, 0, |
| 2884 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2885 | filp); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2886 | } |
| 2887 | put_write_access(inode); |
Mimi Zohar | acd0c93 | 2009-09-04 13:08:46 -0400 | [diff] [blame] | 2888 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | } |
| 2890 | |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 2891 | static inline int open_to_namei_flags(int flag) |
| 2892 | { |
Al Viro | 8a5e929 | 2011-06-25 19:15:54 -0400 | [diff] [blame] | 2893 | if ((flag & O_ACCMODE) == 3) |
| 2894 | flag--; |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 2895 | return flag; |
| 2896 | } |
| 2897 | |
Al Viro | d360775 | 2016-03-25 15:21:09 -0400 | [diff] [blame] | 2898 | 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] | 2899 | { |
Seth Forshee | 1328c72 | 2017-01-26 14:33:46 -0600 | [diff] [blame] | 2900 | struct user_namespace *s_user_ns; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2901 | int error = security_path_mknod(dir, dentry, mode, 0); |
| 2902 | if (error) |
| 2903 | return error; |
| 2904 | |
Seth Forshee | 1328c72 | 2017-01-26 14:33:46 -0600 | [diff] [blame] | 2905 | s_user_ns = dir->dentry->d_sb->s_user_ns; |
| 2906 | if (!kuid_has_mapping(s_user_ns, current_fsuid()) || |
| 2907 | !kgid_has_mapping(s_user_ns, current_fsgid())) |
| 2908 | return -EOVERFLOW; |
| 2909 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2910 | error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); |
| 2911 | if (error) |
| 2912 | return error; |
| 2913 | |
| 2914 | return security_inode_create(dir->dentry->d_inode, dentry, mode); |
| 2915 | } |
| 2916 | |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2917 | /* |
| 2918 | * Attempt to atomically look up, create and open a file from a negative |
| 2919 | * dentry. |
| 2920 | * |
| 2921 | * Returns 0 if successful. The file will have been created and attached to |
| 2922 | * @file by the filesystem calling finish_open(). |
| 2923 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 2924 | * If the file was looked up only or didn't need creating, FMODE_OPENED won't |
| 2925 | * be set. The caller will need to perform the open themselves. @path will |
| 2926 | * 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] | 2927 | * |
| 2928 | * Returns an error code otherwise. |
| 2929 | */ |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 2930 | static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, |
| 2931 | struct file *file, |
| 2932 | const struct open_flags *op, |
| 2933 | int open_flag, umode_t mode) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2934 | { |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2935 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2936 | struct inode *dir = nd->path.dentry->d_inode; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2937 | int error; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2938 | |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 2939 | 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] | 2940 | open_flag &= ~O_TRUNC; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2941 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2942 | if (nd->flags & LOOKUP_DIRECTORY) |
| 2943 | open_flag |= O_DIRECTORY; |
| 2944 | |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2945 | file->f_path.dentry = DENTRY_NOT_SET; |
| 2946 | file->f_path.mnt = nd->path.mnt; |
Al Viro | 0fb1ea0 | 2016-04-27 14:13:10 -0400 | [diff] [blame] | 2947 | error = dir->i_op->atomic_open(dir, dentry, file, |
Al Viro | 44907d7 | 2018-06-08 13:32:02 -0400 | [diff] [blame] | 2948 | open_to_namei_flags(open_flag), mode); |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 2949 | d_lookup_done(dentry); |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 2950 | if (!error) { |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 2951 | if (file->f_mode & FMODE_OPENED) { |
Al Viro | 6fb968c | 2020-01-26 09:53:19 -0500 | [diff] [blame] | 2952 | if (unlikely(dentry != file->f_path.dentry)) { |
| 2953 | dput(dentry); |
| 2954 | dentry = dget(file->f_path.dentry); |
| 2955 | } |
Al Viro | 7be219b | 2020-01-26 10:02:29 -0500 | [diff] [blame] | 2956 | if (file->f_mode & FMODE_CREATED) |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 2957 | fsnotify_create(dir, dentry); |
Al Viro | 64e1ac4 | 2018-07-09 19:17:52 -0400 | [diff] [blame] | 2958 | } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2959 | error = -EIO; |
Al Viro | 03da633 | 2013-09-16 19:22:33 -0400 | [diff] [blame] | 2960 | } else { |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 2961 | if (file->f_path.dentry) { |
| 2962 | dput(dentry); |
| 2963 | dentry = file->f_path.dentry; |
Al Viro | 03da633 | 2013-09-16 19:22:33 -0400 | [diff] [blame] | 2964 | } |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 2965 | if (file->f_mode & FMODE_CREATED) |
Al Viro | 384f26e | 2016-04-28 02:03:55 -0400 | [diff] [blame] | 2966 | fsnotify_create(dir, dentry); |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 2967 | if (unlikely(d_is_negative(dentry))) |
Al Viro | a01e718 | 2016-06-07 21:53:51 -0400 | [diff] [blame] | 2968 | error = -ENOENT; |
Sage Weil | 62b2ce9 | 2012-08-15 13:30:12 -0700 | [diff] [blame] | 2969 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2970 | } |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 2971 | if (error) { |
| 2972 | dput(dentry); |
| 2973 | dentry = ERR_PTR(error); |
| 2974 | } |
| 2975 | return dentry; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2976 | } |
| 2977 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2978 | /* |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2979 | * Look up and maybe create and open the last component. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2980 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 2981 | * Must be called with parent locked (exclusive in O_CREAT case). |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2982 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 2983 | * Returns 0 on success, that is, if |
| 2984 | * the file was successfully atomically created (if necessary) and opened, or |
| 2985 | * the file was not completely opened at this time, though lookups and |
| 2986 | * creations were performed. |
| 2987 | * These case are distinguished by presence of FMODE_OPENED on file->f_mode. |
| 2988 | * In the latter case dentry returned in @path might be negative if O_CREAT |
| 2989 | * hadn't been specified. |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2990 | * |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 2991 | * An error code is returned on failure. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2992 | */ |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 2993 | static struct dentry *lookup_open(struct nameidata *nd, struct file *file, |
| 2994 | const struct open_flags *op, |
| 2995 | bool got_write) |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2996 | { |
| 2997 | struct dentry *dir = nd->path.dentry; |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2998 | struct inode *dir_inode = dir->d_inode; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 2999 | int open_flag = op->open_flag; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3000 | struct dentry *dentry; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3001 | int error, create_error = 0; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3002 | umode_t mode = op->mode; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3003 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3004 | |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3005 | if (unlikely(IS_DEADDIR(dir_inode))) |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3006 | return ERR_PTR(-ENOENT); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3007 | |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3008 | file->f_mode &= ~FMODE_CREATED; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3009 | dentry = d_lookup(dir, &nd->last); |
| 3010 | for (;;) { |
| 3011 | if (!dentry) { |
| 3012 | dentry = d_alloc_parallel(dir, &nd->last, &wq); |
| 3013 | if (IS_ERR(dentry)) |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3014 | return dentry; |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3015 | } |
| 3016 | if (d_in_lookup(dentry)) |
| 3017 | break; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3018 | |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3019 | error = d_revalidate(dentry, nd->flags); |
| 3020 | if (likely(error > 0)) |
| 3021 | break; |
| 3022 | if (error) |
| 3023 | goto out_dput; |
| 3024 | d_invalidate(dentry); |
| 3025 | dput(dentry); |
| 3026 | dentry = NULL; |
| 3027 | } |
| 3028 | if (dentry->d_inode) { |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 3029 | /* Cached positive dentry: will open in f_op->open */ |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3030 | return dentry; |
Al Viro | 6c51e51 | 2016-03-05 20:09:32 -0500 | [diff] [blame] | 3031 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3032 | |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3033 | /* |
| 3034 | * Checking write permission is tricky, bacuse we don't know if we are |
| 3035 | * going to actually need it: O_CREAT opens should work as long as the |
| 3036 | * file exists. But checking existence breaks atomicity. The trick is |
| 3037 | * to check access and if not granted clear O_CREAT from the flags. |
| 3038 | * |
| 3039 | * Another problem is returing the "right" error value (e.g. for an |
| 3040 | * O_EXCL open we want to return EEXIST not EROFS). |
| 3041 | */ |
| 3042 | if (open_flag & O_CREAT) { |
| 3043 | if (!IS_POSIXACL(dir->d_inode)) |
| 3044 | mode &= ~current_umask(); |
| 3045 | if (unlikely(!got_write)) { |
| 3046 | create_error = -EROFS; |
| 3047 | open_flag &= ~O_CREAT; |
| 3048 | if (open_flag & (O_EXCL | O_TRUNC)) |
| 3049 | goto no_open; |
| 3050 | /* No side effects, safe to clear O_CREAT */ |
| 3051 | } else { |
| 3052 | create_error = may_o_create(&nd->path, dentry, mode); |
| 3053 | if (create_error) { |
| 3054 | open_flag &= ~O_CREAT; |
| 3055 | if (open_flag & O_EXCL) |
| 3056 | goto no_open; |
| 3057 | } |
| 3058 | } |
| 3059 | } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) && |
| 3060 | unlikely(!got_write)) { |
| 3061 | /* |
| 3062 | * No O_CREATE -> atomicity not a requirement -> fall |
| 3063 | * back to lookup + open |
| 3064 | */ |
| 3065 | goto no_open; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3066 | } |
| 3067 | |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3068 | if (dir_inode->i_op->atomic_open) { |
Al Viro | 239eb98 | 2020-01-09 14:12:40 -0500 | [diff] [blame] | 3069 | dentry = atomic_open(nd, dentry, file, op, open_flag, mode); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3070 | if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) |
| 3071 | dentry = ERR_PTR(create_error); |
| 3072 | return dentry; |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3073 | } |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3074 | |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3075 | no_open: |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3076 | if (d_in_lookup(dentry)) { |
Al Viro | 12fa5e2 | 2016-04-28 11:19:43 -0400 | [diff] [blame] | 3077 | struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, |
| 3078 | nd->flags); |
Al Viro | 6fbd071 | 2016-04-28 11:50:59 -0400 | [diff] [blame] | 3079 | d_lookup_done(dentry); |
Al Viro | 12fa5e2 | 2016-04-28 11:19:43 -0400 | [diff] [blame] | 3080 | if (unlikely(res)) { |
| 3081 | if (IS_ERR(res)) { |
| 3082 | error = PTR_ERR(res); |
| 3083 | goto out_dput; |
| 3084 | } |
| 3085 | dput(dentry); |
| 3086 | dentry = res; |
| 3087 | } |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 3088 | } |
| 3089 | |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3090 | /* Negative dentry, just create the file */ |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3091 | if (!dentry->d_inode && (open_flag & O_CREAT)) { |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3092 | file->f_mode |= FMODE_CREATED; |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3093 | audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3094 | if (!dir_inode->i_op->create) { |
| 3095 | error = -EACCES; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3096 | goto out_dput; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3097 | } |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3098 | error = dir_inode->i_op->create(dir_inode, dentry, mode, |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3099 | open_flag & O_EXCL); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3100 | if (error) |
| 3101 | goto out_dput; |
Al Viro | ce8644f | 2016-04-26 14:17:56 -0400 | [diff] [blame] | 3102 | fsnotify_create(dir_inode, dentry); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3103 | } |
Al Viro | 1643b43 | 2016-04-27 19:14:10 -0400 | [diff] [blame] | 3104 | if (unlikely(create_error) && !dentry->d_inode) { |
| 3105 | error = create_error; |
| 3106 | goto out_dput; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3107 | } |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3108 | return dentry; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3109 | |
| 3110 | out_dput: |
| 3111 | dput(dentry); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3112 | return ERR_PTR(error); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 3113 | } |
| 3114 | |
| 3115 | /* |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3116 | * Handle the last step of open() |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3117 | */ |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3118 | static const char *do_last(struct nameidata *nd, |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3119 | struct file *file, const struct open_flags *op) |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3120 | { |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 3121 | struct dentry *dir = nd->path.dentry; |
Al Viro | 6404674 | 2020-02-01 16:26:45 +0000 | [diff] [blame] | 3122 | kuid_t dir_uid = nd->inode->i_uid; |
| 3123 | umode_t dir_mode = nd->inode->i_mode; |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 3124 | int open_flag = op->open_flag; |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame^] | 3125 | bool do_truncate; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3126 | bool got_write = false; |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame^] | 3127 | int acc_mode; |
Al Viro | 254cf58 | 2015-05-05 09:40:46 -0400 | [diff] [blame] | 3128 | unsigned seq; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 3129 | struct inode *inode; |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3130 | struct dentry *dentry; |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3131 | const char *res; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 3132 | int error; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3133 | |
Al Viro | c3e380b | 2011-02-23 13:39:45 -0500 | [diff] [blame] | 3134 | nd->flags |= op->intent; |
| 3135 | |
Al Viro | bc77daa | 2013-06-06 09:12:33 -0400 | [diff] [blame] | 3136 | if (nd->last_type != LAST_NORM) { |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 3137 | if (nd->depth) |
| 3138 | put_link(nd); |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3139 | error = handle_dots(nd, nd->last_type); |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 3140 | if (unlikely(error)) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3141 | return ERR_PTR(error); |
Miklos Szeredi | e83db16 | 2012-06-05 15:10:29 +0200 | [diff] [blame] | 3142 | goto finish_open; |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 3143 | } |
Al Viro | 67ee3ad | 2009-12-26 07:01:01 -0500 | [diff] [blame] | 3144 | |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 3145 | if (!(open_flag & O_CREAT)) { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3146 | if (nd->last.name[nd->last.len]) |
| 3147 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 3148 | /* we _can_ be in RCU mode here */ |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3149 | dentry = lookup_fast(nd, &inode, &seq); |
| 3150 | if (IS_ERR(dentry)) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3151 | return ERR_CAST(dentry); |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3152 | if (likely(dentry)) |
Miklos Szeredi | 7157486 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 3153 | goto finish_lookup; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 3154 | |
Miklos Szeredi | 7157486 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 3155 | BUG_ON(nd->inode != dir->d_inode); |
Al Viro | 6583fe2 | 2016-03-05 18:14:03 -0500 | [diff] [blame] | 3156 | BUG_ON(nd->flags & LOOKUP_RCU); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3157 | } else { |
| 3158 | /* create side of things */ |
| 3159 | /* |
| 3160 | * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED |
| 3161 | * has been cleared when we got to the last component we are |
| 3162 | * about to look up |
| 3163 | */ |
| 3164 | error = complete_walk(nd); |
Al Viro | e8bb73d | 2015-05-08 16:28:42 -0400 | [diff] [blame] | 3165 | if (error) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3166 | return ERR_PTR(error); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3167 | |
Al Viro | c9b07ea | 2019-07-14 13:22:27 -0400 | [diff] [blame] | 3168 | audit_inode(nd->name, dir, AUDIT_INODE_PARENT); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 3169 | /* trailing slashes? */ |
Al Viro | deb106c | 2015-05-08 18:05:21 -0400 | [diff] [blame] | 3170 | if (unlikely(nd->last.name[nd->last.len])) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3171 | return ERR_PTR(-EISDIR); |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3172 | } |
| 3173 | |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3174 | if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3175 | error = mnt_want_write(nd->path.mnt); |
| 3176 | if (!error) |
| 3177 | got_write = true; |
| 3178 | /* |
| 3179 | * do _not_ fail yet - we might not need that or fail with |
| 3180 | * a different error; let lookup_open() decide; we'll be |
| 3181 | * dropping this one anyway. |
| 3182 | */ |
| 3183 | } |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3184 | if (open_flag & O_CREAT) |
| 3185 | inode_lock(dir->d_inode); |
| 3186 | else |
| 3187 | inode_lock_shared(dir->d_inode); |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3188 | dentry = lookup_open(nd, file, op, got_write); |
Al Viro | 9cf843e | 2016-04-28 19:35:16 -0400 | [diff] [blame] | 3189 | if (open_flag & O_CREAT) |
| 3190 | inode_unlock(dir->d_inode); |
| 3191 | else |
| 3192 | inode_unlock_shared(dir->d_inode); |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 3193 | |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3194 | if (got_write) { |
| 3195 | mnt_drop_write(nd->path.mnt); |
| 3196 | got_write = false; |
Al Viro | da5ebf5 | 2020-01-09 14:25:14 -0500 | [diff] [blame] | 3197 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3198 | |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3199 | if (IS_ERR(dentry)) |
| 3200 | return ERR_CAST(dentry); |
| 3201 | |
Al Viro | 00a07c1 | 2018-07-09 19:30:20 -0400 | [diff] [blame] | 3202 | if (file->f_mode & FMODE_OPENED) { |
Al Viro | 76ae2a5 | 2015-05-12 18:44:32 -0400 | [diff] [blame] | 3203 | audit_inode(nd->name, file->f_path.dentry, 0); |
Al Viro | 7be219b | 2020-01-26 10:02:29 -0500 | [diff] [blame] | 3204 | dput(nd->path.dentry); |
| 3205 | nd->path.dentry = dentry; |
Al Viro | 3ad5615 | 2020-01-26 10:06:13 -0500 | [diff] [blame] | 3206 | goto finish_open_created; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 3207 | } |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3208 | |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 3209 | if (file->f_mode & FMODE_CREATED) { |
Al Viro | e73cabf | 2020-01-09 14:30:08 -0500 | [diff] [blame] | 3210 | dput(nd->path.dentry); |
| 3211 | nd->path.dentry = dentry; |
Miklos Szeredi | e83db16 | 2012-06-05 15:10:29 +0200 | [diff] [blame] | 3212 | goto finish_open_created; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3213 | } |
| 3214 | |
Al Viro | 20e3435 | 2020-01-09 14:58:31 -0500 | [diff] [blame] | 3215 | finish_lookup: |
Al Viro | 56676ec | 2020-03-10 21:54:54 -0400 | [diff] [blame] | 3216 | if (nd->depth) |
| 3217 | put_link(nd); |
Al Viro | 8c4efe2 | 2020-01-19 12:44:18 -0500 | [diff] [blame] | 3218 | res = step_into(nd, WALK_TRAILING, dentry, inode, seq); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3219 | if (unlikely(res)) { |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3220 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
Al Viro | b0417d2 | 2020-01-14 13:34:20 -0500 | [diff] [blame] | 3221 | return res; |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3222 | } |
Al Viro | 31d1726 | 2020-01-08 20:19:38 -0500 | [diff] [blame] | 3223 | |
| 3224 | if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) { |
| 3225 | audit_inode(nd->name, nd->path.dentry, 0); |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3226 | return ERR_PTR(-EEXIST); |
Al Viro | 31d1726 | 2020-01-08 20:19:38 -0500 | [diff] [blame] | 3227 | } |
Al Viro | bc77daa | 2013-06-06 09:12:33 -0400 | [diff] [blame] | 3228 | finish_open: |
Al Viro | 8f64fb1 | 2016-11-14 01:50:26 -0500 | [diff] [blame] | 3229 | /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 3230 | error = complete_walk(nd); |
Al Viro | fac7d19 | 2016-06-04 11:41:49 -0400 | [diff] [blame] | 3231 | if (error) |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3232 | return ERR_PTR(error); |
Al Viro | 76ae2a5 | 2015-05-12 18:44:32 -0400 | [diff] [blame] | 3233 | audit_inode(nd->name, nd->path.dentry, 0); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3234 | if (open_flag & O_CREAT) { |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3235 | if (d_is_dir(nd->path.dentry)) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3236 | return ERR_PTR(-EISDIR); |
Al Viro | d0cb501 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 3237 | error = may_create_in_sticky(dir_mode, dir_uid, |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3238 | d_backing_inode(nd->path.dentry)); |
| 3239 | if (unlikely(error)) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3240 | return ERR_PTR(error); |
Salvatore Mesoraca | 30aba66 | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 3241 | } |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 3242 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3243 | return ERR_PTR(-ENOTDIR); |
Al Viro | 6c0d46c | 2011-03-09 00:59:59 -0500 | [diff] [blame] | 3244 | |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3245 | finish_open_created: |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame^] | 3246 | do_truncate = false; |
| 3247 | acc_mode = op->acc_mode; |
Al Viro | 5a2d3ed | 2020-01-26 10:32:22 -0500 | [diff] [blame] | 3248 | if (file->f_mode & FMODE_CREATED) { |
| 3249 | /* Don't check for write permission, don't truncate */ |
| 3250 | open_flag &= ~O_TRUNC; |
Al Viro | 5a2d3ed | 2020-01-26 10:32:22 -0500 | [diff] [blame] | 3251 | acc_mode = 0; |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame^] | 3252 | } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3253 | error = mnt_want_write(nd->path.mnt); |
| 3254 | if (error) |
Al Viro | 59e96e6 | 2020-01-26 10:22:24 -0500 | [diff] [blame] | 3255 | return ERR_PTR(error); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame^] | 3256 | do_truncate = true; |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3257 | } |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3258 | error = may_open(&nd->path, acc_mode, open_flag); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame^] | 3259 | if (!error && !(file->f_mode & FMODE_OPENED)) |
Al Viro | 3ad5615 | 2020-01-26 10:06:13 -0500 | [diff] [blame] | 3260 | error = vfs_open(&nd->path, file); |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame^] | 3261 | if (!error) |
| 3262 | error = ima_file_check(file, op->acc_mode); |
| 3263 | if (!error && do_truncate) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3264 | error = handle_truncate(file); |
Al Viro | c80567c | 2016-02-27 19:17:33 -0500 | [diff] [blame] | 3265 | if (unlikely(error > 0)) { |
| 3266 | WARN_ON(1); |
| 3267 | error = -EINVAL; |
| 3268 | } |
Al Viro | 8795e7d | 2020-01-26 10:38:17 -0500 | [diff] [blame^] | 3269 | if (do_truncate) |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 3270 | mnt_drop_write(nd->path.mnt); |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3271 | return ERR_PTR(error); |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3272 | } |
| 3273 | |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3274 | struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag) |
| 3275 | { |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3276 | struct dentry *child = NULL; |
| 3277 | struct inode *dir = dentry->d_inode; |
| 3278 | struct inode *inode; |
| 3279 | int error; |
| 3280 | |
| 3281 | /* we want directory to be writable */ |
| 3282 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 3283 | if (error) |
| 3284 | goto out_err; |
| 3285 | error = -EOPNOTSUPP; |
| 3286 | if (!dir->i_op->tmpfile) |
| 3287 | goto out_err; |
| 3288 | error = -ENOMEM; |
David Howells | cdf0122 | 2017-07-04 17:25:22 +0100 | [diff] [blame] | 3289 | child = d_alloc(dentry, &slash_name); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3290 | if (unlikely(!child)) |
| 3291 | goto out_err; |
| 3292 | error = dir->i_op->tmpfile(dir, child, mode); |
| 3293 | if (error) |
| 3294 | goto out_err; |
| 3295 | error = -ENOENT; |
| 3296 | inode = child->d_inode; |
| 3297 | if (unlikely(!inode)) |
| 3298 | goto out_err; |
| 3299 | if (!(open_flag & O_EXCL)) { |
| 3300 | spin_lock(&inode->i_lock); |
| 3301 | inode->i_state |= I_LINKABLE; |
| 3302 | spin_unlock(&inode->i_lock); |
| 3303 | } |
Mimi Zohar | fdb2410 | 2019-01-22 14:06:49 -0600 | [diff] [blame] | 3304 | ima_post_create_tmpfile(inode); |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3305 | return child; |
| 3306 | |
| 3307 | out_err: |
| 3308 | dput(child); |
| 3309 | return ERR_PTR(error); |
| 3310 | } |
| 3311 | EXPORT_SYMBOL(vfs_tmpfile); |
| 3312 | |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3313 | static int do_tmpfile(struct nameidata *nd, unsigned flags, |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3314 | const struct open_flags *op, |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3315 | struct file *file) |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3316 | { |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3317 | struct dentry *child; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3318 | struct path path; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3319 | int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3320 | if (unlikely(error)) |
| 3321 | return error; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3322 | error = mnt_want_write(path.mnt); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3323 | if (unlikely(error)) |
| 3324 | goto out; |
Amir Goldstein | af7bd4d | 2017-01-17 06:34:52 +0200 | [diff] [blame] | 3325 | child = vfs_tmpfile(path.dentry, op->mode, op->open_flag); |
| 3326 | error = PTR_ERR(child); |
Hirofumi Nakagawa | 684e73b | 2017-09-26 03:21:26 +0900 | [diff] [blame] | 3327 | if (IS_ERR(child)) |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3328 | goto out2; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3329 | dput(path.dentry); |
| 3330 | path.dentry = child; |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3331 | audit_inode(nd->name, child, 0); |
Eric Rannaud | 69a91c2 | 2014-10-30 01:51:01 -0700 | [diff] [blame] | 3332 | /* Don't check for other permissions, the inode was just created */ |
Al Viro | 62fb4a1 | 2015-12-26 22:33:24 -0500 | [diff] [blame] | 3333 | error = may_open(&path, 0, op->open_flag); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3334 | if (error) |
| 3335 | goto out2; |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3336 | file->f_path.mnt = path.mnt; |
Al Viro | be12af3 | 2018-06-08 11:44:56 -0400 | [diff] [blame] | 3337 | error = finish_open(file, child, NULL); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3338 | out2: |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3339 | mnt_drop_write(path.mnt); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3340 | out: |
Al Viro | 625b6d1 | 2015-05-12 16:36:12 -0400 | [diff] [blame] | 3341 | path_put(&path); |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3342 | return error; |
| 3343 | } |
| 3344 | |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3345 | static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) |
| 3346 | { |
| 3347 | struct path path; |
| 3348 | int error = path_lookupat(nd, flags, &path); |
| 3349 | if (!error) { |
| 3350 | audit_inode(nd->name, path.dentry, 0); |
Al Viro | ae2bb29 | 2018-07-10 13:22:28 -0400 | [diff] [blame] | 3351 | error = vfs_open(&path, file); |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3352 | path_put(&path); |
| 3353 | } |
| 3354 | return error; |
| 3355 | } |
| 3356 | |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3357 | static struct file *path_openat(struct nameidata *nd, |
| 3358 | const struct open_flags *op, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | { |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3360 | struct file *file; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3361 | int error; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3362 | |
Al Viro | ea73ea7 | 2018-07-11 15:00:04 -0400 | [diff] [blame] | 3363 | file = alloc_empty_file(op->open_flag, current_cred()); |
Al Viro | 1afc99b | 2013-02-14 20:41:04 -0500 | [diff] [blame] | 3364 | if (IS_ERR(file)) |
| 3365 | return file; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3366 | |
Al Viro | bb458c6 | 2013-07-13 13:26:37 +0400 | [diff] [blame] | 3367 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
Al Viro | 3ec2eef | 2018-06-08 13:43:47 -0400 | [diff] [blame] | 3368 | error = do_tmpfile(nd, flags, op, file); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3369 | } else if (unlikely(file->f_flags & O_PATH)) { |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3370 | error = do_o_path(nd, flags, file); |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3371 | } else { |
| 3372 | const char *s = path_init(nd, flags); |
| 3373 | while (!(error = link_path_walk(s, nd)) && |
Al Viro | 1ccac62 | 2020-01-14 10:13:40 -0500 | [diff] [blame] | 3374 | (s = do_last(nd, file, op)) != NULL) |
| 3375 | ; |
Al Viro | 5f336e7 | 2018-07-09 16:38:06 -0400 | [diff] [blame] | 3376 | terminate_walk(nd); |
Al Viro | 6ac0870 | 2016-04-26 00:02:50 -0400 | [diff] [blame] | 3377 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3378 | if (likely(!error)) { |
Al Viro | aad888f | 2018-06-08 12:58:04 -0400 | [diff] [blame] | 3379 | if (likely(file->f_mode & FMODE_OPENED)) |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3380 | return file; |
| 3381 | WARN_ON(1); |
| 3382 | error = -EINVAL; |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 3383 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3384 | fput(file); |
| 3385 | if (error == -EOPENSTALE) { |
| 3386 | if (flags & LOOKUP_RCU) |
| 3387 | error = -ECHILD; |
| 3388 | else |
| 3389 | error = -ESTALE; |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3390 | } |
Al Viro | 7c1c01e | 2018-06-08 12:56:55 -0400 | [diff] [blame] | 3391 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | } |
| 3393 | |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3394 | struct file *do_filp_open(int dfd, struct filename *pathname, |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3395 | const struct open_flags *op) |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3396 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3397 | struct nameidata nd; |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3398 | int flags = op->lookup_flags; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3399 | struct file *filp; |
| 3400 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3401 | set_nameidata(&nd, dfd, pathname); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3402 | filp = path_openat(&nd, op, flags | LOOKUP_RCU); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3403 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3404 | filp = path_openat(&nd, op, flags); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3405 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3406 | filp = path_openat(&nd, op, flags | LOOKUP_REVAL); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3407 | restore_nameidata(); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3408 | return filp; |
| 3409 | } |
| 3410 | |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3411 | struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3412 | const char *name, const struct open_flags *op) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3413 | { |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3414 | struct nameidata nd; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3415 | struct file *file; |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3416 | struct filename *filename; |
Al Viro | f9652e1 | 2013-06-11 08:23:01 +0400 | [diff] [blame] | 3417 | int flags = op->lookup_flags | LOOKUP_ROOT; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3418 | |
| 3419 | nd.root.mnt = mnt; |
| 3420 | nd.root.dentry = dentry; |
| 3421 | |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3422 | if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3423 | return ERR_PTR(-ELOOP); |
| 3424 | |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3425 | filename = getname_kernel(name); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 3426 | if (IS_ERR(filename)) |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3427 | return ERR_CAST(filename); |
| 3428 | |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3429 | set_nameidata(&nd, -1, filename); |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3430 | file = path_openat(&nd, op, flags | LOOKUP_RCU); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3431 | if (unlikely(file == ERR_PTR(-ECHILD))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3432 | file = path_openat(&nd, op, flags); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3433 | if (unlikely(file == ERR_PTR(-ESTALE))) |
Al Viro | c8a53ee | 2015-05-12 18:43:07 -0400 | [diff] [blame] | 3434 | file = path_openat(&nd, op, flags | LOOKUP_REVAL); |
Al Viro | 9883d18 | 2015-05-13 07:28:08 -0400 | [diff] [blame] | 3435 | restore_nameidata(); |
Paul Moore | 5168910 | 2015-01-22 00:00:03 -0500 | [diff] [blame] | 3436 | putname(filename); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3437 | return file; |
| 3438 | } |
| 3439 | |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3440 | static struct dentry *filename_create(int dfd, struct filename *name, |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3441 | struct path *path, unsigned int lookup_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | { |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3443 | struct dentry *dentry = ERR_PTR(-EEXIST); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3444 | struct qstr last; |
| 3445 | int type; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3446 | int err2; |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3447 | int error; |
| 3448 | bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); |
| 3449 | |
| 3450 | /* |
| 3451 | * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any |
| 3452 | * other flags passed in are ignored! |
| 3453 | */ |
| 3454 | lookup_flags &= LOOKUP_REVAL; |
| 3455 | |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 3456 | name = filename_parentat(dfd, name, lookup_flags, path, &last, &type); |
| 3457 | if (IS_ERR(name)) |
| 3458 | return ERR_CAST(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3460 | /* |
| 3461 | * Yucky last component or no last component at all? |
| 3462 | * (foo/., foo/.., /////) |
| 3463 | */ |
Al Viro | 5c31b6c | 2015-05-12 17:32:54 -0400 | [diff] [blame] | 3464 | if (unlikely(type != LAST_NORM)) |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3465 | goto out; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3466 | |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3467 | /* 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] | 3468 | err2 = mnt_want_write(path->mnt); |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3469 | /* |
| 3470 | * Do the final lookup. |
| 3471 | */ |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3472 | lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3473 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3474 | dentry = __lookup_hash(&last, path->dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | if (IS_ERR(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3476 | goto unlock; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3477 | |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3478 | error = -EEXIST; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3479 | if (d_is_positive(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3480 | goto fail; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3481 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3482 | /* |
| 3483 | * Special case - lookup gave negative, but... we had foo/bar/ |
| 3484 | * From the vfs_mknod() POV we just have a negative dentry - |
| 3485 | * all is fine. Let's be bastards - you had / on the end, you've |
| 3486 | * been asking for (non-existent) directory. -ENOENT for you. |
| 3487 | */ |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3488 | if (unlikely(!is_dir && last.name[last.len])) { |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3489 | error = -ENOENT; |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3490 | goto fail; |
Al Viro | e9baf6e | 2008-05-15 04:49:12 -0400 | [diff] [blame] | 3491 | } |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3492 | if (unlikely(err2)) { |
| 3493 | error = err2; |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3494 | goto fail; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3495 | } |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3496 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | fail: |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3499 | dput(dentry); |
| 3500 | dentry = ERR_PTR(error); |
| 3501 | unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3502 | inode_unlock(path->dentry->d_inode); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3503 | if (!err2) |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3504 | mnt_drop_write(path->mnt); |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3505 | out: |
Al Viro | 391172c | 2015-05-09 11:19:16 -0400 | [diff] [blame] | 3506 | path_put(path); |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3507 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | return dentry; |
| 3509 | } |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3510 | |
| 3511 | struct dentry *kern_path_create(int dfd, const char *pathname, |
| 3512 | struct path *path, unsigned int lookup_flags) |
| 3513 | { |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3514 | return filename_create(dfd, getname_kernel(pathname), |
| 3515 | path, lookup_flags); |
Al Viro | fa14a0b | 2015-01-22 02:16:49 -0500 | [diff] [blame] | 3516 | } |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3517 | EXPORT_SYMBOL(kern_path_create); |
| 3518 | |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3519 | void done_path_create(struct path *path, struct dentry *dentry) |
| 3520 | { |
| 3521 | dput(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3522 | inode_unlock(path->dentry->d_inode); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3523 | mnt_drop_write(path->mnt); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3524 | path_put(path); |
| 3525 | } |
| 3526 | EXPORT_SYMBOL(done_path_create); |
| 3527 | |
Al Viro | 520ae68 | 2015-05-13 07:00:28 -0400 | [diff] [blame] | 3528 | inline struct dentry *user_path_create(int dfd, const char __user *pathname, |
Jeff Layton | 1ac12b4 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 3529 | struct path *path, unsigned int lookup_flags) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3530 | { |
Al Viro | 181c37b | 2015-05-12 17:21:25 -0400 | [diff] [blame] | 3531 | return filename_create(dfd, getname(pathname), path, lookup_flags); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3532 | } |
| 3533 | EXPORT_SYMBOL(user_path_create); |
| 3534 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 3535 | 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] | 3536 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3537 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | |
| 3539 | if (error) |
| 3540 | return error; |
| 3541 | |
Christian Brauner | 94f8200 | 2018-07-05 17:51:20 +0200 | [diff] [blame] | 3542 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | return -EPERM; |
| 3544 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3545 | if (!dir->i_op->mknod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | return -EPERM; |
| 3547 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 3548 | error = devcgroup_inode_mknod(mode, dev); |
| 3549 | if (error) |
| 3550 | return error; |
| 3551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | error = security_inode_mknod(dir, dentry, mode, dev); |
| 3553 | if (error) |
| 3554 | return error; |
| 3555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3556 | error = dir->i_op->mknod(dir, dentry, mode, dev); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3557 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3558 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | return error; |
| 3560 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3561 | EXPORT_SYMBOL(vfs_mknod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | |
Al Viro | f69aac0 | 2011-07-26 04:31:40 -0400 | [diff] [blame] | 3563 | static int may_mknod(umode_t mode) |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3564 | { |
| 3565 | switch (mode & S_IFMT) { |
| 3566 | case S_IFREG: |
| 3567 | case S_IFCHR: |
| 3568 | case S_IFBLK: |
| 3569 | case S_IFIFO: |
| 3570 | case S_IFSOCK: |
| 3571 | case 0: /* zero mode translates to S_IFREG */ |
| 3572 | return 0; |
| 3573 | case S_IFDIR: |
| 3574 | return -EPERM; |
| 3575 | default: |
| 3576 | return -EINVAL; |
| 3577 | } |
| 3578 | } |
| 3579 | |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3580 | long do_mknodat(int dfd, const char __user *filename, umode_t mode, |
| 3581 | unsigned int dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3583 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3584 | struct path path; |
| 3585 | int error; |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3586 | unsigned int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3587 | |
Al Viro | 8e4bfca | 2012-07-20 01:17:26 +0400 | [diff] [blame] | 3588 | error = may_mknod(mode); |
| 3589 | if (error) |
| 3590 | return error; |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3591 | retry: |
| 3592 | dentry = user_path_create(dfd, filename, &path, lookup_flags); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3593 | if (IS_ERR(dentry)) |
| 3594 | return PTR_ERR(dentry); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3595 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3596 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3597 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3598 | error = security_path_mknod(&path, dentry, mode, dev); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3599 | if (error) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3600 | goto out; |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3601 | switch (mode & S_IFMT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | case 0: case S_IFREG: |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 3603 | error = vfs_create(path.dentry->d_inode,dentry,mode,true); |
Mimi Zohar | 05d1a71 | 2016-02-29 19:52:05 -0500 | [diff] [blame] | 3604 | if (!error) |
| 3605 | ima_post_path_mknod(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | break; |
| 3607 | case S_IFCHR: case S_IFBLK: |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3608 | error = vfs_mknod(path.dentry->d_inode,dentry,mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | new_decode_dev(dev)); |
| 3610 | break; |
| 3611 | case S_IFIFO: case S_IFSOCK: |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3612 | error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | } |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3615 | out: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3616 | done_path_create(&path, dentry); |
Jeff Layton | 972567f | 2012-12-20 16:00:10 -0500 | [diff] [blame] | 3617 | if (retry_estale(error, lookup_flags)) { |
| 3618 | lookup_flags |= LOOKUP_REVAL; |
| 3619 | goto retry; |
| 3620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | return error; |
| 3622 | } |
| 3623 | |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3624 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
| 3625 | unsigned int, dev) |
| 3626 | { |
| 3627 | return do_mknodat(dfd, filename, mode, dev); |
| 3628 | } |
| 3629 | |
Al Viro | 8208a22 | 2011-07-25 17:32:17 -0400 | [diff] [blame] | 3630 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3631 | { |
Dominik Brodowski | 87c4e19 | 2018-03-11 11:34:50 +0100 | [diff] [blame] | 3632 | return do_mknodat(AT_FDCWD, filename, mode, dev); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3633 | } |
| 3634 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 3635 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3637 | int error = may_create(dir, dentry); |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3638 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3639 | |
| 3640 | if (error) |
| 3641 | return error; |
| 3642 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3643 | if (!dir->i_op->mkdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3644 | return -EPERM; |
| 3645 | |
| 3646 | mode &= (S_IRWXUGO|S_ISVTX); |
| 3647 | error = security_inode_mkdir(dir, dentry, mode); |
| 3648 | if (error) |
| 3649 | return error; |
| 3650 | |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3651 | if (max_links && dir->i_nlink >= max_links) |
| 3652 | return -EMLINK; |
| 3653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | error = dir->i_op->mkdir(dir, dentry, mode); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3655 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3656 | fsnotify_mkdir(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | return error; |
| 3658 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3659 | EXPORT_SYMBOL(vfs_mkdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3661 | long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | { |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3663 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3664 | struct path path; |
| 3665 | int error; |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3666 | unsigned int lookup_flags = LOOKUP_DIRECTORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3668 | retry: |
| 3669 | dentry = user_path_create(dfd, pathname, &path, lookup_flags); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3670 | if (IS_ERR(dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3671 | return PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3672 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3673 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3674 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3675 | error = security_path_mkdir(&path, dentry, mode); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3676 | if (!error) |
| 3677 | error = vfs_mkdir(path.dentry->d_inode, dentry, mode); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3678 | done_path_create(&path, dentry); |
Jeff Layton | b76d8b8 | 2012-12-20 16:04:09 -0500 | [diff] [blame] | 3679 | if (retry_estale(error, lookup_flags)) { |
| 3680 | lookup_flags |= LOOKUP_REVAL; |
| 3681 | goto retry; |
| 3682 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | return error; |
| 3684 | } |
| 3685 | |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3686 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
| 3687 | { |
| 3688 | return do_mkdirat(dfd, pathname, mode); |
| 3689 | } |
| 3690 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 3691 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3692 | { |
Dominik Brodowski | 0101db7 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3693 | return do_mkdirat(AT_FDCWD, pathname, mode); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3694 | } |
| 3695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3697 | { |
| 3698 | int error = may_delete(dir, dentry, 1); |
| 3699 | |
| 3700 | if (error) |
| 3701 | return error; |
| 3702 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3703 | if (!dir->i_op->rmdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | return -EPERM; |
| 3705 | |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3706 | dget(dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3707 | inode_lock(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3709 | error = -EBUSY; |
Eric W. Biederman | 7af1364 | 2013-10-04 19:15:13 -0700 | [diff] [blame] | 3710 | if (is_local_mountpoint(dentry)) |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3711 | goto out; |
| 3712 | |
| 3713 | error = security_inode_rmdir(dir, dentry); |
| 3714 | if (error) |
| 3715 | goto out; |
| 3716 | |
| 3717 | error = dir->i_op->rmdir(dir, dentry); |
| 3718 | if (error) |
| 3719 | goto out; |
| 3720 | |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 3721 | shrink_dcache_parent(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3722 | dentry->d_inode->i_flags |= S_DEAD; |
| 3723 | dont_mount(dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3724 | detach_mounts(dentry); |
Amir Goldstein | 116b973 | 2019-05-26 17:34:02 +0300 | [diff] [blame] | 3725 | fsnotify_rmdir(dir, dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3726 | |
| 3727 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3728 | inode_unlock(dentry->d_inode); |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3729 | dput(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3730 | if (!error) |
| 3731 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | return error; |
| 3733 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3734 | EXPORT_SYMBOL(vfs_rmdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | |
Dominik Brodowski | f459dffa | 2018-03-11 11:34:48 +0100 | [diff] [blame] | 3736 | long do_rmdir(int dfd, const char __user *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | { |
| 3738 | int error = 0; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3739 | struct filename *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3740 | struct dentry *dentry; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3741 | struct path path; |
| 3742 | struct qstr last; |
| 3743 | int type; |
Jeff Layton | c6ee920 | 2012-12-20 16:28:33 -0500 | [diff] [blame] | 3744 | unsigned int lookup_flags = 0; |
| 3745 | retry: |
Al Viro | c1d4dd2 | 2016-06-05 16:38:18 -0400 | [diff] [blame] | 3746 | name = filename_parentat(dfd, getname(pathname), lookup_flags, |
| 3747 | &path, &last, &type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3748 | if (IS_ERR(name)) |
| 3749 | return PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3751 | switch (type) { |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3752 | case LAST_DOTDOT: |
| 3753 | error = -ENOTEMPTY; |
| 3754 | goto exit1; |
| 3755 | case LAST_DOT: |
| 3756 | error = -EINVAL; |
| 3757 | goto exit1; |
| 3758 | case LAST_ROOT: |
| 3759 | error = -EBUSY; |
| 3760 | goto exit1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | } |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3762 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3763 | error = mnt_want_write(path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3764 | if (error) |
| 3765 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3766 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3767 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3768 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | error = PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3770 | if (IS_ERR(dentry)) |
| 3771 | goto exit2; |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 3772 | if (!dentry->d_inode) { |
| 3773 | error = -ENOENT; |
| 3774 | goto exit3; |
| 3775 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3776 | error = security_path_rmdir(&path, dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3777 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3778 | goto exit3; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3779 | error = vfs_rmdir(path.dentry->d_inode, dentry); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 3780 | exit3: |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3781 | dput(dentry); |
| 3782 | exit2: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3783 | inode_unlock(path.dentry->d_inode); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3784 | mnt_drop_write(path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3785 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3786 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | putname(name); |
Jeff Layton | c6ee920 | 2012-12-20 16:28:33 -0500 | [diff] [blame] | 3788 | if (retry_estale(error, lookup_flags)) { |
| 3789 | lookup_flags |= LOOKUP_REVAL; |
| 3790 | goto retry; |
| 3791 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3792 | return error; |
| 3793 | } |
| 3794 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 3795 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3796 | { |
| 3797 | return do_rmdir(AT_FDCWD, pathname); |
| 3798 | } |
| 3799 | |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3800 | /** |
| 3801 | * vfs_unlink - unlink a filesystem object |
| 3802 | * @dir: parent directory |
| 3803 | * @dentry: victim |
| 3804 | * @delegated_inode: returns victim inode, if the inode is delegated. |
| 3805 | * |
| 3806 | * The caller must hold dir->i_mutex. |
| 3807 | * |
| 3808 | * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and |
| 3809 | * return a reference to the inode in delegated_inode. The caller |
| 3810 | * should then break the delegation on that inode and retry. Because |
| 3811 | * breaking a delegation may take a long time, the caller should drop |
| 3812 | * dir->i_mutex before doing so. |
| 3813 | * |
| 3814 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 3815 | * be appropriate for callers that expect the underlying filesystem not |
| 3816 | * to be NFS exported. |
| 3817 | */ |
| 3818 | 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] | 3819 | { |
J. Bruce Fields | 9accbb9 | 2012-08-28 07:03:24 -0400 | [diff] [blame] | 3820 | struct inode *target = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | int error = may_delete(dir, dentry, 0); |
| 3822 | |
| 3823 | if (error) |
| 3824 | return error; |
| 3825 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3826 | if (!dir->i_op->unlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3827 | return -EPERM; |
| 3828 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3829 | inode_lock(target); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3830 | if (is_local_mountpoint(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | error = -EBUSY; |
| 3832 | else { |
| 3833 | error = security_inode_unlink(dir, dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3834 | if (!error) { |
J. Bruce Fields | 5a14696 | 2012-08-28 07:50:40 -0700 | [diff] [blame] | 3835 | error = try_break_deleg(target, delegated_inode); |
| 3836 | if (error) |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3837 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | error = dir->i_op->unlink(dir, dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3839 | if (!error) { |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 3840 | dont_mount(dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3841 | detach_mounts(dentry); |
Amir Goldstein | 116b973 | 2019-05-26 17:34:02 +0300 | [diff] [blame] | 3842 | fsnotify_unlink(dir, dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 3843 | } |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3844 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3845 | } |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3846 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3847 | inode_unlock(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | |
| 3849 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ |
| 3850 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { |
J. Bruce Fields | 9accbb9 | 2012-08-28 07:03:24 -0400 | [diff] [blame] | 3851 | fsnotify_link_count(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3852 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 3854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | return error; |
| 3856 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3857 | EXPORT_SYMBOL(vfs_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | |
| 3859 | /* |
| 3860 | * 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] | 3861 | * 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] | 3862 | * writeout happening, and we don't want to prevent access to the directory |
| 3863 | * while waiting on the I/O. |
| 3864 | */ |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3865 | long do_unlinkat(int dfd, struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3867 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | struct dentry *dentry; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3869 | struct path path; |
| 3870 | struct qstr last; |
| 3871 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | struct inode *inode = NULL; |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3873 | struct inode *delegated_inode = NULL; |
Jeff Layton | 5d18f81 | 2012-12-20 16:38:04 -0500 | [diff] [blame] | 3874 | unsigned int lookup_flags = 0; |
| 3875 | retry: |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3876 | name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3877 | if (IS_ERR(name)) |
| 3878 | return PTR_ERR(name); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3880 | error = -EISDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3881 | if (type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3883 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3884 | error = mnt_want_write(path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3885 | if (error) |
| 3886 | goto exit1; |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3887 | retry_deleg: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3888 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3889 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3890 | error = PTR_ERR(dentry); |
| 3891 | if (!IS_ERR(dentry)) { |
| 3892 | /* Why not before? Because we want correct error value */ |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3893 | if (last.name[last.len]) |
Török Edwin | 50338b8 | 2011-06-16 00:06:14 +0300 | [diff] [blame] | 3894 | goto slashes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | inode = dentry->d_inode; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3896 | if (d_is_negative(dentry)) |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 3897 | goto slashes; |
| 3898 | ihold(inode); |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3899 | error = security_path_unlink(&path, dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3900 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3901 | goto exit2; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3902 | error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3903 | exit2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | dput(dentry); |
| 3905 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3906 | inode_unlock(path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3907 | if (inode) |
| 3908 | iput(inode); /* truncate the inode here */ |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3909 | inode = NULL; |
| 3910 | if (delegated_inode) { |
J. Bruce Fields | 5a14696 | 2012-08-28 07:50:40 -0700 | [diff] [blame] | 3911 | error = break_deleg_wait(&delegated_inode); |
J. Bruce Fields | b21996e | 2011-09-20 09:14:34 -0400 | [diff] [blame] | 3912 | if (!error) |
| 3913 | goto retry_deleg; |
| 3914 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3915 | mnt_drop_write(path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 3917 | path_put(&path); |
Jeff Layton | 5d18f81 | 2012-12-20 16:38:04 -0500 | [diff] [blame] | 3918 | if (retry_estale(error, lookup_flags)) { |
| 3919 | lookup_flags |= LOOKUP_REVAL; |
| 3920 | inode = NULL; |
| 3921 | goto retry; |
| 3922 | } |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3923 | putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3924 | return error; |
| 3925 | |
| 3926 | slashes: |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3927 | if (d_is_negative(dentry)) |
| 3928 | error = -ENOENT; |
Miklos Szeredi | 44b1d53 | 2014-04-01 17:08:41 +0200 | [diff] [blame] | 3929 | else if (d_is_dir(dentry)) |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 3930 | error = -EISDIR; |
| 3931 | else |
| 3932 | error = -ENOTDIR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | goto exit2; |
| 3934 | } |
| 3935 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3936 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3937 | { |
| 3938 | if ((flag & ~AT_REMOVEDIR) != 0) |
| 3939 | return -EINVAL; |
| 3940 | |
| 3941 | if (flag & AT_REMOVEDIR) |
| 3942 | return do_rmdir(dfd, pathname); |
| 3943 | |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3944 | return do_unlinkat(dfd, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3945 | } |
| 3946 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 3947 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3948 | { |
Christoph Hellwig | da2f136 | 2017-11-04 13:44:45 +0300 | [diff] [blame] | 3949 | return do_unlinkat(AT_FDCWD, getname(pathname)); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3950 | } |
| 3951 | |
Miklos Szeredi | db2e747 | 2008-06-24 16:50:16 +0200 | [diff] [blame] | 3952 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3954 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | |
| 3956 | if (error) |
| 3957 | return error; |
| 3958 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3959 | if (!dir->i_op->symlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | return -EPERM; |
| 3961 | |
| 3962 | error = security_inode_symlink(dir, dentry, oldname); |
| 3963 | if (error) |
| 3964 | return error; |
| 3965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | error = dir->i_op->symlink(dir, dentry, oldname); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3967 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3968 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | return error; |
| 3970 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 3971 | EXPORT_SYMBOL(vfs_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 3973 | long do_symlinkat(const char __user *oldname, int newdfd, |
| 3974 | const char __user *newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3976 | int error; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3977 | struct filename *from; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3978 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3979 | struct path path; |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 3980 | unsigned int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | |
| 3982 | from = getname(oldname); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3983 | if (IS_ERR(from)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | return PTR_ERR(from); |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 3985 | retry: |
| 3986 | dentry = user_path_create(newdfd, newname, &path, lookup_flags); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3987 | error = PTR_ERR(dentry); |
| 3988 | if (IS_ERR(dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3989 | goto out_putname; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3990 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3991 | error = security_path_symlink(&path, dentry, from->name); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3992 | if (!error) |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3993 | error = vfs_symlink(path.dentry->d_inode, dentry, from->name); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3994 | done_path_create(&path, dentry); |
Jeff Layton | f46d356 | 2012-12-11 12:10:08 -0500 | [diff] [blame] | 3995 | if (retry_estale(error, lookup_flags)) { |
| 3996 | lookup_flags |= LOOKUP_REVAL; |
| 3997 | goto retry; |
| 3998 | } |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3999 | out_putname: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | putname(from); |
| 4001 | return error; |
| 4002 | } |
| 4003 | |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4004 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
| 4005 | int, newdfd, const char __user *, newname) |
| 4006 | { |
| 4007 | return do_symlinkat(oldname, newdfd, newname); |
| 4008 | } |
| 4009 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4010 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4011 | { |
Dominik Brodowski | b724e84 | 2018-03-11 11:34:49 +0100 | [diff] [blame] | 4012 | return do_symlinkat(oldname, AT_FDCWD, newname); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4013 | } |
| 4014 | |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4015 | /** |
| 4016 | * vfs_link - create a new link |
| 4017 | * @old_dentry: object to be linked |
| 4018 | * @dir: new parent |
| 4019 | * @new_dentry: where to create the new link |
| 4020 | * @delegated_inode: returns inode needing a delegation break |
| 4021 | * |
| 4022 | * The caller must hold dir->i_mutex |
| 4023 | * |
| 4024 | * If vfs_link discovers a delegation on the to-be-linked file in need |
| 4025 | * of breaking, it will return -EWOULDBLOCK and return a reference to the |
| 4026 | * inode in delegated_inode. The caller should then break the delegation |
| 4027 | * and retry. Because breaking a delegation may take a long time, the |
| 4028 | * caller should drop the i_mutex before doing so. |
| 4029 | * |
| 4030 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4031 | * be appropriate for callers that expect the underlying filesystem not |
| 4032 | * to be NFS exported. |
| 4033 | */ |
| 4034 | 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] | 4035 | { |
| 4036 | struct inode *inode = old_dentry->d_inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 4037 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | int error; |
| 4039 | |
| 4040 | if (!inode) |
| 4041 | return -ENOENT; |
| 4042 | |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 4043 | error = may_create(dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | if (error) |
| 4045 | return error; |
| 4046 | |
| 4047 | if (dir->i_sb != inode->i_sb) |
| 4048 | return -EXDEV; |
| 4049 | |
| 4050 | /* |
| 4051 | * A link to an append-only or immutable file cannot be created. |
| 4052 | */ |
| 4053 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 4054 | return -EPERM; |
Eric W. Biederman | 0bd23d09 | 2016-06-29 14:54:46 -0500 | [diff] [blame] | 4055 | /* |
| 4056 | * Updating the link count will likely cause i_uid and i_gid to |
| 4057 | * be writen back improperly if their true value is unknown to |
| 4058 | * the vfs. |
| 4059 | */ |
| 4060 | if (HAS_UNMAPPED_ID(inode)) |
| 4061 | return -EPERM; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 4062 | if (!dir->i_op->link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | return -EPERM; |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 4064 | if (S_ISDIR(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | return -EPERM; |
| 4066 | |
| 4067 | error = security_inode_link(old_dentry, dir, new_dentry); |
| 4068 | if (error) |
| 4069 | return error; |
| 4070 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4071 | inode_lock(inode); |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 4072 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
Al Viro | f4e0c30 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 4073 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 4074 | error = -ENOENT; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 4075 | else if (max_links && inode->i_nlink >= max_links) |
| 4076 | error = -EMLINK; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4077 | else { |
| 4078 | error = try_break_deleg(inode, delegated_inode); |
| 4079 | if (!error) |
| 4080 | error = dir->i_op->link(old_dentry, dir, new_dentry); |
| 4081 | } |
Al Viro | f4e0c30 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 4082 | |
| 4083 | if (!error && (inode->i_state & I_LINKABLE)) { |
| 4084 | spin_lock(&inode->i_lock); |
| 4085 | inode->i_state &= ~I_LINKABLE; |
| 4086 | spin_unlock(&inode->i_lock); |
| 4087 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4088 | inode_unlock(inode); |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 4089 | if (!error) |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 4090 | fsnotify_link(dir, inode, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | return error; |
| 4092 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4093 | EXPORT_SYMBOL(vfs_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | |
| 4095 | /* |
| 4096 | * Hardlinks are often used in delicate situations. We avoid |
| 4097 | * security-related surprises by not following symlinks on the |
| 4098 | * newname. --KAB |
| 4099 | * |
| 4100 | * We don't follow them on the oldname either to be compatible |
| 4101 | * with linux 2.0, and to avoid hard-linking to directories |
| 4102 | * and other special files. --ADM |
| 4103 | */ |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4104 | int do_linkat(int olddfd, const char __user *oldname, int newdfd, |
| 4105 | const char __user *newname, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | { |
| 4107 | struct dentry *new_dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4108 | struct path old_path, new_path; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4109 | struct inode *delegated_inode = NULL; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4110 | int how = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4113 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 4114 | return -EINVAL; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4115 | /* |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4116 | * To use null names we require CAP_DAC_READ_SEARCH |
| 4117 | * This ensures that not everyone will be able to create |
| 4118 | * handlink using the passed filedescriptor. |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4119 | */ |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4120 | if (flags & AT_EMPTY_PATH) { |
| 4121 | if (!capable(CAP_DAC_READ_SEARCH)) |
| 4122 | return -ENOENT; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4123 | how = LOOKUP_EMPTY; |
Linus Torvalds | f0cc6ff | 2013-08-28 09:18:05 -0700 | [diff] [blame] | 4124 | } |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 4125 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4126 | if (flags & AT_SYMLINK_FOLLOW) |
| 4127 | how |= LOOKUP_FOLLOW; |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4128 | retry: |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 4129 | error = user_path_at(olddfd, oldname, how, &old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | if (error) |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4131 | return error; |
| 4132 | |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4133 | new_dentry = user_path_create(newdfd, newname, &new_path, |
| 4134 | (how & LOOKUP_REVAL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | error = PTR_ERR(new_dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 4136 | if (IS_ERR(new_dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4137 | goto out; |
| 4138 | |
| 4139 | error = -EXDEV; |
| 4140 | if (old_path.mnt != new_path.mnt) |
| 4141 | goto out_dput; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 4142 | error = may_linkat(&old_path); |
| 4143 | if (unlikely(error)) |
| 4144 | goto out_dput; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 4145 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4146 | if (error) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 4147 | goto out_dput; |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4148 | 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] | 4149 | out_dput: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 4150 | done_path_create(&new_path, new_dentry); |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4151 | if (delegated_inode) { |
| 4152 | error = break_deleg_wait(&delegated_inode); |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4153 | if (!error) { |
| 4154 | path_put(&old_path); |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4155 | goto retry; |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4156 | } |
J. Bruce Fields | 146a859 | 2011-09-20 17:14:31 -0400 | [diff] [blame] | 4157 | } |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4158 | if (retry_estale(error, how)) { |
Oleg Drokin | d22e633 | 2014-01-31 15:41:58 -0500 | [diff] [blame] | 4159 | path_put(&old_path); |
Jeff Layton | 442e31c | 2012-12-20 16:15:38 -0500 | [diff] [blame] | 4160 | how |= LOOKUP_REVAL; |
| 4161 | goto retry; |
| 4162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | out: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 4164 | path_put(&old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | |
| 4166 | return error; |
| 4167 | } |
| 4168 | |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4169 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
| 4170 | int, newdfd, const char __user *, newname, int, flags) |
| 4171 | { |
| 4172 | return do_linkat(olddfd, oldname, newdfd, newname, flags); |
| 4173 | } |
| 4174 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 4175 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4176 | { |
Dominik Brodowski | 46ea89e | 2018-03-11 11:34:53 +0100 | [diff] [blame] | 4177 | return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4178 | } |
| 4179 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4180 | /** |
| 4181 | * vfs_rename - rename a filesystem object |
| 4182 | * @old_dir: parent of source |
| 4183 | * @old_dentry: source |
| 4184 | * @new_dir: parent of destination |
| 4185 | * @new_dentry: destination |
| 4186 | * @delegated_inode: returns an inode needing a delegation break |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4187 | * @flags: rename flags |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4188 | * |
| 4189 | * The caller must hold multiple mutexes--see lock_rename()). |
| 4190 | * |
| 4191 | * If vfs_rename discovers a delegation in need of breaking at either |
| 4192 | * the source or destination, it will return -EWOULDBLOCK and return a |
| 4193 | * reference to the inode in delegated_inode. The caller should then |
| 4194 | * break the delegation and retry. Because breaking a delegation may |
| 4195 | * take a long time, the caller should drop all locks before doing |
| 4196 | * so. |
| 4197 | * |
| 4198 | * Alternatively, a caller may pass NULL for delegated_inode. This may |
| 4199 | * be appropriate for callers that expect the underlying filesystem not |
| 4200 | * to be NFS exported. |
| 4201 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | * The worst of all namespace operations - renaming directory. "Perverted" |
| 4203 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... |
| 4204 | * Problems: |
Mauro Carvalho Chehab | 0117d42 | 2017-05-12 07:45:42 -0300 | [diff] [blame] | 4205 | * |
J. Bruce Fields | d03b29a | 2014-02-17 16:52:33 -0500 | [diff] [blame] | 4206 | * a) we can get into loop creation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | * b) race potential - two innocent renames can create a loop together. |
| 4208 | * 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] | 4209 | * 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] | 4210 | * story. |
J. Bruce Fields | 6cedba8 | 2012-03-05 11:40:41 -0500 | [diff] [blame] | 4211 | * c) we have to lock _four_ objects - parents and victim (if it exists), |
| 4212 | * and source (if it is not a directory). |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4213 | * 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] | 4214 | * whether the target exists). Solution: try to be smart with locking |
| 4215 | * 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] | 4216 | * 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] | 4217 | * move will be locked. Thus we can rank directories by the tree |
| 4218 | * (ancestors first) and rank all non-directories after them. |
| 4219 | * That works since everybody except rename does "lock parent, lookup, |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 4220 | * lock child" and rename is under ->s_vfs_rename_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
| 4222 | * has no more than 1 dentry. If "hybrid" objects will ever appear, |
| 4223 | * 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] | 4224 | * 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] | 4225 | * 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] | 4226 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 4227 | * ->i_mutex on parents, which works but leads to some truly excessive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4228 | * locking]. |
| 4229 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4231 | struct inode *new_dir, struct dentry *new_dentry, |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4232 | struct inode **delegated_inode, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | { |
| 4234 | int error; |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4235 | bool is_dir = d_is_dir(old_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4236 | struct inode *source = old_dentry->d_inode; |
| 4237 | struct inode *target = new_dentry->d_inode; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4238 | bool new_is_dir = false; |
| 4239 | unsigned max_links = new_dir->i_sb->s_max_links; |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4240 | struct name_snapshot old_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | |
Miklos Szeredi | 8d3e293 | 2016-12-16 11:02:54 +0100 | [diff] [blame] | 4242 | if (source == target) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4243 | return 0; |
| 4244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | error = may_delete(old_dir, old_dentry, is_dir); |
| 4246 | if (error) |
| 4247 | return error; |
| 4248 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4249 | if (!target) { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 4250 | error = may_create(new_dir, new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4251 | } else { |
| 4252 | new_is_dir = d_is_dir(new_dentry); |
| 4253 | |
| 4254 | if (!(flags & RENAME_EXCHANGE)) |
| 4255 | error = may_delete(new_dir, new_dentry, is_dir); |
| 4256 | else |
| 4257 | error = may_delete(new_dir, new_dentry, new_is_dir); |
| 4258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | if (error) |
| 4260 | return error; |
| 4261 | |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4262 | if (!old_dir->i_op->rename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | return -EPERM; |
| 4264 | |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4265 | /* |
| 4266 | * If we are going to change the parent - check write permissions, |
| 4267 | * we'll need to flip '..'. |
| 4268 | */ |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4269 | if (new_dir != old_dir) { |
| 4270 | if (is_dir) { |
| 4271 | error = inode_permission(source, MAY_WRITE); |
| 4272 | if (error) |
| 4273 | return error; |
| 4274 | } |
| 4275 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { |
| 4276 | error = inode_permission(target, MAY_WRITE); |
| 4277 | if (error) |
| 4278 | return error; |
| 4279 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4280 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4281 | |
Miklos Szeredi | 0b3974e | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4282 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, |
| 4283 | flags); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4284 | if (error) |
| 4285 | return error; |
| 4286 | |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4287 | take_dentry_name_snapshot(&old_name, old_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4288 | dget(new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4289 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4290 | lock_two_nondirectories(source, target); |
| 4291 | else if (target) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4292 | inode_lock(target); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4293 | |
| 4294 | error = -EBUSY; |
Eric W. Biederman | 7af1364 | 2013-10-04 19:15:13 -0700 | [diff] [blame] | 4295 | if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4296 | goto out; |
| 4297 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4298 | if (max_links && new_dir != old_dir) { |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4299 | error = -EMLINK; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4300 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4301 | goto out; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4302 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && |
| 4303 | old_dir->i_nlink >= max_links) |
| 4304 | goto out; |
| 4305 | } |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4306 | if (!is_dir) { |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4307 | error = try_break_deleg(source, delegated_inode); |
| 4308 | if (error) |
| 4309 | goto out; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4310 | } |
| 4311 | if (target && !new_is_dir) { |
| 4312 | error = try_break_deleg(target, delegated_inode); |
| 4313 | if (error) |
| 4314 | goto out; |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4315 | } |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4316 | error = old_dir->i_op->rename(old_dir, old_dentry, |
Miklos Szeredi | 18fc84d | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 4317 | new_dir, new_dentry, flags); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4318 | if (error) |
| 4319 | goto out; |
| 4320 | |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4321 | if (!(flags & RENAME_EXCHANGE) && target) { |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4322 | if (is_dir) { |
| 4323 | shrink_dcache_parent(new_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4324 | target->i_flags |= S_DEAD; |
Al Viro | 8767712 | 2018-05-27 16:23:51 -0400 | [diff] [blame] | 4325 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4326 | dont_mount(new_dentry); |
Eric W. Biederman | 8ed936b | 2013-10-01 18:33:48 -0700 | [diff] [blame] | 4327 | detach_mounts(new_dentry); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4328 | } |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4329 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { |
| 4330 | if (!(flags & RENAME_EXCHANGE)) |
| 4331 | d_move(old_dentry, new_dentry); |
| 4332 | else |
| 4333 | d_exchange(old_dentry, new_dentry); |
| 4334 | } |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4335 | out: |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4336 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4337 | unlock_two_nondirectories(source, target); |
| 4338 | else if (target) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4339 | inode_unlock(target); |
Miklos Szeredi | bc27027 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4340 | dput(new_dentry); |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4341 | if (!error) { |
Al Viro | f4ec3a3 | 2019-04-26 13:21:24 -0400 | [diff] [blame] | 4342 | fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4343 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); |
| 4344 | if (flags & RENAME_EXCHANGE) { |
Al Viro | f4ec3a3 | 2019-04-26 13:21:24 -0400 | [diff] [blame] | 4345 | fsnotify_move(new_dir, old_dir, &old_dentry->d_name, |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4346 | new_is_dir, NULL, new_dentry); |
| 4347 | } |
| 4348 | } |
Al Viro | 49d31c2 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 4349 | release_dentry_name_snapshot(&old_name); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 4350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | return error; |
| 4352 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4353 | EXPORT_SYMBOL(vfs_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4355 | static int do_renameat2(int olddfd, const char __user *oldname, int newdfd, |
| 4356 | const char __user *newname, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4357 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4358 | struct dentry *old_dentry, *new_dentry; |
| 4359 | struct dentry *trap; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4360 | struct path old_path, new_path; |
| 4361 | struct qstr old_last, new_last; |
| 4362 | int old_type, new_type; |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4363 | struct inode *delegated_inode = NULL; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4364 | struct filename *from; |
| 4365 | struct filename *to; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4366 | unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4367 | bool should_retry = false; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4368 | int error; |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4369 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4370 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4371 | return -EINVAL; |
| 4372 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4373 | if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && |
| 4374 | (flags & RENAME_EXCHANGE)) |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4375 | return -EINVAL; |
| 4376 | |
Miklos Szeredi | 0d7a855 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 4377 | if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD)) |
| 4378 | return -EPERM; |
| 4379 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4380 | if (flags & RENAME_EXCHANGE) |
| 4381 | target_flags = 0; |
| 4382 | |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4383 | retry: |
Al Viro | c1d4dd2 | 2016-06-05 16:38:18 -0400 | [diff] [blame] | 4384 | from = filename_parentat(olddfd, getname(oldname), lookup_flags, |
| 4385 | &old_path, &old_last, &old_type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4386 | if (IS_ERR(from)) { |
| 4387 | error = PTR_ERR(from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | goto exit; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | |
Al Viro | c1d4dd2 | 2016-06-05 16:38:18 -0400 | [diff] [blame] | 4391 | to = filename_parentat(newdfd, getname(newname), lookup_flags, |
| 4392 | &new_path, &new_last, &new_type); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4393 | if (IS_ERR(to)) { |
| 4394 | error = PTR_ERR(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | goto exit1; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | |
| 4398 | error = -EXDEV; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4399 | if (old_path.mnt != new_path.mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | goto exit2; |
| 4401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | error = -EBUSY; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4403 | if (old_type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | goto exit2; |
| 4405 | |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4406 | if (flags & RENAME_NOREPLACE) |
| 4407 | error = -EEXIST; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4408 | if (new_type != LAST_NORM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | goto exit2; |
| 4410 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4411 | error = mnt_want_write(old_path.mnt); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4412 | if (error) |
| 4413 | goto exit2; |
| 4414 | |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4415 | retry_deleg: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4416 | trap = lock_rename(new_path.dentry, old_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4418 | old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | error = PTR_ERR(old_dentry); |
| 4420 | if (IS_ERR(old_dentry)) |
| 4421 | goto exit3; |
| 4422 | /* source must exist */ |
| 4423 | error = -ENOENT; |
David Howells | b18825a | 2013-09-12 19:22:53 +0100 | [diff] [blame] | 4424 | if (d_is_negative(old_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | goto exit4; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4426 | 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] | 4427 | error = PTR_ERR(new_dentry); |
| 4428 | if (IS_ERR(new_dentry)) |
| 4429 | goto exit4; |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4430 | error = -EEXIST; |
| 4431 | if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) |
| 4432 | goto exit5; |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4433 | if (flags & RENAME_EXCHANGE) { |
| 4434 | error = -ENOENT; |
| 4435 | if (d_is_negative(new_dentry)) |
| 4436 | goto exit5; |
| 4437 | |
| 4438 | if (!d_is_dir(new_dentry)) { |
| 4439 | error = -ENOTDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4440 | if (new_last.name[new_last.len]) |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4441 | goto exit5; |
| 4442 | } |
| 4443 | } |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4444 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
| 4445 | if (!d_is_dir(old_dentry)) { |
| 4446 | error = -ENOTDIR; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4447 | if (old_last.name[old_last.len]) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4448 | goto exit5; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4449 | if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) |
Miklos Szeredi | 0a7c393 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4450 | goto exit5; |
| 4451 | } |
| 4452 | /* source should not be ancestor of target */ |
| 4453 | error = -EINVAL; |
| 4454 | if (old_dentry == trap) |
| 4455 | goto exit5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | /* target should not be an ancestor of source */ |
Miklos Szeredi | da1ce06 | 2014-04-01 17:08:43 +0200 | [diff] [blame] | 4457 | if (!(flags & RENAME_EXCHANGE)) |
| 4458 | error = -ENOTEMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4459 | if (new_dentry == trap) |
| 4460 | goto exit5; |
| 4461 | |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4462 | error = security_path_rename(&old_path, old_dentry, |
| 4463 | &new_path, new_dentry, flags); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4464 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4465 | goto exit5; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4466 | error = vfs_rename(old_path.dentry->d_inode, old_dentry, |
| 4467 | new_path.dentry->d_inode, new_dentry, |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4468 | &delegated_inode, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | exit5: |
| 4470 | dput(new_dentry); |
| 4471 | exit4: |
| 4472 | dput(old_dentry); |
| 4473 | exit3: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4474 | unlock_rename(new_path.dentry, old_path.dentry); |
J. Bruce Fields | 8e6d782 | 2011-09-20 16:59:58 -0400 | [diff] [blame] | 4475 | if (delegated_inode) { |
| 4476 | error = break_deleg_wait(&delegated_inode); |
| 4477 | if (!error) |
| 4478 | goto retry_deleg; |
| 4479 | } |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4480 | mnt_drop_write(old_path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | exit2: |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4482 | if (retry_estale(error, lookup_flags)) |
| 4483 | should_retry = true; |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4484 | path_put(&new_path); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4485 | putname(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | exit1: |
Al Viro | f5beed7 | 2015-04-30 16:09:11 -0400 | [diff] [blame] | 4487 | path_put(&old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | putname(from); |
Jeff Layton | c6a9428 | 2012-12-11 12:10:10 -0500 | [diff] [blame] | 4489 | if (should_retry) { |
| 4490 | should_retry = false; |
| 4491 | lookup_flags |= LOOKUP_REVAL; |
| 4492 | goto retry; |
| 4493 | } |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4494 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | return error; |
| 4496 | } |
| 4497 | |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4498 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, |
| 4499 | int, newdfd, const char __user *, newname, unsigned int, flags) |
| 4500 | { |
| 4501 | return do_renameat2(olddfd, oldname, newdfd, newname, flags); |
| 4502 | } |
| 4503 | |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4504 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
| 4505 | int, newdfd, const char __user *, newname) |
| 4506 | { |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4507 | return do_renameat2(olddfd, oldname, newdfd, newname, 0); |
Miklos Szeredi | 520c8b1 | 2014-04-01 17:08:42 +0200 | [diff] [blame] | 4508 | } |
| 4509 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 4510 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4511 | { |
Dominik Brodowski | ee81feb | 2018-03-11 11:34:28 +0100 | [diff] [blame] | 4512 | return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4513 | } |
| 4514 | |
Miklos Szeredi | 787fb6b | 2014-10-24 00:14:36 +0200 | [diff] [blame] | 4515 | int vfs_whiteout(struct inode *dir, struct dentry *dentry) |
| 4516 | { |
| 4517 | int error = may_create(dir, dentry); |
| 4518 | if (error) |
| 4519 | return error; |
| 4520 | |
| 4521 | if (!dir->i_op->mknod) |
| 4522 | return -EPERM; |
| 4523 | |
| 4524 | return dir->i_op->mknod(dir, dentry, |
| 4525 | S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); |
| 4526 | } |
| 4527 | EXPORT_SYMBOL(vfs_whiteout); |
| 4528 | |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 4529 | int readlink_copy(char __user *buffer, int buflen, const char *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | { |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 4531 | int len = PTR_ERR(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | if (IS_ERR(link)) |
| 4533 | goto out; |
| 4534 | |
| 4535 | len = strlen(link); |
| 4536 | if (len > (unsigned) buflen) |
| 4537 | len = buflen; |
| 4538 | if (copy_to_user(buffer, link, len)) |
| 4539 | len = -EFAULT; |
| 4540 | out: |
| 4541 | return len; |
| 4542 | } |
| 4543 | |
Miklos Szeredi | d60874c | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 4544 | /** |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4545 | * vfs_readlink - copy symlink body into userspace buffer |
| 4546 | * @dentry: dentry on which to get symbolic link |
| 4547 | * @buffer: user memory pointer |
| 4548 | * @buflen: size of buffer |
| 4549 | * |
| 4550 | * Does not touch atime. That's up to the caller if necessary |
| 4551 | * |
| 4552 | * Does not call security hook. |
| 4553 | */ |
| 4554 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4555 | { |
| 4556 | struct inode *inode = d_inode(dentry); |
Al Viro | f2df5da | 2018-07-19 17:35:51 -0400 | [diff] [blame] | 4557 | DEFINE_DELAYED_CALL(done); |
| 4558 | const char *link; |
| 4559 | int res; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4560 | |
Miklos Szeredi | 76fca90 | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4561 | if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { |
| 4562 | if (unlikely(inode->i_op->readlink)) |
| 4563 | return inode->i_op->readlink(dentry, buffer, buflen); |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4564 | |
Miklos Szeredi | 76fca90 | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4565 | if (!d_is_symlink(dentry)) |
| 4566 | return -EINVAL; |
| 4567 | |
| 4568 | spin_lock(&inode->i_lock); |
| 4569 | inode->i_opflags |= IOP_DEFAULT_READLINK; |
| 4570 | spin_unlock(&inode->i_lock); |
| 4571 | } |
| 4572 | |
Eric Biggers | 4c4f7c1 | 2019-04-10 13:21:14 -0700 | [diff] [blame] | 4573 | link = READ_ONCE(inode->i_link); |
Al Viro | f2df5da | 2018-07-19 17:35:51 -0400 | [diff] [blame] | 4574 | if (!link) { |
| 4575 | link = inode->i_op->get_link(dentry, inode, &done); |
| 4576 | if (IS_ERR(link)) |
| 4577 | return PTR_ERR(link); |
| 4578 | } |
| 4579 | res = readlink_copy(buffer, buflen, link); |
| 4580 | do_delayed_call(&done); |
| 4581 | return res; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 4582 | } |
| 4583 | EXPORT_SYMBOL(vfs_readlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4584 | |
Miklos Szeredi | d60874c | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 4585 | /** |
| 4586 | * vfs_get_link - get symlink body |
| 4587 | * @dentry: dentry on which to get symbolic link |
| 4588 | * @done: caller needs to free returned data with this |
| 4589 | * |
| 4590 | * Calls security hook and i_op->get_link() on the supplied inode. |
| 4591 | * |
| 4592 | * It does not touch atime. That's up to the caller if necessary. |
| 4593 | * |
| 4594 | * Does not work on "special" symlinks like /proc/$$/fd/N |
| 4595 | */ |
| 4596 | const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) |
| 4597 | { |
| 4598 | const char *res = ERR_PTR(-EINVAL); |
| 4599 | struct inode *inode = d_inode(dentry); |
| 4600 | |
| 4601 | if (d_is_symlink(dentry)) { |
| 4602 | res = ERR_PTR(security_inode_readlink(dentry)); |
| 4603 | if (!res) |
| 4604 | res = inode->i_op->get_link(dentry, inode, done); |
| 4605 | } |
| 4606 | return res; |
| 4607 | } |
| 4608 | EXPORT_SYMBOL(vfs_get_link); |
| 4609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | /* get the link contents into pagecache */ |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4611 | const char *page_get_link(struct dentry *dentry, struct inode *inode, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4612 | struct delayed_call *callback) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | { |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 4614 | char *kaddr; |
| 4615 | struct page *page; |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4616 | struct address_space *mapping = inode->i_mapping; |
| 4617 | |
Al Viro | d3883d4 | 2015-11-17 10:41:04 -0500 | [diff] [blame] | 4618 | if (!dentry) { |
| 4619 | page = find_get_page(mapping, 0); |
| 4620 | if (!page) |
| 4621 | return ERR_PTR(-ECHILD); |
| 4622 | if (!PageUptodate(page)) { |
| 4623 | put_page(page); |
| 4624 | return ERR_PTR(-ECHILD); |
| 4625 | } |
| 4626 | } else { |
| 4627 | page = read_mapping_page(mapping, 0, NULL); |
| 4628 | if (IS_ERR(page)) |
| 4629 | return (char*)page; |
| 4630 | } |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4631 | set_delayed_call(callback, page_put_link, page); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 4632 | BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); |
| 4633 | kaddr = page_address(page); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4634 | nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 4635 | return kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | } |
| 4637 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4638 | EXPORT_SYMBOL(page_get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4640 | void page_put_link(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4642 | put_page(arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4644 | EXPORT_SYMBOL(page_put_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | |
Al Viro | aa80dea | 2015-11-16 18:26:34 -0500 | [diff] [blame] | 4646 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4647 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4648 | DEFINE_DELAYED_CALL(done); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4649 | int res = readlink_copy(buffer, buflen, |
| 4650 | page_get_link(dentry, d_inode(dentry), |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 4651 | &done)); |
| 4652 | do_delayed_call(&done); |
Al Viro | aa80dea | 2015-11-16 18:26:34 -0500 | [diff] [blame] | 4653 | return res; |
| 4654 | } |
| 4655 | EXPORT_SYMBOL(page_readlink); |
| 4656 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4657 | /* |
| 4658 | * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS |
| 4659 | */ |
| 4660 | 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] | 4661 | { |
| 4662 | struct address_space *mapping = inode->i_mapping; |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4663 | struct page *page; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4664 | void *fsdata; |
Dmitriy Monakhov | beb497a | 2007-02-16 01:27:18 -0800 | [diff] [blame] | 4665 | int err; |
Tetsuo Handa | c718a97 | 2017-05-08 15:58:59 -0700 | [diff] [blame] | 4666 | unsigned int flags = 0; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4667 | if (nofs) |
| 4668 | flags |= AOP_FLAG_NOFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 4670 | retry: |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4671 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4672 | flags, &page, &fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | if (err) |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4674 | goto fail; |
| 4675 | |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 4676 | memcpy(page_address(page), symname, len-1); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4677 | |
| 4678 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, |
| 4679 | page, fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | if (err < 0) |
| 4681 | goto fail; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4682 | if (err < len-1) |
| 4683 | goto retry; |
| 4684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | mark_inode_dirty(inode); |
| 4686 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | fail: |
| 4688 | return err; |
| 4689 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4690 | EXPORT_SYMBOL(__page_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4692 | int page_symlink(struct inode *inode, const char *symname, int len) |
| 4693 | { |
| 4694 | return __page_symlink(inode, symname, len, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 4695 | !mapping_gfp_constraint(inode->i_mapping, __GFP_FS)); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4696 | } |
Al Viro | 4d35950 | 2014-03-14 12:20:17 -0400 | [diff] [blame] | 4697 | EXPORT_SYMBOL(page_symlink); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4698 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 4699 | const struct inode_operations page_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 4700 | .get_link = page_get_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | EXPORT_SYMBOL(page_symlink_inode_operations); |