]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
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> | |
630d9c47 | 19 | #include <linux/export.h> |
44696908 | 20 | #include <linux/kernel.h> |
1da177e4 LT |
21 | #include <linux/slab.h> |
22 | #include <linux/fs.h> | |
23 | #include <linux/namei.h> | |
1da177e4 | 24 | #include <linux/pagemap.h> |
0eeca283 | 25 | #include <linux/fsnotify.h> |
1da177e4 LT |
26 | #include <linux/personality.h> |
27 | #include <linux/security.h> | |
6146f0d5 | 28 | #include <linux/ima.h> |
1da177e4 LT |
29 | #include <linux/syscalls.h> |
30 | #include <linux/mount.h> | |
31 | #include <linux/audit.h> | |
16f7e0fe | 32 | #include <linux/capability.h> |
834f2a4a | 33 | #include <linux/file.h> |
5590ff0d | 34 | #include <linux/fcntl.h> |
08ce5f16 | 35 | #include <linux/device_cgroup.h> |
5ad4e53b | 36 | #include <linux/fs_struct.h> |
e77819e5 | 37 | #include <linux/posix_acl.h> |
99d263d4 | 38 | #include <linux/hash.h> |
2a18da7a | 39 | #include <linux/bitops.h> |
aeaa4a79 | 40 | #include <linux/init_task.h> |
7c0f6ba6 | 41 | #include <linux/uaccess.h> |
1da177e4 | 42 | |
e81e3f4d | 43 | #include "internal.h" |
c7105365 | 44 | #include "mount.h" |
e81e3f4d | 45 | |
1da177e4 LT |
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 | |
25985edc | 80 | * the name is a symlink pointing to a non-existent name. |
1da177e4 LT |
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) | |
a11f3a05 | 113 | * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives |
1da177e4 LT |
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 | */ | |
91a27b2a | 124 | |
fd2f7cb5 | 125 | #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) |
7950e385 | 126 | |
51f39a1f | 127 | struct filename * |
91a27b2a JL |
128 | getname_flags(const char __user *filename, int flags, int *empty) |
129 | { | |
94b5d262 | 130 | struct filename *result; |
7950e385 | 131 | char *kname; |
94b5d262 | 132 | int len; |
4043cde8 | 133 | |
7ac86265 JL |
134 | result = audit_reusename(filename); |
135 | if (result) | |
136 | return result; | |
137 | ||
7950e385 | 138 | result = __getname(); |
3f9f0aa6 | 139 | if (unlikely(!result)) |
4043cde8 EP |
140 | return ERR_PTR(-ENOMEM); |
141 | ||
7950e385 JL |
142 | /* |
143 | * First, try to embed the struct filename inside the names_cache | |
144 | * allocation | |
145 | */ | |
fd2f7cb5 | 146 | kname = (char *)result->iname; |
91a27b2a | 147 | result->name = kname; |
7950e385 | 148 | |
94b5d262 | 149 | len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); |
91a27b2a | 150 | if (unlikely(len < 0)) { |
94b5d262 AV |
151 | __putname(result); |
152 | return ERR_PTR(len); | |
91a27b2a | 153 | } |
3f9f0aa6 | 154 | |
7950e385 JL |
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 | */ | |
94b5d262 | 161 | if (unlikely(len == EMBEDDED_NAME_MAX)) { |
fd2f7cb5 | 162 | const size_t size = offsetof(struct filename, iname[1]); |
7950e385 JL |
163 | kname = (char *)result; |
164 | ||
fd2f7cb5 AV |
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); | |
94b5d262 AV |
171 | if (unlikely(!result)) { |
172 | __putname(kname); | |
173 | return ERR_PTR(-ENOMEM); | |
7950e385 JL |
174 | } |
175 | result->name = kname; | |
94b5d262 AV |
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 | } | |
7950e385 JL |
187 | } |
188 | ||
94b5d262 | 189 | result->refcnt = 1; |
3f9f0aa6 LT |
190 | /* The empty path is special. */ |
191 | if (unlikely(!len)) { | |
192 | if (empty) | |
4043cde8 | 193 | *empty = 1; |
94b5d262 AV |
194 | if (!(flags & LOOKUP_EMPTY)) { |
195 | putname(result); | |
196 | return ERR_PTR(-ENOENT); | |
197 | } | |
1da177e4 | 198 | } |
3f9f0aa6 | 199 | |
7950e385 | 200 | result->uptr = filename; |
c4ad8f98 | 201 | result->aname = NULL; |
7950e385 JL |
202 | audit_getname(result); |
203 | return result; | |
1da177e4 LT |
204 | } |
205 | ||
91a27b2a JL |
206 | struct filename * |
207 | getname(const char __user * filename) | |
f52e0c11 | 208 | { |
f7493e5d | 209 | return getname_flags(filename, 0, NULL); |
f52e0c11 AV |
210 | } |
211 | ||
c4ad8f98 LT |
212 | struct filename * |
213 | getname_kernel(const char * filename) | |
214 | { | |
215 | struct filename *result; | |
08518549 | 216 | int len = strlen(filename) + 1; |
c4ad8f98 LT |
217 | |
218 | result = __getname(); | |
219 | if (unlikely(!result)) | |
220 | return ERR_PTR(-ENOMEM); | |
221 | ||
08518549 | 222 | if (len <= EMBEDDED_NAME_MAX) { |
fd2f7cb5 | 223 | result->name = (char *)result->iname; |
08518549 | 224 | } else if (len <= PATH_MAX) { |
30ce4d19 | 225 | const size_t size = offsetof(struct filename, iname[1]); |
08518549 PM |
226 | struct filename *tmp; |
227 | ||
30ce4d19 | 228 | tmp = kmalloc(size, GFP_KERNEL); |
08518549 PM |
229 | if (unlikely(!tmp)) { |
230 | __putname(result); | |
231 | return ERR_PTR(-ENOMEM); | |
232 | } | |
233 | tmp->name = (char *)result; | |
08518549 PM |
234 | result = tmp; |
235 | } else { | |
236 | __putname(result); | |
237 | return ERR_PTR(-ENAMETOOLONG); | |
238 | } | |
239 | memcpy((char *)result->name, filename, len); | |
c4ad8f98 LT |
240 | result->uptr = NULL; |
241 | result->aname = NULL; | |
55422d0b | 242 | result->refcnt = 1; |
fd3522fd | 243 | audit_getname(result); |
c4ad8f98 | 244 | |
c4ad8f98 LT |
245 | return result; |
246 | } | |
247 | ||
91a27b2a | 248 | void putname(struct filename *name) |
1da177e4 | 249 | { |
55422d0b PM |
250 | BUG_ON(name->refcnt <= 0); |
251 | ||
252 | if (--name->refcnt > 0) | |
253 | return; | |
254 | ||
fd2f7cb5 | 255 | if (name->name != name->iname) { |
55422d0b PM |
256 | __putname(name->name); |
257 | kfree(name); | |
258 | } else | |
259 | __putname(name); | |
1da177e4 | 260 | } |
1da177e4 | 261 | |
e77819e5 LT |
262 | static int check_acl(struct inode *inode, int mask) |
263 | { | |
84635d68 | 264 | #ifdef CONFIG_FS_POSIX_ACL |
e77819e5 LT |
265 | struct posix_acl *acl; |
266 | ||
e77819e5 | 267 | if (mask & MAY_NOT_BLOCK) { |
3567866b AV |
268 | acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); |
269 | if (!acl) | |
e77819e5 | 270 | return -EAGAIN; |
3567866b | 271 | /* no ->get_acl() calls in RCU mode... */ |
b8a7a3a6 | 272 | if (is_uncached_acl(acl)) |
3567866b | 273 | return -ECHILD; |
206b1d09 | 274 | return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); |
e77819e5 LT |
275 | } |
276 | ||
2982baa2 CH |
277 | acl = get_acl(inode, ACL_TYPE_ACCESS); |
278 | if (IS_ERR(acl)) | |
279 | return PTR_ERR(acl); | |
e77819e5 LT |
280 | if (acl) { |
281 | int error = posix_acl_permission(inode, acl, mask); | |
282 | posix_acl_release(acl); | |
283 | return error; | |
284 | } | |
84635d68 | 285 | #endif |
e77819e5 LT |
286 | |
287 | return -EAGAIN; | |
288 | } | |
289 | ||
5909ccaa | 290 | /* |
948409c7 | 291 | * This does the basic permission checking |
1da177e4 | 292 | */ |
7e40145e | 293 | static int acl_permission_check(struct inode *inode, int mask) |
1da177e4 | 294 | { |
26cf46be | 295 | unsigned int mode = inode->i_mode; |
1da177e4 | 296 | |
8e96e3b7 | 297 | if (likely(uid_eq(current_fsuid(), inode->i_uid))) |
1da177e4 LT |
298 | mode >>= 6; |
299 | else { | |
e77819e5 | 300 | if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { |
7e40145e | 301 | int error = check_acl(inode, mask); |
b74c79e9 NP |
302 | if (error != -EAGAIN) |
303 | return error; | |
1da177e4 LT |
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 | */ | |
9c2c7039 | 313 | if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) |
1da177e4 | 314 | return 0; |
5909ccaa LT |
315 | return -EACCES; |
316 | } | |
317 | ||
318 | /** | |
b74c79e9 | 319 | * generic_permission - check for access rights on a Posix-like filesystem |
5909ccaa | 320 | * @inode: inode to check access rights for |
8fd90c8d | 321 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) |
5909ccaa LT |
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 | |
b74c79e9 NP |
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. | |
5909ccaa | 331 | */ |
2830ba7f | 332 | int generic_permission(struct inode *inode, int mask) |
5909ccaa LT |
333 | { |
334 | int ret; | |
335 | ||
336 | /* | |
948409c7 | 337 | * Do the basic permission checks. |
5909ccaa | 338 | */ |
7e40145e | 339 | ret = acl_permission_check(inode, mask); |
5909ccaa LT |
340 | if (ret != -EACCES) |
341 | return ret; | |
1da177e4 | 342 | |
d594e7ec AV |
343 | if (S_ISDIR(inode->i_mode)) { |
344 | /* DACs are overridable for directories */ | |
d594e7ec | 345 | if (!(mask & MAY_WRITE)) |
23adbe12 AL |
346 | if (capable_wrt_inode_uidgid(inode, |
347 | CAP_DAC_READ_SEARCH)) | |
d594e7ec | 348 | return 0; |
23adbe12 | 349 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE)) |
1da177e4 | 350 | return 0; |
2a4c2242 SS |
351 | return -EACCES; |
352 | } | |
1da177e4 LT |
353 | |
354 | /* | |
355 | * Searching includes executable on directories, else just read. | |
356 | */ | |
7ea66001 | 357 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
d594e7ec | 358 | if (mask == MAY_READ) |
23adbe12 | 359 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH)) |
1da177e4 | 360 | return 0; |
2a4c2242 SS |
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; | |
1da177e4 LT |
369 | |
370 | return -EACCES; | |
371 | } | |
4d359507 | 372 | EXPORT_SYMBOL(generic_permission); |
1da177e4 | 373 | |
3ddcd056 LT |
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 | ||
0bdaea90 DH |
394 | /** |
395 | * sb_permission - Check superblock-level permissions | |
396 | * @sb: Superblock of inode to check permission on | |
55852635 | 397 | * @inode: Inode to check permission on |
0bdaea90 DH |
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. */ | |
bc98a42c | 408 | if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
0bdaea90 DH |
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; | |
4bfd054a EB |
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); | |
0bdaea90 | 458 | } |
4d359507 | 459 | EXPORT_SYMBOL(inode_permission); |
0bdaea90 | 460 | |
5dd784d0 JB |
461 | /** |
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 | */ | |
dcf787f3 | 467 | void path_get(const struct path *path) |
5dd784d0 JB |
468 | { |
469 | mntget(path->mnt); | |
470 | dget(path->dentry); | |
471 | } | |
472 | EXPORT_SYMBOL(path_get); | |
473 | ||
1d957f9b JB |
474 | /** |
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 | */ | |
dcf787f3 | 480 | void path_put(const struct path *path) |
1da177e4 | 481 | { |
1d957f9b JB |
482 | dput(path->dentry); |
483 | mntput(path->mnt); | |
1da177e4 | 484 | } |
1d957f9b | 485 | EXPORT_SYMBOL(path_put); |
1da177e4 | 486 | |
894bc8c4 | 487 | #define EMBEDDED_LEVELS 2 |
1f55a6ec AV |
488 | struct nameidata { |
489 | struct path path; | |
1cf2665b | 490 | struct qstr last; |
1f55a6ec AV |
491 | struct path root; |
492 | struct inode *inode; /* path.dentry.d_inode */ | |
493 | unsigned int flags; | |
ab87f9a5 | 494 | unsigned seq, m_seq, r_seq; |
1f55a6ec AV |
495 | int last_type; |
496 | unsigned depth; | |
756daf26 | 497 | int total_link_count; |
697fc6ca AV |
498 | struct saved { |
499 | struct path link; | |
fceef393 | 500 | struct delayed_call done; |
697fc6ca | 501 | const char *name; |
0450b2d1 | 502 | unsigned seq; |
894bc8c4 | 503 | } *stack, internal[EMBEDDED_LEVELS]; |
9883d185 AV |
504 | struct filename *name; |
505 | struct nameidata *saved; | |
506 | unsigned root_seq; | |
507 | int dfd; | |
3859a271 | 508 | } __randomize_layout; |
1f55a6ec | 509 | |
9883d185 | 510 | static void set_nameidata(struct nameidata *p, int dfd, struct filename *name) |
894bc8c4 | 511 | { |
756daf26 N |
512 | struct nameidata *old = current->nameidata; |
513 | p->stack = p->internal; | |
c8a53ee5 AV |
514 | p->dfd = dfd; |
515 | p->name = name; | |
756daf26 | 516 | p->total_link_count = old ? old->total_link_count : 0; |
9883d185 | 517 | p->saved = old; |
756daf26 | 518 | current->nameidata = p; |
894bc8c4 AV |
519 | } |
520 | ||
9883d185 | 521 | static void restore_nameidata(void) |
894bc8c4 | 522 | { |
9883d185 | 523 | struct nameidata *now = current->nameidata, *old = now->saved; |
756daf26 N |
524 | |
525 | current->nameidata = old; | |
526 | if (old) | |
527 | old->total_link_count = now->total_link_count; | |
e1a63bbc | 528 | if (now->stack != now->internal) |
756daf26 | 529 | kfree(now->stack); |
894bc8c4 AV |
530 | } |
531 | ||
532 | static int __nd_alloc_stack(struct nameidata *nd) | |
533 | { | |
bc40aee0 AV |
534 | struct saved *p; |
535 | ||
536 | if (nd->flags & LOOKUP_RCU) { | |
6da2ec56 | 537 | p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), |
bc40aee0 AV |
538 | GFP_ATOMIC); |
539 | if (unlikely(!p)) | |
540 | return -ECHILD; | |
541 | } else { | |
6da2ec56 | 542 | p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), |
894bc8c4 | 543 | GFP_KERNEL); |
bc40aee0 AV |
544 | if (unlikely(!p)) |
545 | return -ENOMEM; | |
546 | } | |
894bc8c4 AV |
547 | memcpy(p, nd->internal, sizeof(nd->internal)); |
548 | nd->stack = p; | |
549 | return 0; | |
550 | } | |
551 | ||
397d425d | 552 | /** |
6b03f7ed | 553 | * path_connected - Verify that a dentry is below mnt.mnt_root |
397d425d EB |
554 | * |
555 | * Rename can sometimes move a file or directory outside of a bind | |
556 | * mount, path_connected allows those cases to be detected. | |
557 | */ | |
6b03f7ed | 558 | static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) |
397d425d | 559 | { |
95dd7758 | 560 | struct super_block *sb = mnt->mnt_sb; |
397d425d | 561 | |
95dd7758 EB |
562 | /* Bind mounts and multi-root filesystems can have disconnected paths */ |
563 | if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root)) | |
397d425d EB |
564 | return true; |
565 | ||
6b03f7ed | 566 | return is_subdir(dentry, mnt->mnt_root); |
397d425d EB |
567 | } |
568 | ||
894bc8c4 AV |
569 | static inline int nd_alloc_stack(struct nameidata *nd) |
570 | { | |
da4e0be0 | 571 | if (likely(nd->depth != EMBEDDED_LEVELS)) |
894bc8c4 AV |
572 | return 0; |
573 | if (likely(nd->stack != nd->internal)) | |
574 | return 0; | |
575 | return __nd_alloc_stack(nd); | |
576 | } | |
577 | ||
7973387a AV |
578 | static void drop_links(struct nameidata *nd) |
579 | { | |
580 | int i = nd->depth; | |
581 | while (i--) { | |
582 | struct saved *last = nd->stack + i; | |
fceef393 AV |
583 | do_delayed_call(&last->done); |
584 | clear_delayed_call(&last->done); | |
7973387a AV |
585 | } |
586 | } | |
587 | ||
588 | static void terminate_walk(struct nameidata *nd) | |
589 | { | |
590 | drop_links(nd); | |
591 | if (!(nd->flags & LOOKUP_RCU)) { | |
592 | int i; | |
593 | path_put(&nd->path); | |
594 | for (i = 0; i < nd->depth; i++) | |
595 | path_put(&nd->stack[i].link); | |
84a2bd39 | 596 | if (nd->flags & LOOKUP_ROOT_GRABBED) { |
102b8af2 | 597 | path_put(&nd->root); |
84a2bd39 | 598 | nd->flags &= ~LOOKUP_ROOT_GRABBED; |
102b8af2 | 599 | } |
7973387a AV |
600 | } else { |
601 | nd->flags &= ~LOOKUP_RCU; | |
7973387a AV |
602 | rcu_read_unlock(); |
603 | } | |
604 | nd->depth = 0; | |
605 | } | |
606 | ||
607 | /* path_put is needed afterwards regardless of success or failure */ | |
608 | static bool legitimize_path(struct nameidata *nd, | |
609 | struct path *path, unsigned seq) | |
610 | { | |
611 | int res = __legitimize_mnt(path->mnt, nd->m_seq); | |
612 | if (unlikely(res)) { | |
613 | if (res > 0) | |
614 | path->mnt = NULL; | |
615 | path->dentry = NULL; | |
616 | return false; | |
617 | } | |
618 | if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { | |
619 | path->dentry = NULL; | |
620 | return false; | |
621 | } | |
622 | return !read_seqcount_retry(&path->dentry->d_seq, seq); | |
623 | } | |
624 | ||
625 | static bool legitimize_links(struct nameidata *nd) | |
626 | { | |
627 | int i; | |
628 | for (i = 0; i < nd->depth; i++) { | |
629 | struct saved *last = nd->stack + i; | |
630 | if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { | |
631 | drop_links(nd); | |
632 | nd->depth = i + 1; | |
633 | return false; | |
634 | } | |
635 | } | |
636 | return true; | |
637 | } | |
638 | ||
ee594bff AV |
639 | static bool legitimize_root(struct nameidata *nd) |
640 | { | |
adb21d2b AS |
641 | /* |
642 | * For scoped-lookups (where nd->root has been zeroed), we need to | |
643 | * restart the whole lookup from scratch -- because set_root() is wrong | |
644 | * for these lookups (nd->dfd is the root, not the filesystem root). | |
645 | */ | |
646 | if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED)) | |
647 | return false; | |
648 | /* Nothing to do if nd->root is zero or is managed by the VFS user. */ | |
ee594bff AV |
649 | if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT)) |
650 | return true; | |
84a2bd39 | 651 | nd->flags |= LOOKUP_ROOT_GRABBED; |
ee594bff AV |
652 | return legitimize_path(nd, &nd->root, nd->root_seq); |
653 | } | |
654 | ||
19660af7 | 655 | /* |
31e6b01f | 656 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
19660af7 AV |
657 | * Documentation/filesystems/path-lookup.txt). In situations when we can't |
658 | * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab | |
57e3715c | 659 | * normal reference counts on dentries and vfsmounts to transition to ref-walk |
19660af7 AV |
660 | * mode. Refcounts are grabbed at the last known good point before rcu-walk |
661 | * got stuck, so ref-walk may continue from there. If this is not successful | |
662 | * (eg. a seqcount has changed), then failure is returned and it's up to caller | |
663 | * to restart the path walk from the beginning in ref-walk mode. | |
31e6b01f | 664 | */ |
31e6b01f NP |
665 | |
666 | /** | |
19660af7 AV |
667 | * unlazy_walk - try to switch to ref-walk mode. |
668 | * @nd: nameidata pathwalk data | |
39191628 | 669 | * Returns: 0 on success, -ECHILD on failure |
31e6b01f | 670 | * |
4675ac39 AV |
671 | * unlazy_walk attempts to legitimize the current nd->path and nd->root |
672 | * for ref-walk mode. | |
673 | * Must be called from rcu-walk context. | |
7973387a AV |
674 | * Nothing should touch nameidata between unlazy_walk() failure and |
675 | * terminate_walk(). | |
31e6b01f | 676 | */ |
4675ac39 | 677 | static int unlazy_walk(struct nameidata *nd) |
31e6b01f | 678 | { |
31e6b01f NP |
679 | struct dentry *parent = nd->path.dentry; |
680 | ||
681 | BUG_ON(!(nd->flags & LOOKUP_RCU)); | |
e5c832d5 | 682 | |
4675ac39 AV |
683 | nd->flags &= ~LOOKUP_RCU; |
684 | if (unlikely(!legitimize_links(nd))) | |
4675ac39 | 685 | goto out1; |
84a2bd39 AV |
686 | if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) |
687 | goto out; | |
ee594bff AV |
688 | if (unlikely(!legitimize_root(nd))) |
689 | goto out; | |
4675ac39 AV |
690 | rcu_read_unlock(); |
691 | BUG_ON(nd->inode != parent->d_inode); | |
692 | return 0; | |
693 | ||
84a2bd39 | 694 | out1: |
4675ac39 AV |
695 | nd->path.mnt = NULL; |
696 | nd->path.dentry = NULL; | |
4675ac39 AV |
697 | out: |
698 | rcu_read_unlock(); | |
699 | return -ECHILD; | |
700 | } | |
701 | ||
702 | /** | |
703 | * unlazy_child - try to switch to ref-walk mode. | |
704 | * @nd: nameidata pathwalk data | |
705 | * @dentry: child of nd->path.dentry | |
706 | * @seq: seq number to check dentry against | |
707 | * Returns: 0 on success, -ECHILD on failure | |
708 | * | |
709 | * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry | |
710 | * for ref-walk mode. @dentry must be a path found by a do_lookup call on | |
711 | * @nd. Must be called from rcu-walk context. | |
712 | * Nothing should touch nameidata between unlazy_child() failure and | |
713 | * terminate_walk(). | |
714 | */ | |
715 | static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq) | |
716 | { | |
717 | BUG_ON(!(nd->flags & LOOKUP_RCU)); | |
718 | ||
e5c832d5 | 719 | nd->flags &= ~LOOKUP_RCU; |
7973387a AV |
720 | if (unlikely(!legitimize_links(nd))) |
721 | goto out2; | |
722 | if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) | |
723 | goto out2; | |
4675ac39 | 724 | if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) |
7973387a | 725 | goto out1; |
48a066e7 | 726 | |
15570086 | 727 | /* |
4675ac39 AV |
728 | * We need to move both the parent and the dentry from the RCU domain |
729 | * to be properly refcounted. And the sequence number in the dentry | |
730 | * validates *both* dentry counters, since we checked the sequence | |
731 | * number of the parent after we got the child sequence number. So we | |
732 | * know the parent must still be valid if the child sequence number is | |
15570086 | 733 | */ |
4675ac39 AV |
734 | if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) |
735 | goto out; | |
84a2bd39 AV |
736 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
737 | goto out_dput; | |
e5c832d5 LT |
738 | /* |
739 | * Sequence counts matched. Now make sure that the root is | |
740 | * still valid and get it if required. | |
741 | */ | |
84a2bd39 AV |
742 | if (unlikely(!legitimize_root(nd))) |
743 | goto out_dput; | |
8b61e74f | 744 | rcu_read_unlock(); |
31e6b01f | 745 | return 0; |
19660af7 | 746 | |
7973387a AV |
747 | out2: |
748 | nd->path.mnt = NULL; | |
749 | out1: | |
750 | nd->path.dentry = NULL; | |
e5c832d5 | 751 | out: |
8b61e74f | 752 | rcu_read_unlock(); |
84a2bd39 AV |
753 | return -ECHILD; |
754 | out_dput: | |
755 | rcu_read_unlock(); | |
756 | dput(dentry); | |
31e6b01f NP |
757 | return -ECHILD; |
758 | } | |
759 | ||
4ce16ef3 | 760 | static inline int d_revalidate(struct dentry *dentry, unsigned int flags) |
34286d66 | 761 | { |
a89f8337 AV |
762 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) |
763 | return dentry->d_op->d_revalidate(dentry, flags); | |
764 | else | |
765 | return 1; | |
34286d66 NP |
766 | } |
767 | ||
9f1fafee AV |
768 | /** |
769 | * complete_walk - successful completion of path walk | |
770 | * @nd: pointer nameidata | |
39159de2 | 771 | * |
9f1fafee AV |
772 | * If we had been in RCU mode, drop out of it and legitimize nd->path. |
773 | * Revalidate the final result, unless we'd already done that during | |
774 | * the path walk or the filesystem doesn't ask for it. Return 0 on | |
775 | * success, -error on failure. In case of failure caller does not | |
776 | * need to drop nd->path. | |
39159de2 | 777 | */ |
9f1fafee | 778 | static int complete_walk(struct nameidata *nd) |
39159de2 | 779 | { |
16c2cd71 | 780 | struct dentry *dentry = nd->path.dentry; |
39159de2 | 781 | int status; |
39159de2 | 782 | |
9f1fafee | 783 | if (nd->flags & LOOKUP_RCU) { |
adb21d2b AS |
784 | /* |
785 | * We don't want to zero nd->root for scoped-lookups or | |
786 | * externally-managed nd->root. | |
787 | */ | |
788 | if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED))) | |
9f1fafee | 789 | nd->root.mnt = NULL; |
4675ac39 | 790 | if (unlikely(unlazy_walk(nd))) |
9f1fafee | 791 | return -ECHILD; |
9f1fafee AV |
792 | } |
793 | ||
adb21d2b AS |
794 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
795 | /* | |
796 | * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't | |
797 | * ever step outside the root during lookup" and should already | |
798 | * be guaranteed by the rest of namei, we want to avoid a namei | |
799 | * BUG resulting in userspace being given a path that was not | |
800 | * scoped within the root at some point during the lookup. | |
801 | * | |
802 | * So, do a final sanity-check to make sure that in the | |
803 | * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) | |
804 | * we won't silently return an fd completely outside of the | |
805 | * requested root to userspace. | |
806 | * | |
807 | * Userspace could move the path outside the root after this | |
808 | * check, but as discussed elsewhere this is not a concern (the | |
809 | * resolved file was inside the root at some point). | |
810 | */ | |
811 | if (!path_is_under(&nd->path, &nd->root)) | |
812 | return -EXDEV; | |
813 | } | |
814 | ||
16c2cd71 AV |
815 | if (likely(!(nd->flags & LOOKUP_JUMPED))) |
816 | return 0; | |
817 | ||
ecf3d1f1 | 818 | if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) |
39159de2 JL |
819 | return 0; |
820 | ||
ecf3d1f1 | 821 | status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); |
39159de2 JL |
822 | if (status > 0) |
823 | return 0; | |
824 | ||
16c2cd71 | 825 | if (!status) |
39159de2 | 826 | status = -ESTALE; |
16c2cd71 | 827 | |
39159de2 JL |
828 | return status; |
829 | } | |
830 | ||
740a1678 | 831 | static int set_root(struct nameidata *nd) |
31e6b01f | 832 | { |
7bd88377 | 833 | struct fs_struct *fs = current->fs; |
c28cc364 | 834 | |
adb21d2b AS |
835 | /* |
836 | * Jumping to the real root in a scoped-lookup is a BUG in namei, but we | |
837 | * still have to ensure it doesn't happen because it will cause a breakout | |
838 | * from the dirfd. | |
839 | */ | |
840 | if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) | |
841 | return -ENOTRECOVERABLE; | |
842 | ||
9e6697e2 AV |
843 | if (nd->flags & LOOKUP_RCU) { |
844 | unsigned seq; | |
845 | ||
846 | do { | |
847 | seq = read_seqcount_begin(&fs->seq); | |
848 | nd->root = fs->root; | |
849 | nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); | |
850 | } while (read_seqcount_retry(&fs->seq, seq)); | |
851 | } else { | |
852 | get_fs_root(fs, &nd->root); | |
84a2bd39 | 853 | nd->flags |= LOOKUP_ROOT_GRABBED; |
9e6697e2 | 854 | } |
740a1678 | 855 | return 0; |
31e6b01f NP |
856 | } |
857 | ||
7b9337aa NP |
858 | static inline void path_to_nameidata(const struct path *path, |
859 | struct nameidata *nd) | |
051d3812 | 860 | { |
31e6b01f NP |
861 | if (!(nd->flags & LOOKUP_RCU)) { |
862 | dput(nd->path.dentry); | |
863 | if (nd->path.mnt != path->mnt) | |
864 | mntput(nd->path.mnt); | |
9a229683 | 865 | } |
31e6b01f | 866 | nd->path.mnt = path->mnt; |
4ac91378 | 867 | nd->path.dentry = path->dentry; |
051d3812 IK |
868 | } |
869 | ||
248fb5b9 AV |
870 | static int nd_jump_root(struct nameidata *nd) |
871 | { | |
adb21d2b AS |
872 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
873 | return -EXDEV; | |
72ba2929 AS |
874 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
875 | /* Absolute path arguments to path_init() are allowed. */ | |
876 | if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) | |
877 | return -EXDEV; | |
878 | } | |
740a1678 AS |
879 | if (!nd->root.mnt) { |
880 | int error = set_root(nd); | |
881 | if (error) | |
882 | return error; | |
883 | } | |
248fb5b9 AV |
884 | if (nd->flags & LOOKUP_RCU) { |
885 | struct dentry *d; | |
886 | nd->path = nd->root; | |
887 | d = nd->path.dentry; | |
888 | nd->inode = d->d_inode; | |
889 | nd->seq = nd->root_seq; | |
890 | if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq))) | |
891 | return -ECHILD; | |
892 | } else { | |
893 | path_put(&nd->path); | |
894 | nd->path = nd->root; | |
895 | path_get(&nd->path); | |
896 | nd->inode = nd->path.dentry->d_inode; | |
897 | } | |
898 | nd->flags |= LOOKUP_JUMPED; | |
899 | return 0; | |
900 | } | |
901 | ||
b5fb63c1 | 902 | /* |
6b255391 | 903 | * Helper to directly jump to a known parsed path from ->get_link, |
b5fb63c1 CH |
904 | * caller must have taken a reference to path beforehand. |
905 | */ | |
1bc82070 | 906 | int nd_jump_link(struct path *path) |
b5fb63c1 | 907 | { |
4b99d499 | 908 | int error = -ELOOP; |
6e77137b | 909 | struct nameidata *nd = current->nameidata; |
b5fb63c1 | 910 | |
4b99d499 AS |
911 | if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) |
912 | goto err; | |
913 | ||
72ba2929 AS |
914 | error = -EXDEV; |
915 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { | |
916 | if (nd->path.mnt != path->mnt) | |
917 | goto err; | |
918 | } | |
adb21d2b AS |
919 | /* Not currently safe for scoped-lookups. */ |
920 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) | |
921 | goto err; | |
72ba2929 | 922 | |
4b99d499 | 923 | path_put(&nd->path); |
b5fb63c1 CH |
924 | nd->path = *path; |
925 | nd->inode = nd->path.dentry->d_inode; | |
926 | nd->flags |= LOOKUP_JUMPED; | |
1bc82070 | 927 | return 0; |
4b99d499 AS |
928 | |
929 | err: | |
930 | path_put(path); | |
931 | return error; | |
b5fb63c1 CH |
932 | } |
933 | ||
b9ff4429 | 934 | static inline void put_link(struct nameidata *nd) |
574197e0 | 935 | { |
21c3003d | 936 | struct saved *last = nd->stack + --nd->depth; |
fceef393 | 937 | do_delayed_call(&last->done); |
6548fae2 AV |
938 | if (!(nd->flags & LOOKUP_RCU)) |
939 | path_put(&last->link); | |
574197e0 AV |
940 | } |
941 | ||
561ec64a LT |
942 | int sysctl_protected_symlinks __read_mostly = 0; |
943 | int sysctl_protected_hardlinks __read_mostly = 0; | |
30aba665 SM |
944 | int sysctl_protected_fifos __read_mostly; |
945 | int sysctl_protected_regular __read_mostly; | |
800179c9 KC |
946 | |
947 | /** | |
948 | * may_follow_link - Check symlink following for unsafe situations | |
55852635 | 949 | * @nd: nameidata pathwalk data |
800179c9 KC |
950 | * |
951 | * In the case of the sysctl_protected_symlinks sysctl being enabled, | |
952 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is | |
953 | * in a sticky world-writable directory. This is to protect privileged | |
954 | * processes from failing races against path names that may change out | |
955 | * from under them by way of other users creating malicious symlinks. | |
956 | * It will permit symlinks to be followed only when outside a sticky | |
957 | * world-writable directory, or when the uid of the symlink and follower | |
958 | * match, or when the directory owner matches the symlink's owner. | |
959 | * | |
960 | * Returns 0 if following the symlink is allowed, -ve on error. | |
961 | */ | |
ad6cc4c3 | 962 | static inline int may_follow_link(struct nameidata *nd, const struct inode *inode) |
800179c9 | 963 | { |
800179c9 | 964 | const struct inode *parent; |
2d7f9e2a | 965 | kuid_t puid; |
800179c9 KC |
966 | |
967 | if (!sysctl_protected_symlinks) | |
968 | return 0; | |
969 | ||
970 | /* Allowed if owner and follower match. */ | |
81abe27b | 971 | if (uid_eq(current_cred()->fsuid, inode->i_uid)) |
800179c9 KC |
972 | return 0; |
973 | ||
974 | /* Allowed if parent directory not sticky and world-writable. */ | |
aa65fa35 | 975 | parent = nd->inode; |
800179c9 KC |
976 | if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
977 | return 0; | |
978 | ||
979 | /* Allowed if parent directory and link owner match. */ | |
2d7f9e2a SF |
980 | puid = parent->i_uid; |
981 | if (uid_valid(puid) && uid_eq(puid, inode->i_uid)) | |
800179c9 KC |
982 | return 0; |
983 | ||
31956502 AV |
984 | if (nd->flags & LOOKUP_RCU) |
985 | return -ECHILD; | |
986 | ||
ea841baf | 987 | audit_inode(nd->name, nd->stack[0].link.dentry, 0); |
245d7369 | 988 | audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); |
800179c9 KC |
989 | return -EACCES; |
990 | } | |
991 | ||
992 | /** | |
993 | * safe_hardlink_source - Check for safe hardlink conditions | |
994 | * @inode: the source inode to hardlink from | |
995 | * | |
996 | * Return false if at least one of the following conditions: | |
997 | * - inode is not a regular file | |
998 | * - inode is setuid | |
999 | * - inode is setgid and group-exec | |
1000 | * - access failure for read and write | |
1001 | * | |
1002 | * Otherwise returns true. | |
1003 | */ | |
1004 | static bool safe_hardlink_source(struct inode *inode) | |
1005 | { | |
1006 | umode_t mode = inode->i_mode; | |
1007 | ||
1008 | /* Special files should not get pinned to the filesystem. */ | |
1009 | if (!S_ISREG(mode)) | |
1010 | return false; | |
1011 | ||
1012 | /* Setuid files should not get pinned to the filesystem. */ | |
1013 | if (mode & S_ISUID) | |
1014 | return false; | |
1015 | ||
1016 | /* Executable setgid files should not get pinned to the filesystem. */ | |
1017 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) | |
1018 | return false; | |
1019 | ||
1020 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ | |
1021 | if (inode_permission(inode, MAY_READ | MAY_WRITE)) | |
1022 | return false; | |
1023 | ||
1024 | return true; | |
1025 | } | |
1026 | ||
1027 | /** | |
1028 | * may_linkat - Check permissions for creating a hardlink | |
1029 | * @link: the source to hardlink from | |
1030 | * | |
1031 | * Block hardlink when all of: | |
1032 | * - sysctl_protected_hardlinks enabled | |
1033 | * - fsuid does not match inode | |
1034 | * - hardlink source is unsafe (see safe_hardlink_source() above) | |
f2ca3796 | 1035 | * - not CAP_FOWNER in a namespace with the inode owner uid mapped |
800179c9 KC |
1036 | * |
1037 | * Returns 0 if successful, -ve on error. | |
1038 | */ | |
1039 | static int may_linkat(struct path *link) | |
1040 | { | |
593d1ce8 EB |
1041 | struct inode *inode = link->dentry->d_inode; |
1042 | ||
1043 | /* Inode writeback is not safe when the uid or gid are invalid. */ | |
1044 | if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid)) | |
1045 | return -EOVERFLOW; | |
800179c9 KC |
1046 | |
1047 | if (!sysctl_protected_hardlinks) | |
1048 | return 0; | |
1049 | ||
800179c9 KC |
1050 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, |
1051 | * otherwise, it must be a safe source. | |
1052 | */ | |
cc658db4 | 1053 | if (safe_hardlink_source(inode) || inode_owner_or_capable(inode)) |
800179c9 KC |
1054 | return 0; |
1055 | ||
245d7369 | 1056 | audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); |
800179c9 KC |
1057 | return -EPERM; |
1058 | } | |
1059 | ||
30aba665 SM |
1060 | /** |
1061 | * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory | |
1062 | * should be allowed, or not, on files that already | |
1063 | * exist. | |
d0cb5018 AV |
1064 | * @dir_mode: mode bits of directory |
1065 | * @dir_uid: owner of directory | |
30aba665 SM |
1066 | * @inode: the inode of the file to open |
1067 | * | |
1068 | * Block an O_CREAT open of a FIFO (or a regular file) when: | |
1069 | * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled | |
1070 | * - the file already exists | |
1071 | * - we are in a sticky directory | |
1072 | * - we don't own the file | |
1073 | * - the owner of the directory doesn't own the file | |
1074 | * - the directory is world writable | |
1075 | * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 | |
1076 | * the directory doesn't have to be world writable: being group writable will | |
1077 | * be enough. | |
1078 | * | |
1079 | * Returns 0 if the open is allowed, -ve on error. | |
1080 | */ | |
d0cb5018 | 1081 | static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid, |
30aba665 SM |
1082 | struct inode * const inode) |
1083 | { | |
1084 | if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || | |
1085 | (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || | |
d0cb5018 AV |
1086 | likely(!(dir_mode & S_ISVTX)) || |
1087 | uid_eq(inode->i_uid, dir_uid) || | |
30aba665 SM |
1088 | uid_eq(current_fsuid(), inode->i_uid)) |
1089 | return 0; | |
1090 | ||
d0cb5018 AV |
1091 | if (likely(dir_mode & 0002) || |
1092 | (dir_mode & 0020 && | |
30aba665 SM |
1093 | ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || |
1094 | (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { | |
245d7369 KC |
1095 | const char *operation = S_ISFIFO(inode->i_mode) ? |
1096 | "sticky_create_fifo" : | |
1097 | "sticky_create_regular"; | |
1098 | audit_log_path_denied(AUDIT_ANOM_CREAT, operation); | |
30aba665 SM |
1099 | return -EACCES; |
1100 | } | |
1101 | return 0; | |
1102 | } | |
1103 | ||
f015f126 DH |
1104 | /* |
1105 | * follow_up - Find the mountpoint of path's vfsmount | |
1106 | * | |
1107 | * Given a path, find the mountpoint of its source file system. | |
1108 | * Replace @path with the path of the mountpoint in the parent mount. | |
1109 | * Up is towards /. | |
1110 | * | |
1111 | * Return 1 if we went up a level and 0 if we were already at the | |
1112 | * root. | |
1113 | */ | |
bab77ebf | 1114 | int follow_up(struct path *path) |
1da177e4 | 1115 | { |
0714a533 AV |
1116 | struct mount *mnt = real_mount(path->mnt); |
1117 | struct mount *parent; | |
1da177e4 | 1118 | struct dentry *mountpoint; |
99b7db7b | 1119 | |
48a066e7 | 1120 | read_seqlock_excl(&mount_lock); |
0714a533 | 1121 | parent = mnt->mnt_parent; |
3c0a6163 | 1122 | if (parent == mnt) { |
48a066e7 | 1123 | read_sequnlock_excl(&mount_lock); |
1da177e4 LT |
1124 | return 0; |
1125 | } | |
0714a533 | 1126 | mntget(&parent->mnt); |
a73324da | 1127 | mountpoint = dget(mnt->mnt_mountpoint); |
48a066e7 | 1128 | read_sequnlock_excl(&mount_lock); |
bab77ebf AV |
1129 | dput(path->dentry); |
1130 | path->dentry = mountpoint; | |
1131 | mntput(path->mnt); | |
0714a533 | 1132 | path->mnt = &parent->mnt; |
1da177e4 LT |
1133 | return 1; |
1134 | } | |
4d359507 | 1135 | EXPORT_SYMBOL(follow_up); |
1da177e4 | 1136 | |
b5c84bf6 | 1137 | /* |
9875cf80 DH |
1138 | * Perform an automount |
1139 | * - return -EISDIR to tell follow_managed() to stop and return the path we | |
1140 | * were called with. | |
1da177e4 | 1141 | */ |
1c9f5e06 | 1142 | static int follow_automount(struct path *path, int *count, unsigned lookup_flags) |
31e6b01f | 1143 | { |
25e195aa | 1144 | struct dentry *dentry = path->dentry; |
9875cf80 | 1145 | |
0ec26fd0 MS |
1146 | /* We don't want to mount if someone's just doing a stat - |
1147 | * unless they're stat'ing a directory and appended a '/' to | |
1148 | * the name. | |
1149 | * | |
1150 | * We do, however, want to mount if someone wants to open or | |
1151 | * create a file of any type under the mountpoint, wants to | |
1152 | * traverse through the mountpoint or wants to open the | |
1153 | * mounted directory. Also, autofs may mark negative dentries | |
1154 | * as being automount points. These will need the attentions | |
1155 | * of the daemon to instantiate them before they can be used. | |
9875cf80 | 1156 | */ |
1c9f5e06 | 1157 | if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
5d38f049 | 1158 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && |
25e195aa | 1159 | dentry->d_inode) |
5d38f049 | 1160 | return -EISDIR; |
0ec26fd0 | 1161 | |
1c9f5e06 | 1162 | if (count && (*count)++ >= MAXSYMLINKS) |
9875cf80 DH |
1163 | return -ELOOP; |
1164 | ||
25e195aa | 1165 | return finish_automount(dentry->d_op->d_automount(path), path); |
463ffb2e AV |
1166 | } |
1167 | ||
9875cf80 | 1168 | /* |
9deed3eb AV |
1169 | * mount traversal - out-of-line part. One note on ->d_flags accesses - |
1170 | * dentries are pinned but not locked here, so negative dentry can go | |
1171 | * positive right under us. Use of smp_load_acquire() provides a barrier | |
1172 | * sufficient for ->d_inode and ->d_flags consistency. | |
9875cf80 | 1173 | */ |
9deed3eb AV |
1174 | static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, |
1175 | int *count, unsigned lookup_flags) | |
1da177e4 | 1176 | { |
9deed3eb | 1177 | struct vfsmount *mnt = path->mnt; |
9875cf80 | 1178 | bool need_mntput = false; |
8aef1884 | 1179 | int ret = 0; |
9875cf80 | 1180 | |
9deed3eb | 1181 | while (flags & DCACHE_MANAGED_DENTRY) { |
cc53ce53 DH |
1182 | /* Allow the filesystem to manage the transit without i_mutex |
1183 | * being held. */ | |
d41efb52 | 1184 | if (flags & DCACHE_MANAGE_TRANSIT) { |
fb5f51c7 | 1185 | ret = path->dentry->d_op->d_manage(path, false); |
508c8772 | 1186 | flags = smp_load_acquire(&path->dentry->d_flags); |
cc53ce53 | 1187 | if (ret < 0) |
8aef1884 | 1188 | break; |
cc53ce53 DH |
1189 | } |
1190 | ||
9deed3eb | 1191 | if (flags & DCACHE_MOUNTED) { // something's mounted on it.. |
9875cf80 | 1192 | struct vfsmount *mounted = lookup_mnt(path); |
9deed3eb | 1193 | if (mounted) { // ... in our namespace |
9875cf80 DH |
1194 | dput(path->dentry); |
1195 | if (need_mntput) | |
1196 | mntput(path->mnt); | |
1197 | path->mnt = mounted; | |
1198 | path->dentry = dget(mounted->mnt_root); | |
9deed3eb AV |
1199 | // here we know it's positive |
1200 | flags = path->dentry->d_flags; | |
9875cf80 DH |
1201 | need_mntput = true; |
1202 | continue; | |
1203 | } | |
9875cf80 DH |
1204 | } |
1205 | ||
9deed3eb AV |
1206 | if (!(flags & DCACHE_NEED_AUTOMOUNT)) |
1207 | break; | |
9875cf80 | 1208 | |
9deed3eb AV |
1209 | // uncovered automount point |
1210 | ret = follow_automount(path, count, lookup_flags); | |
1211 | flags = smp_load_acquire(&path->dentry->d_flags); | |
1212 | if (ret < 0) | |
1213 | break; | |
1da177e4 | 1214 | } |
8aef1884 | 1215 | |
9deed3eb AV |
1216 | if (ret == -EISDIR) |
1217 | ret = 0; | |
1218 | // possible if you race with several mount --move | |
1219 | if (need_mntput && path->mnt == mnt) | |
1220 | mntput(path->mnt); | |
1221 | if (!ret && unlikely(d_flags_negative(flags))) | |
d41efb52 | 1222 | ret = -ENOENT; |
9deed3eb | 1223 | *jumped = need_mntput; |
8402752e | 1224 | return ret; |
1da177e4 LT |
1225 | } |
1226 | ||
9deed3eb AV |
1227 | static inline int traverse_mounts(struct path *path, bool *jumped, |
1228 | int *count, unsigned lookup_flags) | |
1229 | { | |
1230 | unsigned flags = smp_load_acquire(&path->dentry->d_flags); | |
1231 | ||
1232 | /* fastpath */ | |
1233 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { | |
1234 | *jumped = false; | |
1235 | if (unlikely(d_flags_negative(flags))) | |
1236 | return -ENOENT; | |
1237 | return 0; | |
1238 | } | |
1239 | return __traverse_mounts(path, flags, jumped, count, lookup_flags); | |
1240 | } | |
1241 | ||
cc53ce53 | 1242 | int follow_down_one(struct path *path) |
1da177e4 LT |
1243 | { |
1244 | struct vfsmount *mounted; | |
1245 | ||
1c755af4 | 1246 | mounted = lookup_mnt(path); |
1da177e4 | 1247 | if (mounted) { |
9393bd07 AV |
1248 | dput(path->dentry); |
1249 | mntput(path->mnt); | |
1250 | path->mnt = mounted; | |
1251 | path->dentry = dget(mounted->mnt_root); | |
1da177e4 LT |
1252 | return 1; |
1253 | } | |
1254 | return 0; | |
1255 | } | |
4d359507 | 1256 | EXPORT_SYMBOL(follow_down_one); |
1da177e4 | 1257 | |
9deed3eb AV |
1258 | /* |
1259 | * Follow down to the covering mount currently visible to userspace. At each | |
1260 | * point, the filesystem owning that dentry may be queried as to whether the | |
1261 | * caller is permitted to proceed or not. | |
1262 | */ | |
1263 | int follow_down(struct path *path) | |
1264 | { | |
1265 | struct vfsmount *mnt = path->mnt; | |
1266 | bool jumped; | |
1267 | int ret = traverse_mounts(path, &jumped, NULL, 0); | |
1268 | ||
1269 | if (path->mnt != mnt) | |
1270 | mntput(mnt); | |
1271 | return ret; | |
1272 | } | |
1273 | EXPORT_SYMBOL(follow_down); | |
1274 | ||
9875cf80 | 1275 | /* |
287548e4 AV |
1276 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
1277 | * we meet a managed dentry that would need blocking. | |
9875cf80 DH |
1278 | */ |
1279 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, | |
254cf582 | 1280 | struct inode **inode, unsigned *seqp) |
9875cf80 | 1281 | { |
ea936aeb AV |
1282 | struct dentry *dentry = path->dentry; |
1283 | unsigned int flags = dentry->d_flags; | |
1284 | ||
1285 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) | |
1286 | return true; | |
1287 | ||
1288 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) | |
1289 | return false; | |
1290 | ||
62a7375e | 1291 | for (;;) { |
62a7375e IK |
1292 | /* |
1293 | * Don't forget we might have a non-mountpoint managed dentry | |
1294 | * that wants to block transit. | |
1295 | */ | |
ea936aeb AV |
1296 | if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { |
1297 | int res = dentry->d_op->d_manage(path, true); | |
1298 | if (res) | |
1299 | return res == -EISDIR; | |
1300 | flags = dentry->d_flags; | |
b8faf035 | 1301 | } |
62a7375e | 1302 | |
ea936aeb AV |
1303 | if (flags & DCACHE_MOUNTED) { |
1304 | struct mount *mounted = __lookup_mnt(path->mnt, dentry); | |
1305 | if (mounted) { | |
1306 | path->mnt = &mounted->mnt; | |
1307 | dentry = path->dentry = mounted->mnt.mnt_root; | |
1308 | nd->flags |= LOOKUP_JUMPED; | |
1309 | *seqp = read_seqcount_begin(&dentry->d_seq); | |
1310 | *inode = dentry->d_inode; | |
1311 | /* | |
1312 | * We don't need to re-check ->d_seq after this | |
1313 | * ->d_inode read - there will be an RCU delay | |
1314 | * between mount hash removal and ->mnt_root | |
1315 | * becoming unpinned. | |
1316 | */ | |
1317 | flags = dentry->d_flags; | |
1318 | continue; | |
1319 | } | |
1320 | if (read_seqretry(&mount_lock, nd->m_seq)) | |
1321 | return false; | |
1322 | } | |
1323 | return !(flags & DCACHE_NEED_AUTOMOUNT); | |
9875cf80 | 1324 | } |
287548e4 AV |
1325 | } |
1326 | ||
db3c9ade AV |
1327 | static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, |
1328 | struct path *path, struct inode **inode, | |
1329 | unsigned int *seqp) | |
bd7c4b50 | 1330 | { |
9deed3eb | 1331 | bool jumped; |
db3c9ade | 1332 | int ret; |
bd7c4b50 | 1333 | |
db3c9ade AV |
1334 | path->mnt = nd->path.mnt; |
1335 | path->dentry = dentry; | |
c153007b AV |
1336 | if (nd->flags & LOOKUP_RCU) { |
1337 | unsigned int seq = *seqp; | |
1338 | if (unlikely(!*inode)) | |
1339 | return -ENOENT; | |
1340 | if (likely(__follow_mount_rcu(nd, path, inode, seqp))) | |
9deed3eb | 1341 | return 0; |
c153007b AV |
1342 | if (unlazy_child(nd, dentry, seq)) |
1343 | return -ECHILD; | |
1344 | // *path might've been clobbered by __follow_mount_rcu() | |
1345 | path->mnt = nd->path.mnt; | |
1346 | path->dentry = dentry; | |
1347 | } | |
9deed3eb AV |
1348 | ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); |
1349 | if (jumped) { | |
1350 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) | |
1351 | ret = -EXDEV; | |
1352 | else | |
1353 | nd->flags |= LOOKUP_JUMPED; | |
1354 | } | |
1355 | if (unlikely(ret)) { | |
1356 | dput(path->dentry); | |
1357 | if (path->mnt != nd->path.mnt) | |
1358 | mntput(path->mnt); | |
1359 | } else { | |
bd7c4b50 AV |
1360 | *inode = d_backing_inode(path->dentry); |
1361 | *seqp = 0; /* out of RCU mode, so the value doesn't matter */ | |
1362 | } | |
1363 | return ret; | |
1364 | } | |
1365 | ||
9875cf80 DH |
1366 | /* |
1367 | * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() | |
1368 | */ | |
1369 | static void follow_mount(struct path *path) | |
1370 | { | |
1371 | while (d_mountpoint(path->dentry)) { | |
1372 | struct vfsmount *mounted = lookup_mnt(path); | |
1373 | if (!mounted) | |
1374 | break; | |
1375 | dput(path->dentry); | |
1376 | mntput(path->mnt); | |
1377 | path->mnt = mounted; | |
1378 | path->dentry = dget(mounted->mnt_root); | |
1379 | } | |
1380 | } | |
1381 | ||
baa03890 | 1382 | /* |
f4fdace9 OD |
1383 | * This looks up the name in dcache and possibly revalidates the found dentry. |
1384 | * NULL is returned if the dentry does not exist in the cache. | |
baa03890 | 1385 | */ |
e3c13928 AV |
1386 | static struct dentry *lookup_dcache(const struct qstr *name, |
1387 | struct dentry *dir, | |
6c51e513 | 1388 | unsigned int flags) |
baa03890 | 1389 | { |
a89f8337 | 1390 | struct dentry *dentry = d_lookup(dir, name); |
bad61189 | 1391 | if (dentry) { |
a89f8337 AV |
1392 | int error = d_revalidate(dentry, flags); |
1393 | if (unlikely(error <= 0)) { | |
1394 | if (!error) | |
1395 | d_invalidate(dentry); | |
1396 | dput(dentry); | |
1397 | return ERR_PTR(error); | |
bad61189 MS |
1398 | } |
1399 | } | |
baa03890 NP |
1400 | return dentry; |
1401 | } | |
1402 | ||
44396f4b | 1403 | /* |
a03ece5f AV |
1404 | * Parent directory has inode locked exclusive. This is one |
1405 | * and only case when ->lookup() gets called on non in-lookup | |
1406 | * dentries - as the matter of fact, this only gets called | |
1407 | * when directory is guaranteed to have no in-lookup children | |
1408 | * at all. | |
44396f4b | 1409 | */ |
e3c13928 | 1410 | static struct dentry *__lookup_hash(const struct qstr *name, |
72bd866a | 1411 | struct dentry *base, unsigned int flags) |
a3255546 | 1412 | { |
6c51e513 | 1413 | struct dentry *dentry = lookup_dcache(name, base, flags); |
a03ece5f AV |
1414 | struct dentry *old; |
1415 | struct inode *dir = base->d_inode; | |
a3255546 | 1416 | |
6c51e513 | 1417 | if (dentry) |
bad61189 | 1418 | return dentry; |
a3255546 | 1419 | |
a03ece5f AV |
1420 | /* Don't create child dentry for a dead directory. */ |
1421 | if (unlikely(IS_DEADDIR(dir))) | |
1422 | return ERR_PTR(-ENOENT); | |
1423 | ||
6c51e513 AV |
1424 | dentry = d_alloc(base, name); |
1425 | if (unlikely(!dentry)) | |
1426 | return ERR_PTR(-ENOMEM); | |
1427 | ||
a03ece5f AV |
1428 | old = dir->i_op->lookup(dir, dentry, flags); |
1429 | if (unlikely(old)) { | |
1430 | dput(dentry); | |
1431 | dentry = old; | |
1432 | } | |
1433 | return dentry; | |
a3255546 AV |
1434 | } |
1435 | ||
20e34357 AV |
1436 | static struct dentry *lookup_fast(struct nameidata *nd, |
1437 | struct inode **inode, | |
1438 | unsigned *seqp) | |
1da177e4 | 1439 | { |
31e6b01f | 1440 | struct dentry *dentry, *parent = nd->path.dentry; |
5a18fff2 | 1441 | int status = 1; |
9875cf80 | 1442 | |
b04f784e NP |
1443 | /* |
1444 | * Rename seqlock is not required here because in the off chance | |
5d0f49c1 AV |
1445 | * of a false negative due to a concurrent rename, the caller is |
1446 | * going to fall back to non-racy lookup. | |
b04f784e | 1447 | */ |
31e6b01f NP |
1448 | if (nd->flags & LOOKUP_RCU) { |
1449 | unsigned seq; | |
da53be12 | 1450 | dentry = __d_lookup_rcu(parent, &nd->last, &seq); |
5d0f49c1 | 1451 | if (unlikely(!dentry)) { |
4675ac39 | 1452 | if (unlazy_walk(nd)) |
20e34357 AV |
1453 | return ERR_PTR(-ECHILD); |
1454 | return NULL; | |
5d0f49c1 | 1455 | } |
5a18fff2 | 1456 | |
12f8ad4b LT |
1457 | /* |
1458 | * This sequence count validates that the inode matches | |
1459 | * the dentry name information from lookup. | |
1460 | */ | |
63afdfc7 | 1461 | *inode = d_backing_inode(dentry); |
5d0f49c1 | 1462 | if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) |
20e34357 | 1463 | return ERR_PTR(-ECHILD); |
12f8ad4b LT |
1464 | |
1465 | /* | |
1466 | * This sequence count validates that the parent had no | |
1467 | * changes while we did the lookup of the dentry above. | |
1468 | * | |
1469 | * The memory barrier in read_seqcount_begin of child is | |
1470 | * enough, we can use __read_seqcount_retry here. | |
1471 | */ | |
5d0f49c1 | 1472 | if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq))) |
20e34357 | 1473 | return ERR_PTR(-ECHILD); |
5a18fff2 | 1474 | |
254cf582 | 1475 | *seqp = seq; |
a89f8337 | 1476 | status = d_revalidate(dentry, nd->flags); |
c153007b | 1477 | if (likely(status > 0)) |
20e34357 | 1478 | return dentry; |
4675ac39 | 1479 | if (unlazy_child(nd, dentry, seq)) |
20e34357 | 1480 | return ERR_PTR(-ECHILD); |
209a7fb2 AV |
1481 | if (unlikely(status == -ECHILD)) |
1482 | /* we'd been told to redo it in non-rcu mode */ | |
1483 | status = d_revalidate(dentry, nd->flags); | |
5a18fff2 | 1484 | } else { |
e97cdc87 | 1485 | dentry = __d_lookup(parent, &nd->last); |
5d0f49c1 | 1486 | if (unlikely(!dentry)) |
20e34357 | 1487 | return NULL; |
a89f8337 | 1488 | status = d_revalidate(dentry, nd->flags); |
9875cf80 | 1489 | } |
5a18fff2 | 1490 | if (unlikely(status <= 0)) { |
e9742b53 | 1491 | if (!status) |
5d0f49c1 | 1492 | d_invalidate(dentry); |
5542aa2f | 1493 | dput(dentry); |
20e34357 | 1494 | return ERR_PTR(status); |
24643087 | 1495 | } |
20e34357 | 1496 | return dentry; |
697f514d MS |
1497 | } |
1498 | ||
1499 | /* Fast lookup failed, do it the slow way */ | |
88d8331a AV |
1500 | static struct dentry *__lookup_slow(const struct qstr *name, |
1501 | struct dentry *dir, | |
1502 | unsigned int flags) | |
697f514d | 1503 | { |
88d8331a | 1504 | struct dentry *dentry, *old; |
1936386e | 1505 | struct inode *inode = dir->d_inode; |
d9171b93 | 1506 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
1936386e | 1507 | |
1936386e | 1508 | /* Don't go there if it's already dead */ |
94bdd655 | 1509 | if (unlikely(IS_DEADDIR(inode))) |
88d8331a | 1510 | return ERR_PTR(-ENOENT); |
94bdd655 | 1511 | again: |
d9171b93 | 1512 | dentry = d_alloc_parallel(dir, name, &wq); |
94bdd655 | 1513 | if (IS_ERR(dentry)) |
88d8331a | 1514 | return dentry; |
94bdd655 | 1515 | if (unlikely(!d_in_lookup(dentry))) { |
c64cd6e3 AV |
1516 | int error = d_revalidate(dentry, flags); |
1517 | if (unlikely(error <= 0)) { | |
1518 | if (!error) { | |
1519 | d_invalidate(dentry); | |
949a852e | 1520 | dput(dentry); |
c64cd6e3 | 1521 | goto again; |
949a852e | 1522 | } |
c64cd6e3 AV |
1523 | dput(dentry); |
1524 | dentry = ERR_PTR(error); | |
949a852e | 1525 | } |
94bdd655 AV |
1526 | } else { |
1527 | old = inode->i_op->lookup(inode, dentry, flags); | |
1528 | d_lookup_done(dentry); | |
1529 | if (unlikely(old)) { | |
1530 | dput(dentry); | |
1531 | dentry = old; | |
949a852e AV |
1532 | } |
1533 | } | |
e3c13928 | 1534 | return dentry; |
1da177e4 LT |
1535 | } |
1536 | ||
88d8331a AV |
1537 | static struct dentry *lookup_slow(const struct qstr *name, |
1538 | struct dentry *dir, | |
1539 | unsigned int flags) | |
1540 | { | |
1541 | struct inode *inode = dir->d_inode; | |
1542 | struct dentry *res; | |
1543 | inode_lock_shared(inode); | |
1544 | res = __lookup_slow(name, dir, flags); | |
1545 | inode_unlock_shared(inode); | |
1546 | return res; | |
1547 | } | |
1548 | ||
52094c8a AV |
1549 | static inline int may_lookup(struct nameidata *nd) |
1550 | { | |
1551 | if (nd->flags & LOOKUP_RCU) { | |
4ad5abb3 | 1552 | int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); |
52094c8a AV |
1553 | if (err != -ECHILD) |
1554 | return err; | |
4675ac39 | 1555 | if (unlazy_walk(nd)) |
52094c8a AV |
1556 | return -ECHILD; |
1557 | } | |
4ad5abb3 | 1558 | return inode_permission(nd->inode, MAY_EXEC); |
52094c8a AV |
1559 | } |
1560 | ||
b1a81972 AV |
1561 | enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; |
1562 | ||
06708adb | 1563 | static const char *pick_link(struct nameidata *nd, struct path *link, |
b1a81972 | 1564 | struct inode *inode, unsigned seq, int flags) |
d63ff28f | 1565 | { |
1cf2665b | 1566 | struct saved *last; |
ad6cc4c3 AV |
1567 | const char *res; |
1568 | int error; | |
1569 | ||
756daf26 | 1570 | if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) { |
626de996 | 1571 | path_to_nameidata(link, nd); |
06708adb | 1572 | return ERR_PTR(-ELOOP); |
626de996 | 1573 | } |
bc40aee0 | 1574 | if (!(nd->flags & LOOKUP_RCU)) { |
7973387a AV |
1575 | if (link->mnt == nd->path.mnt) |
1576 | mntget(link->mnt); | |
d63ff28f | 1577 | } |
626de996 AV |
1578 | error = nd_alloc_stack(nd); |
1579 | if (unlikely(error)) { | |
bc40aee0 | 1580 | if (error == -ECHILD) { |
ad1633a1 AV |
1581 | if (unlikely(!legitimize_path(nd, link, seq))) { |
1582 | drop_links(nd); | |
1583 | nd->depth = 0; | |
1584 | nd->flags &= ~LOOKUP_RCU; | |
1585 | nd->path.mnt = NULL; | |
1586 | nd->path.dentry = NULL; | |
ad1633a1 | 1587 | rcu_read_unlock(); |
4675ac39 | 1588 | } else if (likely(unlazy_walk(nd)) == 0) |
ad1633a1 | 1589 | error = nd_alloc_stack(nd); |
bc40aee0 AV |
1590 | } |
1591 | if (error) { | |
1592 | path_put(link); | |
06708adb | 1593 | return ERR_PTR(error); |
bc40aee0 | 1594 | } |
626de996 AV |
1595 | } |
1596 | ||
ab104923 | 1597 | last = nd->stack + nd->depth++; |
1cf2665b | 1598 | last->link = *link; |
fceef393 | 1599 | clear_delayed_call(&last->done); |
0450b2d1 | 1600 | last->seq = seq; |
ad6cc4c3 | 1601 | |
b1a81972 | 1602 | if (flags & WALK_TRAILING) { |
ad6cc4c3 AV |
1603 | error = may_follow_link(nd, inode); |
1604 | if (unlikely(error)) | |
1605 | return ERR_PTR(error); | |
1606 | } | |
1607 | ||
1608 | if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS)) | |
1609 | return ERR_PTR(-ELOOP); | |
1610 | ||
1611 | if (!(nd->flags & LOOKUP_RCU)) { | |
1612 | touch_atime(&last->link); | |
1613 | cond_resched(); | |
1614 | } else if (atime_needs_update(&last->link, inode)) { | |
1615 | if (unlikely(unlazy_walk(nd))) | |
1616 | return ERR_PTR(-ECHILD); | |
1617 | touch_atime(&last->link); | |
1618 | } | |
1619 | ||
1620 | error = security_inode_follow_link(link->dentry, inode, | |
1621 | nd->flags & LOOKUP_RCU); | |
1622 | if (unlikely(error)) | |
1623 | return ERR_PTR(error); | |
1624 | ||
ad6cc4c3 AV |
1625 | res = READ_ONCE(inode->i_link); |
1626 | if (!res) { | |
1627 | const char * (*get)(struct dentry *, struct inode *, | |
1628 | struct delayed_call *); | |
1629 | get = inode->i_op->get_link; | |
1630 | if (nd->flags & LOOKUP_RCU) { | |
1631 | res = get(NULL, inode, &last->done); | |
1632 | if (res == ERR_PTR(-ECHILD)) { | |
1633 | if (unlikely(unlazy_walk(nd))) | |
1634 | return ERR_PTR(-ECHILD); | |
1635 | res = get(link->dentry, inode, &last->done); | |
1636 | } | |
1637 | } else { | |
1638 | res = get(link->dentry, inode, &last->done); | |
1639 | } | |
1640 | if (!res) | |
1641 | goto all_done; | |
1642 | if (IS_ERR(res)) | |
1643 | return res; | |
1644 | } | |
1645 | if (*res == '/') { | |
1646 | error = nd_jump_root(nd); | |
1647 | if (unlikely(error)) | |
1648 | return ERR_PTR(error); | |
1649 | while (unlikely(*++res == '/')) | |
1650 | ; | |
1651 | } | |
1652 | if (*res) | |
1653 | return res; | |
1654 | all_done: // pure jump | |
1655 | put_link(nd); | |
1656 | return NULL; | |
d63ff28f AV |
1657 | } |
1658 | ||
3ddcd056 LT |
1659 | /* |
1660 | * Do we need to follow links? We _really_ want to be able | |
1661 | * to do this check without having to look at inode->i_op, | |
1662 | * so we keep a cache of "no, this doesn't need follow_link" | |
1663 | * for the common case. | |
1664 | */ | |
b0417d2c | 1665 | static const char *step_into(struct nameidata *nd, int flags, |
cbae4d12 | 1666 | struct dentry *dentry, struct inode *inode, unsigned seq) |
3ddcd056 | 1667 | { |
cbae4d12 AV |
1668 | struct path path; |
1669 | int err = handle_mounts(nd, dentry, &path, &inode, &seq); | |
1670 | ||
1671 | if (err < 0) | |
b0417d2c | 1672 | return ERR_PTR(err); |
cbae4d12 | 1673 | if (likely(!d_is_symlink(path.dentry)) || |
8c4efe22 | 1674 | ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || |
aca2903e | 1675 | (flags & WALK_NOFOLLOW)) { |
8f64fb1c | 1676 | /* not a symlink or should not follow */ |
cbae4d12 | 1677 | path_to_nameidata(&path, nd); |
8f64fb1c AV |
1678 | nd->inode = inode; |
1679 | nd->seq = seq; | |
b0417d2c | 1680 | return NULL; |
8f64fb1c | 1681 | } |
a7f77542 AV |
1682 | /* make sure that d_is_symlink above matches inode */ |
1683 | if (nd->flags & LOOKUP_RCU) { | |
cbae4d12 | 1684 | if (read_seqcount_retry(&path.dentry->d_seq, seq)) |
b0417d2c | 1685 | return ERR_PTR(-ECHILD); |
a7f77542 | 1686 | } |
b1a81972 | 1687 | return pick_link(nd, &path, inode, seq, flags); |
3ddcd056 LT |
1688 | } |
1689 | ||
c2df1968 AV |
1690 | static struct dentry *follow_dotdot_rcu(struct nameidata *nd, |
1691 | struct inode **inodep, | |
1692 | unsigned *seqp) | |
957dd41d | 1693 | { |
12487f30 | 1694 | struct dentry *parent, *old; |
957dd41d | 1695 | |
12487f30 AV |
1696 | if (path_equal(&nd->path, &nd->root)) |
1697 | goto in_root; | |
1698 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { | |
1699 | while (1) { | |
957dd41d AV |
1700 | struct mount *mnt = real_mount(nd->path.mnt); |
1701 | struct mount *mparent = mnt->mnt_parent; | |
1702 | struct dentry *mountpoint = mnt->mnt_mountpoint; | |
c2df1968 | 1703 | struct inode *inode = mountpoint->d_inode; |
957dd41d AV |
1704 | unsigned seq = read_seqcount_begin(&mountpoint->d_seq); |
1705 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) | |
7521f22b | 1706 | return ERR_PTR(-ECHILD); |
957dd41d | 1707 | if (&mparent->mnt == nd->path.mnt) |
12487f30 | 1708 | goto in_root; |
957dd41d | 1709 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
7521f22b | 1710 | return ERR_PTR(-ECHILD); |
957dd41d AV |
1711 | /* we know that mountpoint was pinned */ |
1712 | nd->path.dentry = mountpoint; | |
1713 | nd->path.mnt = &mparent->mnt; | |
c2df1968 | 1714 | nd->inode = inode; |
957dd41d | 1715 | nd->seq = seq; |
12487f30 AV |
1716 | if (path_equal(&nd->path, &nd->root)) |
1717 | goto in_root; | |
1718 | if (nd->path.dentry != nd->path.mnt->mnt_root) | |
1719 | break; | |
957dd41d AV |
1720 | } |
1721 | } | |
12487f30 AV |
1722 | old = nd->path.dentry; |
1723 | parent = old->d_parent; | |
1724 | *inodep = parent->d_inode; | |
1725 | *seqp = read_seqcount_begin(&parent->d_seq); | |
1726 | if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) | |
1727 | return ERR_PTR(-ECHILD); | |
1728 | if (unlikely(!path_connected(nd->path.mnt, parent))) | |
1729 | return ERR_PTR(-ECHILD); | |
1730 | return parent; | |
1731 | in_root: | |
c2df1968 AV |
1732 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
1733 | return ERR_PTR(-ECHILD); | |
1734 | return NULL; | |
957dd41d AV |
1735 | } |
1736 | ||
c2df1968 AV |
1737 | static struct dentry *follow_dotdot(struct nameidata *nd, |
1738 | struct inode **inodep, | |
1739 | unsigned *seqp) | |
957dd41d | 1740 | { |
12487f30 AV |
1741 | struct dentry *parent; |
1742 | ||
1743 | if (path_equal(&nd->path, &nd->root)) | |
1744 | goto in_root; | |
1745 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { | |
1746 | while (1) { | |
1747 | if (!follow_up(&nd->path)) | |
1748 | goto in_root; | |
1749 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) | |
1750 | return ERR_PTR(-EXDEV); | |
1751 | if (path_equal(&nd->path, &nd->root)) | |
1752 | goto in_root; | |
1753 | if (nd->path.dentry != nd->path.mnt->mnt_root) | |
1754 | break; | |
957dd41d | 1755 | } |
957dd41d | 1756 | } |
12487f30 AV |
1757 | /* rare case of legitimate dget_parent()... */ |
1758 | parent = dget_parent(nd->path.dentry); | |
1759 | if (unlikely(!path_connected(nd->path.mnt, parent))) { | |
1760 | dput(parent); | |
1761 | return ERR_PTR(-ENOENT); | |
1762 | } | |
1763 | *seqp = 0; | |
1764 | *inodep = parent->d_inode; | |
1765 | return parent; | |
1766 | ||
1767 | in_root: | |
c2df1968 AV |
1768 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
1769 | return ERR_PTR(-EXDEV); | |
1770 | dget(nd->path.dentry); | |
1771 | return NULL; | |
957dd41d AV |
1772 | } |
1773 | ||
7521f22b | 1774 | static const char *handle_dots(struct nameidata *nd, int type) |
957dd41d AV |
1775 | { |
1776 | if (type == LAST_DOTDOT) { | |
7521f22b | 1777 | const char *error = NULL; |
c2df1968 AV |
1778 | struct dentry *parent; |
1779 | struct inode *inode; | |
1780 | unsigned seq; | |
957dd41d AV |
1781 | |
1782 | if (!nd->root.mnt) { | |
7521f22b | 1783 | error = ERR_PTR(set_root(nd)); |
957dd41d AV |
1784 | if (error) |
1785 | return error; | |
1786 | } | |
1787 | if (nd->flags & LOOKUP_RCU) | |
c2df1968 | 1788 | parent = follow_dotdot_rcu(nd, &inode, &seq); |
957dd41d | 1789 | else |
c2df1968 AV |
1790 | parent = follow_dotdot(nd, &inode, &seq); |
1791 | if (IS_ERR(parent)) | |
1792 | return ERR_CAST(parent); | |
1793 | if (unlikely(!parent)) | |
1794 | error = step_into(nd, WALK_NOFOLLOW, | |
1795 | nd->path.dentry, nd->inode, nd->seq); | |
1796 | else | |
1797 | error = step_into(nd, WALK_NOFOLLOW, | |
1798 | parent, inode, seq); | |
1799 | if (unlikely(error)) | |
957dd41d AV |
1800 | return error; |
1801 | ||
1802 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { | |
1803 | /* | |
1804 | * If there was a racing rename or mount along our | |
1805 | * path, then we can't be sure that ".." hasn't jumped | |
1806 | * above nd->root (and so userspace should retry or use | |
1807 | * some fallback). | |
1808 | */ | |
1809 | smp_rmb(); | |
1810 | if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))) | |
7521f22b | 1811 | return ERR_PTR(-EAGAIN); |
957dd41d | 1812 | if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))) |
7521f22b | 1813 | return ERR_PTR(-EAGAIN); |
957dd41d AV |
1814 | } |
1815 | } | |
7521f22b | 1816 | return NULL; |
957dd41d AV |
1817 | } |
1818 | ||
92d27016 | 1819 | static const char *walk_component(struct nameidata *nd, int flags) |
ce57dfc1 | 1820 | { |
db3c9ade | 1821 | struct dentry *dentry; |
ce57dfc1 | 1822 | struct inode *inode; |
254cf582 | 1823 | unsigned seq; |
ce57dfc1 AV |
1824 | /* |
1825 | * "." and ".." are special - ".." especially so because it has | |
1826 | * to be able to know about the current root directory and | |
1827 | * parent relationships. | |
1828 | */ | |
4693a547 | 1829 | if (unlikely(nd->last_type != LAST_NORM)) { |
1c4ff1a8 | 1830 | if (!(flags & WALK_MORE) && nd->depth) |
4693a547 | 1831 | put_link(nd); |
7521f22b | 1832 | return handle_dots(nd, nd->last_type); |
4693a547 | 1833 | } |
20e34357 AV |
1834 | dentry = lookup_fast(nd, &inode, &seq); |
1835 | if (IS_ERR(dentry)) | |
92d27016 | 1836 | return ERR_CAST(dentry); |
20e34357 | 1837 | if (unlikely(!dentry)) { |
db3c9ade AV |
1838 | dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); |
1839 | if (IS_ERR(dentry)) | |
92d27016 | 1840 | return ERR_CAST(dentry); |
ce57dfc1 | 1841 | } |
56676ec3 AV |
1842 | if (!(flags & WALK_MORE) && nd->depth) |
1843 | put_link(nd); | |
b0417d2c | 1844 | return step_into(nd, flags, dentry, inode, seq); |
ce57dfc1 AV |
1845 | } |
1846 | ||
bfcfaa77 LT |
1847 | /* |
1848 | * We can do the critical dentry name comparison and hashing | |
1849 | * operations one word at a time, but we are limited to: | |
1850 | * | |
1851 | * - Architectures with fast unaligned word accesses. We could | |
1852 | * do a "get_unaligned()" if this helps and is sufficiently | |
1853 | * fast. | |
1854 | * | |
bfcfaa77 LT |
1855 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
1856 | * do not trap on the (extremely unlikely) case of a page | |
1857 | * crossing operation. | |
1858 | * | |
1859 | * - Furthermore, we need an efficient 64-bit compile for the | |
1860 | * 64-bit case in order to generate the "number of bytes in | |
1861 | * the final mask". Again, that could be replaced with a | |
1862 | * efficient population count instruction or similar. | |
1863 | */ | |
1864 | #ifdef CONFIG_DCACHE_WORD_ACCESS | |
1865 | ||
f68e556e | 1866 | #include <asm/word-at-a-time.h> |
bfcfaa77 | 1867 | |
468a9428 | 1868 | #ifdef HASH_MIX |
bfcfaa77 | 1869 | |
468a9428 | 1870 | /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ |
bfcfaa77 | 1871 | |
468a9428 | 1872 | #elif defined(CONFIG_64BIT) |
0fed3ac8 | 1873 | /* |
2a18da7a GS |
1874 | * Register pressure in the mixing function is an issue, particularly |
1875 | * on 32-bit x86, but almost any function requires one state value and | |
1876 | * one temporary. Instead, use a function designed for two state values | |
1877 | * and no temporaries. | |
1878 | * | |
1879 | * This function cannot create a collision in only two iterations, so | |
1880 | * we have two iterations to achieve avalanche. In those two iterations, | |
1881 | * we have six layers of mixing, which is enough to spread one bit's | |
1882 | * influence out to 2^6 = 64 state bits. | |
1883 | * | |
1884 | * Rotate constants are scored by considering either 64 one-bit input | |
1885 | * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the | |
1886 | * probability of that delta causing a change to each of the 128 output | |
1887 | * bits, using a sample of random initial states. | |
1888 | * | |
1889 | * The Shannon entropy of the computed probabilities is then summed | |
1890 | * to produce a score. Ideally, any input change has a 50% chance of | |
1891 | * toggling any given output bit. | |
1892 | * | |
1893 | * Mixing scores (in bits) for (12,45): | |
1894 | * Input delta: 1-bit 2-bit | |
1895 | * 1 round: 713.3 42542.6 | |
1896 | * 2 rounds: 2753.7 140389.8 | |
1897 | * 3 rounds: 5954.1 233458.2 | |
1898 | * 4 rounds: 7862.6 256672.2 | |
1899 | * Perfect: 8192 258048 | |
1900 | * (64*128) (64*63/2 * 128) | |
0fed3ac8 | 1901 | */ |
2a18da7a GS |
1902 | #define HASH_MIX(x, y, a) \ |
1903 | ( x ^= (a), \ | |
1904 | y ^= x, x = rol64(x,12),\ | |
1905 | x += y, y = rol64(y,45),\ | |
1906 | y *= 9 ) | |
bfcfaa77 | 1907 | |
0fed3ac8 | 1908 | /* |
2a18da7a GS |
1909 | * Fold two longs into one 32-bit hash value. This must be fast, but |
1910 | * latency isn't quite as critical, as there is a fair bit of additional | |
1911 | * work done before the hash value is used. | |
0fed3ac8 | 1912 | */ |
2a18da7a | 1913 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
0fed3ac8 | 1914 | { |
2a18da7a GS |
1915 | y ^= x * GOLDEN_RATIO_64; |
1916 | y *= GOLDEN_RATIO_64; | |
1917 | return y >> 32; | |
0fed3ac8 GS |
1918 | } |
1919 | ||
bfcfaa77 LT |
1920 | #else /* 32-bit case */ |
1921 | ||
2a18da7a GS |
1922 | /* |
1923 | * Mixing scores (in bits) for (7,20): | |
1924 | * Input delta: 1-bit 2-bit | |
1925 | * 1 round: 330.3 9201.6 | |
1926 | * 2 rounds: 1246.4 25475.4 | |
1927 | * 3 rounds: 1907.1 31295.1 | |
1928 | * 4 rounds: 2042.3 31718.6 | |
1929 | * Perfect: 2048 31744 | |
1930 | * (32*64) (32*31/2 * 64) | |
1931 | */ | |
1932 | #define HASH_MIX(x, y, a) \ | |
1933 | ( x ^= (a), \ | |
1934 | y ^= x, x = rol32(x, 7),\ | |
1935 | x += y, y = rol32(y,20),\ | |
1936 | y *= 9 ) | |
bfcfaa77 | 1937 | |
2a18da7a | 1938 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
0fed3ac8 | 1939 | { |
2a18da7a GS |
1940 | /* Use arch-optimized multiply if one exists */ |
1941 | return __hash_32(y ^ __hash_32(x)); | |
0fed3ac8 GS |
1942 | } |
1943 | ||
bfcfaa77 LT |
1944 | #endif |
1945 | ||
2a18da7a GS |
1946 | /* |
1947 | * Return the hash of a string of known length. This is carfully | |
1948 | * designed to match hash_name(), which is the more critical function. | |
1949 | * In particular, we must end by hashing a final word containing 0..7 | |
1950 | * payload bytes, to match the way that hash_name() iterates until it | |
1951 | * finds the delimiter after the name. | |
1952 | */ | |
8387ff25 | 1953 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
bfcfaa77 | 1954 | { |
8387ff25 | 1955 | unsigned long a, x = 0, y = (unsigned long)salt; |
bfcfaa77 LT |
1956 | |
1957 | for (;;) { | |
fcfd2fbf GS |
1958 | if (!len) |
1959 | goto done; | |
e419b4cc | 1960 | a = load_unaligned_zeropad(name); |
bfcfaa77 LT |
1961 | if (len < sizeof(unsigned long)) |
1962 | break; | |
2a18da7a | 1963 | HASH_MIX(x, y, a); |
bfcfaa77 LT |
1964 | name += sizeof(unsigned long); |
1965 | len -= sizeof(unsigned long); | |
bfcfaa77 | 1966 | } |
2a18da7a | 1967 | x ^= a & bytemask_from_count(len); |
bfcfaa77 | 1968 | done: |
2a18da7a | 1969 | return fold_hash(x, y); |
bfcfaa77 LT |
1970 | } |
1971 | EXPORT_SYMBOL(full_name_hash); | |
1972 | ||
fcfd2fbf | 1973 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
8387ff25 | 1974 | u64 hashlen_string(const void *salt, const char *name) |
fcfd2fbf | 1975 | { |
8387ff25 LT |
1976 | unsigned long a = 0, x = 0, y = (unsigned long)salt; |
1977 | unsigned long adata, mask, len; | |
fcfd2fbf GS |
1978 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
1979 | ||
8387ff25 LT |
1980 | len = 0; |
1981 | goto inside; | |
1982 | ||
fcfd2fbf | 1983 | do { |
2a18da7a | 1984 | HASH_MIX(x, y, a); |
fcfd2fbf | 1985 | len += sizeof(unsigned long); |
8387ff25 | 1986 | inside: |
fcfd2fbf GS |
1987 | a = load_unaligned_zeropad(name+len); |
1988 | } while (!has_zero(a, &adata, &constants)); | |
1989 | ||
1990 | adata = prep_zero_mask(a, adata, &constants); | |
1991 | mask = create_zero_mask(adata); | |
2a18da7a | 1992 | x ^= a & zero_bytemask(mask); |
fcfd2fbf | 1993 | |
2a18da7a | 1994 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
fcfd2fbf GS |
1995 | } |
1996 | EXPORT_SYMBOL(hashlen_string); | |
1997 | ||
bfcfaa77 LT |
1998 | /* |
1999 | * Calculate the length and hash of the path component, and | |
d6bb3e90 | 2000 | * return the "hash_len" as the result. |
bfcfaa77 | 2001 | */ |
8387ff25 | 2002 | static inline u64 hash_name(const void *salt, const char *name) |
bfcfaa77 | 2003 | { |
8387ff25 LT |
2004 | unsigned long a = 0, b, x = 0, y = (unsigned long)salt; |
2005 | unsigned long adata, bdata, mask, len; | |
36126f8f | 2006 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
bfcfaa77 | 2007 | |
8387ff25 LT |
2008 | len = 0; |
2009 | goto inside; | |
2010 | ||
bfcfaa77 | 2011 | do { |
2a18da7a | 2012 | HASH_MIX(x, y, a); |
bfcfaa77 | 2013 | len += sizeof(unsigned long); |
8387ff25 | 2014 | inside: |
e419b4cc | 2015 | a = load_unaligned_zeropad(name+len); |
36126f8f LT |
2016 | b = a ^ REPEAT_BYTE('/'); |
2017 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); | |
2018 | ||
2019 | adata = prep_zero_mask(a, adata, &constants); | |
2020 | bdata = prep_zero_mask(b, bdata, &constants); | |
36126f8f | 2021 | mask = create_zero_mask(adata | bdata); |
2a18da7a | 2022 | x ^= a & zero_bytemask(mask); |
36126f8f | 2023 | |
2a18da7a | 2024 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
bfcfaa77 LT |
2025 | } |
2026 | ||
2a18da7a | 2027 | #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ |
bfcfaa77 | 2028 | |
fcfd2fbf | 2029 | /* Return the hash of a string of known length */ |
8387ff25 | 2030 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
0145acc2 | 2031 | { |
8387ff25 | 2032 | unsigned long hash = init_name_hash(salt); |
0145acc2 | 2033 | while (len--) |
fcfd2fbf | 2034 | hash = partial_name_hash((unsigned char)*name++, hash); |
0145acc2 LT |
2035 | return end_name_hash(hash); |
2036 | } | |
ae942ae7 | 2037 | EXPORT_SYMBOL(full_name_hash); |
0145acc2 | 2038 | |
fcfd2fbf | 2039 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
8387ff25 | 2040 | u64 hashlen_string(const void *salt, const char *name) |
fcfd2fbf | 2041 | { |
8387ff25 | 2042 | unsigned long hash = init_name_hash(salt); |
fcfd2fbf GS |
2043 | unsigned long len = 0, c; |
2044 | ||
2045 | c = (unsigned char)*name; | |
e0ab7af9 | 2046 | while (c) { |
fcfd2fbf GS |
2047 | len++; |
2048 | hash = partial_name_hash(c, hash); | |
2049 | c = (unsigned char)name[len]; | |
e0ab7af9 | 2050 | } |
fcfd2fbf GS |
2051 | return hashlen_create(end_name_hash(hash), len); |
2052 | } | |
f2a031b6 | 2053 | EXPORT_SYMBOL(hashlen_string); |
fcfd2fbf | 2054 | |
200e9ef7 LT |
2055 | /* |
2056 | * We know there's a real path component here of at least | |
2057 | * one character. | |
2058 | */ | |
8387ff25 | 2059 | static inline u64 hash_name(const void *salt, const char *name) |
200e9ef7 | 2060 | { |
8387ff25 | 2061 | unsigned long hash = init_name_hash(salt); |
200e9ef7 LT |
2062 | unsigned long len = 0, c; |
2063 | ||
2064 | c = (unsigned char)*name; | |
2065 | do { | |
2066 | len++; | |
2067 | hash = partial_name_hash(c, hash); | |
2068 | c = (unsigned char)name[len]; | |
2069 | } while (c && c != '/'); | |
d6bb3e90 | 2070 | return hashlen_create(end_name_hash(hash), len); |
200e9ef7 LT |
2071 | } |
2072 | ||
bfcfaa77 LT |
2073 | #endif |
2074 | ||
1da177e4 LT |
2075 | /* |
2076 | * Name resolution. | |
ea3834d9 PM |
2077 | * This is the basic name resolution function, turning a pathname into |
2078 | * the final dentry. We expect 'base' to be positive and a directory. | |
1da177e4 | 2079 | * |
ea3834d9 PM |
2080 | * Returns 0 and nd will have valid dentry and mnt on success. |
2081 | * Returns error and drops reference to input namei data on failure. | |
1da177e4 | 2082 | */ |
6de88d72 | 2083 | static int link_path_walk(const char *name, struct nameidata *nd) |
1da177e4 | 2084 | { |
d8d4611a | 2085 | int depth = 0; // depth <= nd->depth |
1da177e4 | 2086 | int err; |
32cd7468 | 2087 | |
b4c03536 | 2088 | nd->last_type = LAST_ROOT; |
c108837e | 2089 | nd->flags |= LOOKUP_PARENT; |
9b5858e9 AV |
2090 | if (IS_ERR(name)) |
2091 | return PTR_ERR(name); | |
1da177e4 LT |
2092 | while (*name=='/') |
2093 | name++; | |
2094 | if (!*name) | |
9e18f10a | 2095 | return 0; |
1da177e4 | 2096 | |
1da177e4 LT |
2097 | /* At this point we know we have a real path component. */ |
2098 | for(;;) { | |
92d27016 | 2099 | const char *link; |
d6bb3e90 | 2100 | u64 hash_len; |
fe479a58 | 2101 | int type; |
1da177e4 | 2102 | |
52094c8a | 2103 | err = may_lookup(nd); |
2a18da7a | 2104 | if (err) |
3595e234 | 2105 | return err; |
1da177e4 | 2106 | |
8387ff25 | 2107 | hash_len = hash_name(nd->path.dentry, name); |
1da177e4 | 2108 | |
fe479a58 | 2109 | type = LAST_NORM; |
d6bb3e90 | 2110 | if (name[0] == '.') switch (hashlen_len(hash_len)) { |
fe479a58 | 2111 | case 2: |
200e9ef7 | 2112 | if (name[1] == '.') { |
fe479a58 | 2113 | type = LAST_DOTDOT; |
16c2cd71 AV |
2114 | nd->flags |= LOOKUP_JUMPED; |
2115 | } | |
fe479a58 AV |
2116 | break; |
2117 | case 1: | |
2118 | type = LAST_DOT; | |
2119 | } | |
5a202bcd AV |
2120 | if (likely(type == LAST_NORM)) { |
2121 | struct dentry *parent = nd->path.dentry; | |
16c2cd71 | 2122 | nd->flags &= ~LOOKUP_JUMPED; |
5a202bcd | 2123 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
a060dc50 | 2124 | struct qstr this = { { .hash_len = hash_len }, .name = name }; |
da53be12 | 2125 | err = parent->d_op->d_hash(parent, &this); |
5a202bcd | 2126 | if (err < 0) |
3595e234 | 2127 | return err; |
d6bb3e90 LT |
2128 | hash_len = this.hash_len; |
2129 | name = this.name; | |
5a202bcd AV |
2130 | } |
2131 | } | |
fe479a58 | 2132 | |
d6bb3e90 LT |
2133 | nd->last.hash_len = hash_len; |
2134 | nd->last.name = name; | |
5f4a6a69 AV |
2135 | nd->last_type = type; |
2136 | ||
d6bb3e90 LT |
2137 | name += hashlen_len(hash_len); |
2138 | if (!*name) | |
bdf6cbf1 | 2139 | goto OK; |
200e9ef7 LT |
2140 | /* |
2141 | * If it wasn't NUL, we know it was '/'. Skip that | |
2142 | * slash, and continue until no more slashes. | |
2143 | */ | |
2144 | do { | |
d6bb3e90 LT |
2145 | name++; |
2146 | } while (unlikely(*name == '/')); | |
8620c238 AV |
2147 | if (unlikely(!*name)) { |
2148 | OK: | |
d8d4611a | 2149 | /* pathname or trailing symlink, done */ |
c108837e AV |
2150 | if (!depth) { |
2151 | nd->flags &= ~LOOKUP_PARENT; | |
8620c238 | 2152 | return 0; |
c108837e | 2153 | } |
8620c238 | 2154 | /* last component of nested symlink */ |
d8d4611a | 2155 | name = nd->stack[--depth].name; |
8c4efe22 | 2156 | link = walk_component(nd, 0); |
1c4ff1a8 AV |
2157 | } else { |
2158 | /* not the last component */ | |
8c4efe22 | 2159 | link = walk_component(nd, WALK_MORE); |
8620c238 | 2160 | } |
92d27016 AV |
2161 | if (unlikely(link)) { |
2162 | if (IS_ERR(link)) | |
2163 | return PTR_ERR(link); | |
2164 | /* a symlink to follow */ | |
d8d4611a | 2165 | nd->stack[depth++].name = name; |
92d27016 AV |
2166 | name = link; |
2167 | continue; | |
31e6b01f | 2168 | } |
97242f99 AV |
2169 | if (unlikely(!d_can_lookup(nd->path.dentry))) { |
2170 | if (nd->flags & LOOKUP_RCU) { | |
4675ac39 | 2171 | if (unlazy_walk(nd)) |
97242f99 AV |
2172 | return -ECHILD; |
2173 | } | |
3595e234 | 2174 | return -ENOTDIR; |
97242f99 | 2175 | } |
1da177e4 | 2176 | } |
1da177e4 LT |
2177 | } |
2178 | ||
edc2b1da | 2179 | /* must be paired with terminate_walk() */ |
c8a53ee5 | 2180 | static const char *path_init(struct nameidata *nd, unsigned flags) |
31e6b01f | 2181 | { |
740a1678 | 2182 | int error; |
c8a53ee5 | 2183 | const char *s = nd->name->name; |
31e6b01f | 2184 | |
c0eb027e LT |
2185 | if (!*s) |
2186 | flags &= ~LOOKUP_RCU; | |
edc2b1da AV |
2187 | if (flags & LOOKUP_RCU) |
2188 | rcu_read_lock(); | |
c0eb027e | 2189 | |
c108837e | 2190 | nd->flags = flags | LOOKUP_JUMPED; |
31e6b01f | 2191 | nd->depth = 0; |
ab87f9a5 AS |
2192 | |
2193 | nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); | |
2194 | nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); | |
2195 | smp_rmb(); | |
2196 | ||
5b6ca027 | 2197 | if (flags & LOOKUP_ROOT) { |
b18825a7 DH |
2198 | struct dentry *root = nd->root.dentry; |
2199 | struct inode *inode = root->d_inode; | |
93893862 AV |
2200 | if (*s && unlikely(!d_can_lookup(root))) |
2201 | return ERR_PTR(-ENOTDIR); | |
5b6ca027 AV |
2202 | nd->path = nd->root; |
2203 | nd->inode = inode; | |
2204 | if (flags & LOOKUP_RCU) { | |
ab87f9a5 | 2205 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
8f47a016 | 2206 | nd->root_seq = nd->seq; |
5b6ca027 AV |
2207 | } else { |
2208 | path_get(&nd->path); | |
2209 | } | |
368ee9ba | 2210 | return s; |
5b6ca027 AV |
2211 | } |
2212 | ||
31e6b01f | 2213 | nd->root.mnt = NULL; |
248fb5b9 AV |
2214 | nd->path.mnt = NULL; |
2215 | nd->path.dentry = NULL; | |
31e6b01f | 2216 | |
8db52c7e AS |
2217 | /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ |
2218 | if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { | |
740a1678 AS |
2219 | error = nd_jump_root(nd); |
2220 | if (unlikely(error)) | |
2221 | return ERR_PTR(error); | |
2222 | return s; | |
8db52c7e AS |
2223 | } |
2224 | ||
2225 | /* Relative pathname -- get the starting-point it is relative to. */ | |
2226 | if (nd->dfd == AT_FDCWD) { | |
e41f7d4e AV |
2227 | if (flags & LOOKUP_RCU) { |
2228 | struct fs_struct *fs = current->fs; | |
2229 | unsigned seq; | |
31e6b01f | 2230 | |
e41f7d4e AV |
2231 | do { |
2232 | seq = read_seqcount_begin(&fs->seq); | |
2233 | nd->path = fs->pwd; | |
ef55d917 | 2234 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e AV |
2235 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
2236 | } while (read_seqcount_retry(&fs->seq, seq)); | |
2237 | } else { | |
2238 | get_fs_pwd(current->fs, &nd->path); | |
ef55d917 | 2239 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e | 2240 | } |
31e6b01f | 2241 | } else { |
582aa64a | 2242 | /* Caller must check execute permissions on the starting path component */ |
c8a53ee5 | 2243 | struct fd f = fdget_raw(nd->dfd); |
31e6b01f NP |
2244 | struct dentry *dentry; |
2245 | ||
2903ff01 | 2246 | if (!f.file) |
368ee9ba | 2247 | return ERR_PTR(-EBADF); |
31e6b01f | 2248 | |
2903ff01 | 2249 | dentry = f.file->f_path.dentry; |
31e6b01f | 2250 | |
edc2b1da AV |
2251 | if (*s && unlikely(!d_can_lookup(dentry))) { |
2252 | fdput(f); | |
2253 | return ERR_PTR(-ENOTDIR); | |
f52e0c11 | 2254 | } |
31e6b01f | 2255 | |
2903ff01 | 2256 | nd->path = f.file->f_path; |
e41f7d4e | 2257 | if (flags & LOOKUP_RCU) { |
34a26b99 AV |
2258 | nd->inode = nd->path.dentry->d_inode; |
2259 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); | |
e41f7d4e | 2260 | } else { |
2903ff01 | 2261 | path_get(&nd->path); |
34a26b99 | 2262 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e | 2263 | } |
34a26b99 | 2264 | fdput(f); |
31e6b01f | 2265 | } |
8db52c7e | 2266 | |
adb21d2b AS |
2267 | /* For scoped-lookups we need to set the root to the dirfd as well. */ |
2268 | if (flags & LOOKUP_IS_SCOPED) { | |
2269 | nd->root = nd->path; | |
2270 | if (flags & LOOKUP_RCU) { | |
2271 | nd->root_seq = nd->seq; | |
2272 | } else { | |
2273 | path_get(&nd->root); | |
2274 | nd->flags |= LOOKUP_ROOT_GRABBED; | |
2275 | } | |
2276 | } | |
2277 | return s; | |
9b4a9b14 AV |
2278 | } |
2279 | ||
1ccac622 | 2280 | static inline const char *lookup_last(struct nameidata *nd) |
bd92d7fe AV |
2281 | { |
2282 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) | |
2283 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
2284 | ||
c108837e | 2285 | return walk_component(nd, WALK_TRAILING); |
bd92d7fe AV |
2286 | } |
2287 | ||
4f757f3c AV |
2288 | static int handle_lookup_down(struct nameidata *nd) |
2289 | { | |
c153007b | 2290 | if (!(nd->flags & LOOKUP_RCU)) |
db3c9ade | 2291 | dget(nd->path.dentry); |
b0417d2c AV |
2292 | return PTR_ERR(step_into(nd, WALK_NOFOLLOW, |
2293 | nd->path.dentry, nd->inode, nd->seq)); | |
4f757f3c AV |
2294 | } |
2295 | ||
9b4a9b14 | 2296 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
c8a53ee5 | 2297 | static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) |
9b4a9b14 | 2298 | { |
c8a53ee5 | 2299 | const char *s = path_init(nd, flags); |
bd92d7fe | 2300 | int err; |
31e6b01f | 2301 | |
9b5858e9 | 2302 | if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { |
4f757f3c | 2303 | err = handle_lookup_down(nd); |
5f336e72 AV |
2304 | if (unlikely(err < 0)) |
2305 | s = ERR_PTR(err); | |
4f757f3c AV |
2306 | } |
2307 | ||
1ccac622 AV |
2308 | while (!(err = link_path_walk(s, nd)) && |
2309 | (s = lookup_last(nd)) != NULL) | |
2310 | ; | |
9f1fafee AV |
2311 | if (!err) |
2312 | err = complete_walk(nd); | |
bd92d7fe | 2313 | |
deb106c6 AV |
2314 | if (!err && nd->flags & LOOKUP_DIRECTORY) |
2315 | if (!d_can_lookup(nd->path.dentry)) | |
bd23a539 | 2316 | err = -ENOTDIR; |
161aff1d AV |
2317 | if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { |
2318 | err = handle_lookup_down(nd); | |
2319 | nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please... | |
2320 | } | |
625b6d10 AV |
2321 | if (!err) { |
2322 | *path = nd->path; | |
2323 | nd->path.mnt = NULL; | |
2324 | nd->path.dentry = NULL; | |
2325 | } | |
2326 | terminate_walk(nd); | |
bd92d7fe | 2327 | return err; |
ee0827cd | 2328 | } |
31e6b01f | 2329 | |
31d921c7 DH |
2330 | int filename_lookup(int dfd, struct filename *name, unsigned flags, |
2331 | struct path *path, struct path *root) | |
ee0827cd | 2332 | { |
894bc8c4 | 2333 | int retval; |
9883d185 | 2334 | struct nameidata nd; |
abc9f5be AV |
2335 | if (IS_ERR(name)) |
2336 | return PTR_ERR(name); | |
9ad1aaa6 AV |
2337 | if (unlikely(root)) { |
2338 | nd.root = *root; | |
2339 | flags |= LOOKUP_ROOT; | |
2340 | } | |
9883d185 | 2341 | set_nameidata(&nd, dfd, name); |
c8a53ee5 | 2342 | retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); |
ee0827cd | 2343 | if (unlikely(retval == -ECHILD)) |
c8a53ee5 | 2344 | retval = path_lookupat(&nd, flags, path); |
ee0827cd | 2345 | if (unlikely(retval == -ESTALE)) |
c8a53ee5 | 2346 | retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); |
31e6b01f | 2347 | |
f78570dd | 2348 | if (likely(!retval)) |
161aff1d AV |
2349 | audit_inode(name, path->dentry, |
2350 | flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); | |
9883d185 | 2351 | restore_nameidata(); |
e4bd1c1a | 2352 | putname(name); |
170aa3d0 | 2353 | return retval; |
1da177e4 LT |
2354 | } |
2355 | ||
8bcb77fa | 2356 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
c8a53ee5 | 2357 | static int path_parentat(struct nameidata *nd, unsigned flags, |
391172c4 | 2358 | struct path *parent) |
8bcb77fa | 2359 | { |
c8a53ee5 | 2360 | const char *s = path_init(nd, flags); |
9b5858e9 | 2361 | int err = link_path_walk(s, nd); |
8bcb77fa AV |
2362 | if (!err) |
2363 | err = complete_walk(nd); | |
391172c4 AV |
2364 | if (!err) { |
2365 | *parent = nd->path; | |
2366 | nd->path.mnt = NULL; | |
2367 | nd->path.dentry = NULL; | |
2368 | } | |
2369 | terminate_walk(nd); | |
8bcb77fa AV |
2370 | return err; |
2371 | } | |
2372 | ||
5c31b6ce | 2373 | static struct filename *filename_parentat(int dfd, struct filename *name, |
391172c4 AV |
2374 | unsigned int flags, struct path *parent, |
2375 | struct qstr *last, int *type) | |
8bcb77fa AV |
2376 | { |
2377 | int retval; | |
9883d185 | 2378 | struct nameidata nd; |
8bcb77fa | 2379 | |
5c31b6ce AV |
2380 | if (IS_ERR(name)) |
2381 | return name; | |
9883d185 | 2382 | set_nameidata(&nd, dfd, name); |
c8a53ee5 | 2383 | retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); |
8bcb77fa | 2384 | if (unlikely(retval == -ECHILD)) |
c8a53ee5 | 2385 | retval = path_parentat(&nd, flags, parent); |
8bcb77fa | 2386 | if (unlikely(retval == -ESTALE)) |
c8a53ee5 | 2387 | retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); |
391172c4 AV |
2388 | if (likely(!retval)) { |
2389 | *last = nd.last; | |
2390 | *type = nd.last_type; | |
c9b07eab | 2391 | audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); |
5c31b6ce AV |
2392 | } else { |
2393 | putname(name); | |
2394 | name = ERR_PTR(retval); | |
391172c4 | 2395 | } |
9883d185 | 2396 | restore_nameidata(); |
5c31b6ce | 2397 | return name; |
8bcb77fa AV |
2398 | } |
2399 | ||
79714f72 AV |
2400 | /* does lookup, returns the object with parent locked */ |
2401 | struct dentry *kern_path_locked(const char *name, struct path *path) | |
5590ff0d | 2402 | { |
5c31b6ce AV |
2403 | struct filename *filename; |
2404 | struct dentry *d; | |
391172c4 AV |
2405 | struct qstr last; |
2406 | int type; | |
51689104 | 2407 | |
5c31b6ce AV |
2408 | filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path, |
2409 | &last, &type); | |
51689104 PM |
2410 | if (IS_ERR(filename)) |
2411 | return ERR_CAST(filename); | |
5c31b6ce | 2412 | if (unlikely(type != LAST_NORM)) { |
391172c4 | 2413 | path_put(path); |
5c31b6ce AV |
2414 | putname(filename); |
2415 | return ERR_PTR(-EINVAL); | |
79714f72 | 2416 | } |
5955102c | 2417 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
391172c4 | 2418 | d = __lookup_hash(&last, path->dentry, 0); |
79714f72 | 2419 | if (IS_ERR(d)) { |
5955102c | 2420 | inode_unlock(path->dentry->d_inode); |
391172c4 | 2421 | path_put(path); |
79714f72 | 2422 | } |
51689104 | 2423 | putname(filename); |
79714f72 | 2424 | return d; |
5590ff0d UD |
2425 | } |
2426 | ||
d1811465 AV |
2427 | int kern_path(const char *name, unsigned int flags, struct path *path) |
2428 | { | |
abc9f5be AV |
2429 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
2430 | flags, path, NULL); | |
d1811465 | 2431 | } |
4d359507 | 2432 | EXPORT_SYMBOL(kern_path); |
d1811465 | 2433 | |
16f18200 JJS |
2434 | /** |
2435 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair | |
2436 | * @dentry: pointer to dentry of the base directory | |
2437 | * @mnt: pointer to vfs mount of the base directory | |
2438 | * @name: pointer to file name | |
2439 | * @flags: lookup flags | |
e0a01249 | 2440 | * @path: pointer to struct path to fill |
16f18200 JJS |
2441 | */ |
2442 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, | |
2443 | const char *name, unsigned int flags, | |
e0a01249 | 2444 | struct path *path) |
16f18200 | 2445 | { |
9ad1aaa6 | 2446 | struct path root = {.mnt = mnt, .dentry = dentry}; |
9ad1aaa6 | 2447 | /* the first argument of filename_lookup() is ignored with root */ |
abc9f5be AV |
2448 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
2449 | flags , path, &root); | |
16f18200 | 2450 | } |
4d359507 | 2451 | EXPORT_SYMBOL(vfs_path_lookup); |
16f18200 | 2452 | |
3c95f0dc AV |
2453 | static int lookup_one_len_common(const char *name, struct dentry *base, |
2454 | int len, struct qstr *this) | |
057f6c01 | 2455 | { |
3c95f0dc AV |
2456 | this->name = name; |
2457 | this->len = len; | |
2458 | this->hash = full_name_hash(base, name, len); | |
6a96ba54 | 2459 | if (!len) |
3c95f0dc | 2460 | return -EACCES; |
6a96ba54 | 2461 | |
21d8a15a AV |
2462 | if (unlikely(name[0] == '.')) { |
2463 | if (len < 2 || (len == 2 && name[1] == '.')) | |
3c95f0dc | 2464 | return -EACCES; |
21d8a15a AV |
2465 | } |
2466 | ||
6a96ba54 | 2467 | while (len--) { |
3c95f0dc | 2468 | unsigned int c = *(const unsigned char *)name++; |
6a96ba54 | 2469 | if (c == '/' || c == '\0') |
3c95f0dc | 2470 | return -EACCES; |
6a96ba54 | 2471 | } |
5a202bcd AV |
2472 | /* |
2473 | * See if the low-level filesystem might want | |
2474 | * to use its own hash.. | |
2475 | */ | |
2476 | if (base->d_flags & DCACHE_OP_HASH) { | |
3c95f0dc | 2477 | int err = base->d_op->d_hash(base, this); |
5a202bcd | 2478 | if (err < 0) |
3c95f0dc | 2479 | return err; |
5a202bcd | 2480 | } |
eead1911 | 2481 | |
3c95f0dc AV |
2482 | return inode_permission(base->d_inode, MAY_EXEC); |
2483 | } | |
2484 | ||
0da0b7fd DH |
2485 | /** |
2486 | * try_lookup_one_len - filesystem helper to lookup single pathname component | |
2487 | * @name: pathname component to lookup | |
2488 | * @base: base directory to lookup from | |
2489 | * @len: maximum length @len should be interpreted to | |
2490 | * | |
2491 | * Look up a dentry by name in the dcache, returning NULL if it does not | |
2492 | * currently exist. The function does not try to create a dentry. | |
2493 | * | |
2494 | * Note that this routine is purely a helper for filesystem usage and should | |
2495 | * not be called by generic code. | |
2496 | * | |
2497 | * The caller must hold base->i_mutex. | |
2498 | */ | |
2499 | struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) | |
2500 | { | |
2501 | struct qstr this; | |
2502 | int err; | |
2503 | ||
2504 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); | |
2505 | ||
2506 | err = lookup_one_len_common(name, base, len, &this); | |
2507 | if (err) | |
2508 | return ERR_PTR(err); | |
2509 | ||
2510 | return lookup_dcache(&this, base, 0); | |
2511 | } | |
2512 | EXPORT_SYMBOL(try_lookup_one_len); | |
2513 | ||
3c95f0dc AV |
2514 | /** |
2515 | * lookup_one_len - filesystem helper to lookup single pathname component | |
2516 | * @name: pathname component to lookup | |
2517 | * @base: base directory to lookup from | |
2518 | * @len: maximum length @len should be interpreted to | |
2519 | * | |
2520 | * Note that this routine is purely a helper for filesystem usage and should | |
2521 | * not be called by generic code. | |
2522 | * | |
2523 | * The caller must hold base->i_mutex. | |
2524 | */ | |
2525 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) | |
2526 | { | |
8613a209 | 2527 | struct dentry *dentry; |
3c95f0dc AV |
2528 | struct qstr this; |
2529 | int err; | |
2530 | ||
2531 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); | |
2532 | ||
2533 | err = lookup_one_len_common(name, base, len, &this); | |
cda309de MS |
2534 | if (err) |
2535 | return ERR_PTR(err); | |
2536 | ||
8613a209 AV |
2537 | dentry = lookup_dcache(&this, base, 0); |
2538 | return dentry ? dentry : __lookup_slow(&this, base, 0); | |
057f6c01 | 2539 | } |
4d359507 | 2540 | EXPORT_SYMBOL(lookup_one_len); |
057f6c01 | 2541 | |
bbddca8e N |
2542 | /** |
2543 | * lookup_one_len_unlocked - filesystem helper to lookup single pathname component | |
2544 | * @name: pathname component to lookup | |
2545 | * @base: base directory to lookup from | |
2546 | * @len: maximum length @len should be interpreted to | |
2547 | * | |
2548 | * Note that this routine is purely a helper for filesystem usage and should | |
2549 | * not be called by generic code. | |
2550 | * | |
2551 | * Unlike lookup_one_len, it should be called without the parent | |
2552 | * i_mutex held, and will take the i_mutex itself if necessary. | |
2553 | */ | |
2554 | struct dentry *lookup_one_len_unlocked(const char *name, | |
2555 | struct dentry *base, int len) | |
2556 | { | |
2557 | struct qstr this; | |
bbddca8e | 2558 | int err; |
20d00ee8 | 2559 | struct dentry *ret; |
bbddca8e | 2560 | |
3c95f0dc | 2561 | err = lookup_one_len_common(name, base, len, &this); |
bbddca8e N |
2562 | if (err) |
2563 | return ERR_PTR(err); | |
2564 | ||
20d00ee8 LT |
2565 | ret = lookup_dcache(&this, base, 0); |
2566 | if (!ret) | |
2567 | ret = lookup_slow(&this, base, 0); | |
2568 | return ret; | |
bbddca8e N |
2569 | } |
2570 | EXPORT_SYMBOL(lookup_one_len_unlocked); | |
2571 | ||
6c2d4798 AV |
2572 | /* |
2573 | * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) | |
2574 | * on negatives. Returns known positive or ERR_PTR(); that's what | |
2575 | * most of the users want. Note that pinned negative with unlocked parent | |
2576 | * _can_ become positive at any time, so callers of lookup_one_len_unlocked() | |
2577 | * need to be very careful; pinned positives have ->d_inode stable, so | |
2578 | * this one avoids such problems. | |
2579 | */ | |
2580 | struct dentry *lookup_positive_unlocked(const char *name, | |
2581 | struct dentry *base, int len) | |
2582 | { | |
2583 | struct dentry *ret = lookup_one_len_unlocked(name, base, len); | |
2fa6b1e0 | 2584 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { |
6c2d4798 AV |
2585 | dput(ret); |
2586 | ret = ERR_PTR(-ENOENT); | |
2587 | } | |
2588 | return ret; | |
2589 | } | |
2590 | EXPORT_SYMBOL(lookup_positive_unlocked); | |
2591 | ||
eedf265a EB |
2592 | #ifdef CONFIG_UNIX98_PTYS |
2593 | int path_pts(struct path *path) | |
2594 | { | |
2595 | /* Find something mounted on "pts" in the same directory as | |
2596 | * the input path. | |
2597 | */ | |
a6a7eb76 AV |
2598 | struct dentry *parent = dget_parent(path->dentry); |
2599 | struct dentry *child; | |
eedf265a | 2600 | struct qstr this; |
eedf265a | 2601 | |
a6a7eb76 AV |
2602 | if (unlikely(!path_connected(path->mnt, parent))) { |
2603 | dput(parent); | |
63b27720 | 2604 | return -ENOENT; |
a6a7eb76 | 2605 | } |
63b27720 AV |
2606 | dput(path->dentry); |
2607 | path->dentry = parent; | |
eedf265a EB |
2608 | this.name = "pts"; |
2609 | this.len = 3; | |
2610 | child = d_hash_and_lookup(parent, &this); | |
2611 | if (!child) | |
2612 | return -ENOENT; | |
2613 | ||
2614 | path->dentry = child; | |
2615 | dput(parent); | |
2616 | follow_mount(path); | |
2617 | return 0; | |
2618 | } | |
2619 | #endif | |
2620 | ||
1fa1e7f6 AW |
2621 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
2622 | struct path *path, int *empty) | |
1da177e4 | 2623 | { |
abc9f5be AV |
2624 | return filename_lookup(dfd, getname_flags(name, flags, empty), |
2625 | flags, path, NULL); | |
1da177e4 | 2626 | } |
b853a161 | 2627 | EXPORT_SYMBOL(user_path_at_empty); |
1fa1e7f6 | 2628 | |
cbdf35bc | 2629 | int __check_sticky(struct inode *dir, struct inode *inode) |
1da177e4 | 2630 | { |
8e96e3b7 | 2631 | kuid_t fsuid = current_fsuid(); |
da9592ed | 2632 | |
8e96e3b7 | 2633 | if (uid_eq(inode->i_uid, fsuid)) |
1da177e4 | 2634 | return 0; |
8e96e3b7 | 2635 | if (uid_eq(dir->i_uid, fsuid)) |
1da177e4 | 2636 | return 0; |
23adbe12 | 2637 | return !capable_wrt_inode_uidgid(inode, CAP_FOWNER); |
1da177e4 | 2638 | } |
cbdf35bc | 2639 | EXPORT_SYMBOL(__check_sticky); |
1da177e4 LT |
2640 | |
2641 | /* | |
2642 | * Check whether we can remove a link victim from directory dir, check | |
2643 | * whether the type of victim is right. | |
2644 | * 1. We can't do it if dir is read-only (done in permission()) | |
2645 | * 2. We should have write and exec permissions on dir | |
2646 | * 3. We can't remove anything from append-only dir | |
2647 | * 4. We can't do anything with immutable dir (done in permission()) | |
2648 | * 5. If the sticky bit on dir is set we should either | |
2649 | * a. be owner of dir, or | |
2650 | * b. be owner of victim, or | |
2651 | * c. have CAP_FOWNER capability | |
2652 | * 6. If the victim is append-only or immutable we can't do antyhing with | |
2653 | * links pointing to it. | |
0bd23d09 EB |
2654 | * 7. If the victim has an unknown uid or gid we can't change the inode. |
2655 | * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
2656 | * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
2657 | * 10. We can't remove a root or mountpoint. | |
2658 | * 11. We don't allow removal of NFS sillyrenamed files; it's handled by | |
1da177e4 LT |
2659 | * nfs_async_unlink(). |
2660 | */ | |
b18825a7 | 2661 | static int may_delete(struct inode *dir, struct dentry *victim, bool isdir) |
1da177e4 | 2662 | { |
63afdfc7 | 2663 | struct inode *inode = d_backing_inode(victim); |
1da177e4 LT |
2664 | int error; |
2665 | ||
b18825a7 | 2666 | if (d_is_negative(victim)) |
1da177e4 | 2667 | return -ENOENT; |
b18825a7 | 2668 | BUG_ON(!inode); |
1da177e4 LT |
2669 | |
2670 | BUG_ON(victim->d_parent->d_inode != dir); | |
593d1ce8 EB |
2671 | |
2672 | /* Inode writeback is not safe when the uid or gid are invalid. */ | |
2673 | if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid)) | |
2674 | return -EOVERFLOW; | |
2675 | ||
4fa6b5ec | 2676 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
1da177e4 | 2677 | |
f419a2e3 | 2678 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
1da177e4 LT |
2679 | if (error) |
2680 | return error; | |
2681 | if (IS_APPEND(dir)) | |
2682 | return -EPERM; | |
b18825a7 DH |
2683 | |
2684 | if (check_sticky(dir, inode) || IS_APPEND(inode) || | |
0bd23d09 | 2685 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode)) |
1da177e4 LT |
2686 | return -EPERM; |
2687 | if (isdir) { | |
44b1d530 | 2688 | if (!d_is_dir(victim)) |
1da177e4 LT |
2689 | return -ENOTDIR; |
2690 | if (IS_ROOT(victim)) | |
2691 | return -EBUSY; | |
44b1d530 | 2692 | } else if (d_is_dir(victim)) |
1da177e4 LT |
2693 | return -EISDIR; |
2694 | if (IS_DEADDIR(dir)) | |
2695 | return -ENOENT; | |
2696 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
2697 | return -EBUSY; | |
2698 | return 0; | |
2699 | } | |
2700 | ||
2701 | /* Check whether we can create an object with dentry child in directory | |
2702 | * dir. | |
2703 | * 1. We can't do it if child already exists (open has special treatment for | |
2704 | * this case, but since we are inlined it's OK) | |
2705 | * 2. We can't do it if dir is read-only (done in permission()) | |
036d5236 EB |
2706 | * 3. We can't do it if the fs can't represent the fsuid or fsgid. |
2707 | * 4. We should have write and exec permissions on dir | |
2708 | * 5. We can't do it if dir is immutable (done in permission()) | |
1da177e4 | 2709 | */ |
a95164d9 | 2710 | static inline int may_create(struct inode *dir, struct dentry *child) |
1da177e4 | 2711 | { |
036d5236 | 2712 | struct user_namespace *s_user_ns; |
14e972b4 | 2713 | audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); |
1da177e4 LT |
2714 | if (child->d_inode) |
2715 | return -EEXIST; | |
2716 | if (IS_DEADDIR(dir)) | |
2717 | return -ENOENT; | |
036d5236 EB |
2718 | s_user_ns = dir->i_sb->s_user_ns; |
2719 | if (!kuid_has_mapping(s_user_ns, current_fsuid()) || | |
2720 | !kgid_has_mapping(s_user_ns, current_fsgid())) | |
2721 | return -EOVERFLOW; | |
f419a2e3 | 2722 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
1da177e4 LT |
2723 | } |
2724 | ||
1da177e4 LT |
2725 | /* |
2726 | * p1 and p2 should be directories on the same fs. | |
2727 | */ | |
2728 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) | |
2729 | { | |
2730 | struct dentry *p; | |
2731 | ||
2732 | if (p1 == p2) { | |
5955102c | 2733 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
1da177e4 LT |
2734 | return NULL; |
2735 | } | |
2736 | ||
fc64005c | 2737 | mutex_lock(&p1->d_sb->s_vfs_rename_mutex); |
1da177e4 | 2738 | |
e2761a11 OH |
2739 | p = d_ancestor(p2, p1); |
2740 | if (p) { | |
5955102c AV |
2741 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); |
2742 | inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); | |
e2761a11 | 2743 | return p; |
1da177e4 LT |
2744 | } |
2745 | ||
e2761a11 OH |
2746 | p = d_ancestor(p1, p2); |
2747 | if (p) { | |
5955102c AV |
2748 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
2749 | inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); | |
e2761a11 | 2750 | return p; |
1da177e4 LT |
2751 | } |
2752 | ||
5955102c AV |
2753 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
2754 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); | |
1da177e4 LT |
2755 | return NULL; |
2756 | } | |
4d359507 | 2757 | EXPORT_SYMBOL(lock_rename); |
1da177e4 LT |
2758 | |
2759 | void unlock_rename(struct dentry *p1, struct dentry *p2) | |
2760 | { | |
5955102c | 2761 | inode_unlock(p1->d_inode); |
1da177e4 | 2762 | if (p1 != p2) { |
5955102c | 2763 | inode_unlock(p2->d_inode); |
fc64005c | 2764 | mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); |
1da177e4 LT |
2765 | } |
2766 | } | |
4d359507 | 2767 | EXPORT_SYMBOL(unlock_rename); |
1da177e4 | 2768 | |
4acdaf27 | 2769 | int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
312b63fb | 2770 | bool want_excl) |
1da177e4 | 2771 | { |
a95164d9 | 2772 | int error = may_create(dir, dentry); |
1da177e4 LT |
2773 | if (error) |
2774 | return error; | |
2775 | ||
acfa4380 | 2776 | if (!dir->i_op->create) |
1da177e4 LT |
2777 | return -EACCES; /* shouldn't it be ENOSYS? */ |
2778 | mode &= S_IALLUGO; | |
2779 | mode |= S_IFREG; | |
2780 | error = security_inode_create(dir, dentry, mode); | |
2781 | if (error) | |
2782 | return error; | |
312b63fb | 2783 | error = dir->i_op->create(dir, dentry, mode, want_excl); |
a74574aa | 2784 | if (!error) |
f38aa942 | 2785 | fsnotify_create(dir, dentry); |
1da177e4 LT |
2786 | return error; |
2787 | } | |
4d359507 | 2788 | EXPORT_SYMBOL(vfs_create); |
1da177e4 | 2789 | |
8e6c848e AV |
2790 | int vfs_mkobj(struct dentry *dentry, umode_t mode, |
2791 | int (*f)(struct dentry *, umode_t, void *), | |
2792 | void *arg) | |
2793 | { | |
2794 | struct inode *dir = dentry->d_parent->d_inode; | |
2795 | int error = may_create(dir, dentry); | |
2796 | if (error) | |
2797 | return error; | |
2798 | ||
2799 | mode &= S_IALLUGO; | |
2800 | mode |= S_IFREG; | |
2801 | error = security_inode_create(dir, dentry, mode); | |
2802 | if (error) | |
2803 | return error; | |
2804 | error = f(dentry, mode, arg); | |
2805 | if (!error) | |
2806 | fsnotify_create(dir, dentry); | |
2807 | return error; | |
2808 | } | |
2809 | EXPORT_SYMBOL(vfs_mkobj); | |
2810 | ||
a2982cc9 EB |
2811 | bool may_open_dev(const struct path *path) |
2812 | { | |
2813 | return !(path->mnt->mnt_flags & MNT_NODEV) && | |
2814 | !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); | |
2815 | } | |
2816 | ||
f0bb5aaf | 2817 | static int may_open(const struct path *path, int acc_mode, int flag) |
1da177e4 | 2818 | { |
3fb64190 | 2819 | struct dentry *dentry = path->dentry; |
1da177e4 LT |
2820 | struct inode *inode = dentry->d_inode; |
2821 | int error; | |
2822 | ||
2823 | if (!inode) | |
2824 | return -ENOENT; | |
2825 | ||
c8fe8f30 CH |
2826 | switch (inode->i_mode & S_IFMT) { |
2827 | case S_IFLNK: | |
1da177e4 | 2828 | return -ELOOP; |
c8fe8f30 CH |
2829 | case S_IFDIR: |
2830 | if (acc_mode & MAY_WRITE) | |
2831 | return -EISDIR; | |
2832 | break; | |
2833 | case S_IFBLK: | |
2834 | case S_IFCHR: | |
a2982cc9 | 2835 | if (!may_open_dev(path)) |
1da177e4 | 2836 | return -EACCES; |
c8fe8f30 CH |
2837 | /*FALLTHRU*/ |
2838 | case S_IFIFO: | |
2839 | case S_IFSOCK: | |
1da177e4 | 2840 | flag &= ~O_TRUNC; |
c8fe8f30 | 2841 | break; |
4a3fd211 | 2842 | } |
b41572e9 | 2843 | |
62fb4a15 | 2844 | error = inode_permission(inode, MAY_OPEN | acc_mode); |
b41572e9 DH |
2845 | if (error) |
2846 | return error; | |
6146f0d5 | 2847 | |
1da177e4 LT |
2848 | /* |
2849 | * An append-only file must be opened in append mode for writing. | |
2850 | */ | |
2851 | if (IS_APPEND(inode)) { | |
8737c930 | 2852 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
7715b521 | 2853 | return -EPERM; |
1da177e4 | 2854 | if (flag & O_TRUNC) |
7715b521 | 2855 | return -EPERM; |
1da177e4 LT |
2856 | } |
2857 | ||
2858 | /* O_NOATIME can only be set by the owner or superuser */ | |
2e149670 | 2859 | if (flag & O_NOATIME && !inode_owner_or_capable(inode)) |
7715b521 | 2860 | return -EPERM; |
1da177e4 | 2861 | |
f3c7691e | 2862 | return 0; |
7715b521 | 2863 | } |
1da177e4 | 2864 | |
e1181ee6 | 2865 | static int handle_truncate(struct file *filp) |
7715b521 | 2866 | { |
f0bb5aaf | 2867 | const struct path *path = &filp->f_path; |
7715b521 AV |
2868 | struct inode *inode = path->dentry->d_inode; |
2869 | int error = get_write_access(inode); | |
2870 | if (error) | |
2871 | return error; | |
2872 | /* | |
2873 | * Refuse to truncate files with mandatory locks held on them. | |
2874 | */ | |
d7a06983 | 2875 | error = locks_verify_locked(filp); |
7715b521 | 2876 | if (!error) |
ea0d3ab2 | 2877 | error = security_path_truncate(path); |
7715b521 AV |
2878 | if (!error) { |
2879 | error = do_truncate(path->dentry, 0, | |
2880 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, | |
e1181ee6 | 2881 | filp); |
7715b521 AV |
2882 | } |
2883 | put_write_access(inode); | |
acd0c935 | 2884 | return error; |
1da177e4 LT |
2885 | } |
2886 | ||
d57999e1 DH |
2887 | static inline int open_to_namei_flags(int flag) |
2888 | { | |
8a5e929d AV |
2889 | if ((flag & O_ACCMODE) == 3) |
2890 | flag--; | |
d57999e1 DH |
2891 | return flag; |
2892 | } | |
2893 | ||
d3607752 | 2894 | static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode) |
d18e9008 | 2895 | { |
1328c727 | 2896 | struct user_namespace *s_user_ns; |
d18e9008 MS |
2897 | int error = security_path_mknod(dir, dentry, mode, 0); |
2898 | if (error) | |
2899 | return error; | |
2900 | ||
1328c727 SF |
2901 | s_user_ns = dir->dentry->d_sb->s_user_ns; |
2902 | if (!kuid_has_mapping(s_user_ns, current_fsuid()) || | |
2903 | !kgid_has_mapping(s_user_ns, current_fsgid())) | |
2904 | return -EOVERFLOW; | |
2905 | ||
d18e9008 MS |
2906 | error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); |
2907 | if (error) | |
2908 | return error; | |
2909 | ||
2910 | return security_inode_create(dir->dentry->d_inode, dentry, mode); | |
2911 | } | |
2912 | ||
1acf0af9 DH |
2913 | /* |
2914 | * Attempt to atomically look up, create and open a file from a negative | |
2915 | * dentry. | |
2916 | * | |
2917 | * Returns 0 if successful. The file will have been created and attached to | |
2918 | * @file by the filesystem calling finish_open(). | |
2919 | * | |
00a07c15 AV |
2920 | * If the file was looked up only or didn't need creating, FMODE_OPENED won't |
2921 | * be set. The caller will need to perform the open themselves. @path will | |
2922 | * have been updated to point to the new dentry. This may be negative. | |
1acf0af9 DH |
2923 | * |
2924 | * Returns an error code otherwise. | |
2925 | */ | |
239eb983 AV |
2926 | static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, |
2927 | struct file *file, | |
2928 | const struct open_flags *op, | |
2929 | int open_flag, umode_t mode) | |
d18e9008 | 2930 | { |
384f26e2 | 2931 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
d18e9008 | 2932 | struct inode *dir = nd->path.dentry->d_inode; |
d18e9008 | 2933 | int error; |
d18e9008 | 2934 | |
384f26e2 | 2935 | if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */ |
d18e9008 | 2936 | open_flag &= ~O_TRUNC; |
d18e9008 | 2937 | |
d18e9008 MS |
2938 | if (nd->flags & LOOKUP_DIRECTORY) |
2939 | open_flag |= O_DIRECTORY; | |
2940 | ||
30d90494 AV |
2941 | file->f_path.dentry = DENTRY_NOT_SET; |
2942 | file->f_path.mnt = nd->path.mnt; | |
0fb1ea09 | 2943 | error = dir->i_op->atomic_open(dir, dentry, file, |
44907d79 | 2944 | open_to_namei_flags(open_flag), mode); |
6fbd0714 | 2945 | d_lookup_done(dentry); |
384f26e2 | 2946 | if (!error) { |
64e1ac4d | 2947 | if (file->f_mode & FMODE_OPENED) { |
6fb968cd AV |
2948 | if (unlikely(dentry != file->f_path.dentry)) { |
2949 | dput(dentry); | |
2950 | dentry = dget(file->f_path.dentry); | |
2951 | } | |
7be219b4 | 2952 | if (file->f_mode & FMODE_CREATED) |
64e1ac4d | 2953 | fsnotify_create(dir, dentry); |
64e1ac4d | 2954 | } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
2675a4eb | 2955 | error = -EIO; |
03da633a | 2956 | } else { |
384f26e2 AV |
2957 | if (file->f_path.dentry) { |
2958 | dput(dentry); | |
2959 | dentry = file->f_path.dentry; | |
03da633a | 2960 | } |
73a09dd9 | 2961 | if (file->f_mode & FMODE_CREATED) |
384f26e2 | 2962 | fsnotify_create(dir, dentry); |
239eb983 | 2963 | if (unlikely(d_is_negative(dentry))) |
a01e718f | 2964 | error = -ENOENT; |
62b2ce96 | 2965 | } |
d18e9008 | 2966 | } |
239eb983 AV |
2967 | if (error) { |
2968 | dput(dentry); | |
2969 | dentry = ERR_PTR(error); | |
2970 | } | |
2971 | return dentry; | |
d18e9008 MS |
2972 | } |
2973 | ||
d58ffd35 | 2974 | /* |
1acf0af9 | 2975 | * Look up and maybe create and open the last component. |
d58ffd35 | 2976 | * |
00a07c15 | 2977 | * Must be called with parent locked (exclusive in O_CREAT case). |
1acf0af9 | 2978 | * |
00a07c15 AV |
2979 | * Returns 0 on success, that is, if |
2980 | * the file was successfully atomically created (if necessary) and opened, or | |
2981 | * the file was not completely opened at this time, though lookups and | |
2982 | * creations were performed. | |
2983 | * These case are distinguished by presence of FMODE_OPENED on file->f_mode. | |
2984 | * In the latter case dentry returned in @path might be negative if O_CREAT | |
2985 | * hadn't been specified. | |
1acf0af9 | 2986 | * |
00a07c15 | 2987 | * An error code is returned on failure. |
d58ffd35 | 2988 | */ |
da5ebf5a AV |
2989 | static struct dentry *lookup_open(struct nameidata *nd, struct file *file, |
2990 | const struct open_flags *op, | |
2991 | bool got_write) | |
d58ffd35 MS |
2992 | { |
2993 | struct dentry *dir = nd->path.dentry; | |
54ef4872 | 2994 | struct inode *dir_inode = dir->d_inode; |
1643b43f | 2995 | int open_flag = op->open_flag; |
d58ffd35 | 2996 | struct dentry *dentry; |
1643b43f | 2997 | int error, create_error = 0; |
1643b43f | 2998 | umode_t mode = op->mode; |
6fbd0714 | 2999 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
d58ffd35 | 3000 | |
ce8644fc | 3001 | if (unlikely(IS_DEADDIR(dir_inode))) |
da5ebf5a | 3002 | return ERR_PTR(-ENOENT); |
d58ffd35 | 3003 | |
73a09dd9 | 3004 | file->f_mode &= ~FMODE_CREATED; |
6fbd0714 AV |
3005 | dentry = d_lookup(dir, &nd->last); |
3006 | for (;;) { | |
3007 | if (!dentry) { | |
3008 | dentry = d_alloc_parallel(dir, &nd->last, &wq); | |
3009 | if (IS_ERR(dentry)) | |
da5ebf5a | 3010 | return dentry; |
6fbd0714 AV |
3011 | } |
3012 | if (d_in_lookup(dentry)) | |
3013 | break; | |
d58ffd35 | 3014 | |
6fbd0714 AV |
3015 | error = d_revalidate(dentry, nd->flags); |
3016 | if (likely(error > 0)) | |
3017 | break; | |
3018 | if (error) | |
3019 | goto out_dput; | |
3020 | d_invalidate(dentry); | |
3021 | dput(dentry); | |
3022 | dentry = NULL; | |
3023 | } | |
3024 | if (dentry->d_inode) { | |
6c51e513 | 3025 | /* Cached positive dentry: will open in f_op->open */ |
da5ebf5a | 3026 | return dentry; |
6c51e513 | 3027 | } |
d18e9008 | 3028 | |
1643b43f AV |
3029 | /* |
3030 | * Checking write permission is tricky, bacuse we don't know if we are | |
3031 | * going to actually need it: O_CREAT opens should work as long as the | |
3032 | * file exists. But checking existence breaks atomicity. The trick is | |
3033 | * to check access and if not granted clear O_CREAT from the flags. | |
3034 | * | |
3035 | * Another problem is returing the "right" error value (e.g. for an | |
3036 | * O_EXCL open we want to return EEXIST not EROFS). | |
3037 | */ | |
3038 | if (open_flag & O_CREAT) { | |
3039 | if (!IS_POSIXACL(dir->d_inode)) | |
3040 | mode &= ~current_umask(); | |
3041 | if (unlikely(!got_write)) { | |
3042 | create_error = -EROFS; | |
3043 | open_flag &= ~O_CREAT; | |
3044 | if (open_flag & (O_EXCL | O_TRUNC)) | |
3045 | goto no_open; | |
3046 | /* No side effects, safe to clear O_CREAT */ | |
3047 | } else { | |
3048 | create_error = may_o_create(&nd->path, dentry, mode); | |
3049 | if (create_error) { | |
3050 | open_flag &= ~O_CREAT; | |
3051 | if (open_flag & O_EXCL) | |
3052 | goto no_open; | |
3053 | } | |
3054 | } | |
3055 | } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) && | |
3056 | unlikely(!got_write)) { | |
3057 | /* | |
3058 | * No O_CREATE -> atomicity not a requirement -> fall | |
3059 | * back to lookup + open | |
3060 | */ | |
3061 | goto no_open; | |
d18e9008 MS |
3062 | } |
3063 | ||
6ac08709 | 3064 | if (dir_inode->i_op->atomic_open) { |
239eb983 | 3065 | dentry = atomic_open(nd, dentry, file, op, open_flag, mode); |
da5ebf5a AV |
3066 | if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) |
3067 | dentry = ERR_PTR(create_error); | |
3068 | return dentry; | |
d18e9008 | 3069 | } |
54ef4872 | 3070 | |
1643b43f | 3071 | no_open: |
6fbd0714 | 3072 | if (d_in_lookup(dentry)) { |
12fa5e24 AV |
3073 | struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, |
3074 | nd->flags); | |
6fbd0714 | 3075 | d_lookup_done(dentry); |
12fa5e24 AV |
3076 | if (unlikely(res)) { |
3077 | if (IS_ERR(res)) { | |
3078 | error = PTR_ERR(res); | |
3079 | goto out_dput; | |
3080 | } | |
3081 | dput(dentry); | |
3082 | dentry = res; | |
3083 | } | |
54ef4872 MS |
3084 | } |
3085 | ||
d58ffd35 | 3086 | /* Negative dentry, just create the file */ |
1643b43f | 3087 | if (!dentry->d_inode && (open_flag & O_CREAT)) { |
73a09dd9 | 3088 | file->f_mode |= FMODE_CREATED; |
ce8644fc | 3089 | audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
ce8644fc AV |
3090 | if (!dir_inode->i_op->create) { |
3091 | error = -EACCES; | |
d58ffd35 | 3092 | goto out_dput; |
ce8644fc AV |
3093 | } |
3094 | error = dir_inode->i_op->create(dir_inode, dentry, mode, | |
1643b43f | 3095 | open_flag & O_EXCL); |
d58ffd35 MS |
3096 | if (error) |
3097 | goto out_dput; | |
ce8644fc | 3098 | fsnotify_create(dir_inode, dentry); |
d58ffd35 | 3099 | } |
1643b43f AV |
3100 | if (unlikely(create_error) && !dentry->d_inode) { |
3101 | error = create_error; | |
3102 | goto out_dput; | |
d58ffd35 | 3103 | } |
da5ebf5a | 3104 | return dentry; |
d58ffd35 MS |
3105 | |
3106 | out_dput: | |
3107 | dput(dentry); | |
da5ebf5a | 3108 | return ERR_PTR(error); |
d58ffd35 MS |
3109 | } |
3110 | ||
c981a482 | 3111 | static const char *open_last_lookups(struct nameidata *nd, |
3ec2eef1 | 3112 | struct file *file, const struct open_flags *op) |
fb1cc555 | 3113 | { |
a1e28038 | 3114 | struct dentry *dir = nd->path.dentry; |
ca344a89 | 3115 | int open_flag = op->open_flag; |
64894cf8 | 3116 | bool got_write = false; |
254cf582 | 3117 | unsigned seq; |
a1eb3315 | 3118 | struct inode *inode; |
da5ebf5a | 3119 | struct dentry *dentry; |
b0417d2c | 3120 | const char *res; |
16c2cd71 | 3121 | int error; |
1f36f774 | 3122 | |
c3e380b0 AV |
3123 | nd->flags |= op->intent; |
3124 | ||
bc77daa7 | 3125 | if (nd->last_type != LAST_NORM) { |
56676ec3 AV |
3126 | if (nd->depth) |
3127 | put_link(nd); | |
7521f22b AV |
3128 | res = handle_dots(nd, nd->last_type); |
3129 | if (likely(!res)) | |
3130 | res = ERR_PTR(complete_walk(nd)); | |
3131 | return res; | |
1f36f774 | 3132 | } |
67ee3ad2 | 3133 | |
ca344a89 | 3134 | if (!(open_flag & O_CREAT)) { |
fe2d35ff AV |
3135 | if (nd->last.name[nd->last.len]) |
3136 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
3137 | /* we _can_ be in RCU mode here */ | |
20e34357 AV |
3138 | dentry = lookup_fast(nd, &inode, &seq); |
3139 | if (IS_ERR(dentry)) | |
1ccac622 | 3140 | return ERR_CAST(dentry); |
20e34357 | 3141 | if (likely(dentry)) |
71574865 MS |
3142 | goto finish_lookup; |
3143 | ||
6583fe22 | 3144 | BUG_ON(nd->flags & LOOKUP_RCU); |
b6183df7 MS |
3145 | } else { |
3146 | /* create side of things */ | |
3147 | /* | |
3148 | * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED | |
3149 | * has been cleared when we got to the last component we are | |
3150 | * about to look up | |
3151 | */ | |
3152 | error = complete_walk(nd); | |
c981a482 | 3153 | if (unlikely(error)) |
1ccac622 | 3154 | return ERR_PTR(error); |
fe2d35ff | 3155 | |
c9b07eab | 3156 | audit_inode(nd->name, dir, AUDIT_INODE_PARENT); |
b6183df7 | 3157 | /* trailing slashes? */ |
deb106c6 | 3158 | if (unlikely(nd->last.name[nd->last.len])) |
1ccac622 | 3159 | return ERR_PTR(-EISDIR); |
b6183df7 | 3160 | } |
a2c36b45 | 3161 | |
9cf843e3 | 3162 | if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
64894cf8 AV |
3163 | error = mnt_want_write(nd->path.mnt); |
3164 | if (!error) | |
3165 | got_write = true; | |
3166 | /* | |
3167 | * do _not_ fail yet - we might not need that or fail with | |
3168 | * a different error; let lookup_open() decide; we'll be | |
3169 | * dropping this one anyway. | |
3170 | */ | |
3171 | } | |
9cf843e3 AV |
3172 | if (open_flag & O_CREAT) |
3173 | inode_lock(dir->d_inode); | |
3174 | else | |
3175 | inode_lock_shared(dir->d_inode); | |
da5ebf5a | 3176 | dentry = lookup_open(nd, file, op, got_write); |
9cf843e3 AV |
3177 | if (open_flag & O_CREAT) |
3178 | inode_unlock(dir->d_inode); | |
3179 | else | |
3180 | inode_unlock_shared(dir->d_inode); | |
a1e28038 | 3181 | |
c981a482 | 3182 | if (got_write) |
59e96e65 | 3183 | mnt_drop_write(nd->path.mnt); |
d18e9008 | 3184 | |
59e96e65 AV |
3185 | if (IS_ERR(dentry)) |
3186 | return ERR_CAST(dentry); | |
3187 | ||
973d4b73 | 3188 | if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { |
e73cabff AV |
3189 | dput(nd->path.dentry); |
3190 | nd->path.dentry = dentry; | |
c981a482 | 3191 | return NULL; |
fb1cc555 AV |
3192 | } |
3193 | ||
20e34357 | 3194 | finish_lookup: |
56676ec3 AV |
3195 | if (nd->depth) |
3196 | put_link(nd); | |
8c4efe22 | 3197 | res = step_into(nd, WALK_TRAILING, dentry, inode, seq); |
b0417d2c | 3198 | if (unlikely(res)) { |
b0417d2c | 3199 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
b0417d2c | 3200 | return res; |
1ccac622 | 3201 | } |
31d1726d AV |
3202 | |
3203 | if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) { | |
3204 | audit_inode(nd->name, nd->path.dentry, 0); | |
1ccac622 | 3205 | return ERR_PTR(-EEXIST); |
31d1726d | 3206 | } |
c981a482 | 3207 | |
8f64fb1c | 3208 | /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ |
c981a482 AV |
3209 | return ERR_PTR(complete_walk(nd)); |
3210 | } | |
3211 | ||
3212 | /* | |
3213 | * Handle the last step of open() | |
3214 | */ | |
3215 | static const char *do_last(struct nameidata *nd, | |
3216 | struct file *file, const struct open_flags *op) | |
3217 | { | |
3218 | kuid_t dir_uid = nd->inode->i_uid; | |
3219 | umode_t dir_mode = nd->inode->i_mode; | |
3220 | int open_flag = op->open_flag; | |
3221 | bool do_truncate; | |
3222 | int acc_mode; | |
3223 | const char *link; | |
3224 | int error; | |
3225 | ||
3226 | link = open_last_lookups(nd, file, op); | |
3227 | if (unlikely(link)) | |
3228 | return link; | |
3229 | ||
973d4b73 AV |
3230 | if (!(file->f_mode & FMODE_CREATED)) |
3231 | audit_inode(nd->name, nd->path.dentry, 0); | |
30aba665 | 3232 | if (open_flag & O_CREAT) { |
30aba665 | 3233 | if (d_is_dir(nd->path.dentry)) |
59e96e65 | 3234 | return ERR_PTR(-EISDIR); |
d0cb5018 | 3235 | error = may_create_in_sticky(dir_mode, dir_uid, |
30aba665 SM |
3236 | d_backing_inode(nd->path.dentry)); |
3237 | if (unlikely(error)) | |
59e96e65 | 3238 | return ERR_PTR(error); |
30aba665 | 3239 | } |
44b1d530 | 3240 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) |
59e96e65 | 3241 | return ERR_PTR(-ENOTDIR); |
6c0d46c4 | 3242 | |
8795e7d4 AV |
3243 | do_truncate = false; |
3244 | acc_mode = op->acc_mode; | |
5a2d3edd AV |
3245 | if (file->f_mode & FMODE_CREATED) { |
3246 | /* Don't check for write permission, don't truncate */ | |
3247 | open_flag &= ~O_TRUNC; | |
5a2d3edd | 3248 | acc_mode = 0; |
8795e7d4 | 3249 | } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { |
0f9d1a10 AV |
3250 | error = mnt_want_write(nd->path.mnt); |
3251 | if (error) | |
59e96e65 | 3252 | return ERR_PTR(error); |
8795e7d4 | 3253 | do_truncate = true; |
0f9d1a10 | 3254 | } |
6ac08709 | 3255 | error = may_open(&nd->path, acc_mode, open_flag); |
8795e7d4 | 3256 | if (!error && !(file->f_mode & FMODE_OPENED)) |
3ad5615a | 3257 | error = vfs_open(&nd->path, file); |
8795e7d4 AV |
3258 | if (!error) |
3259 | error = ima_file_check(file, op->acc_mode); | |
3260 | if (!error && do_truncate) | |
2675a4eb | 3261 | error = handle_truncate(file); |
c80567c8 AV |
3262 | if (unlikely(error > 0)) { |
3263 | WARN_ON(1); | |
3264 | error = -EINVAL; | |
3265 | } | |
8795e7d4 | 3266 | if (do_truncate) |
0f9d1a10 | 3267 | mnt_drop_write(nd->path.mnt); |
1ccac622 | 3268 | return ERR_PTR(error); |
fb1cc555 AV |
3269 | } |
3270 | ||
af7bd4dc AG |
3271 | struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag) |
3272 | { | |
af7bd4dc AG |
3273 | struct dentry *child = NULL; |
3274 | struct inode *dir = dentry->d_inode; | |
3275 | struct inode *inode; | |
3276 | int error; | |
3277 | ||
3278 | /* we want directory to be writable */ | |
3279 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
3280 | if (error) | |
3281 | goto out_err; | |
3282 | error = -EOPNOTSUPP; | |
3283 | if (!dir->i_op->tmpfile) | |
3284 | goto out_err; | |
3285 | error = -ENOMEM; | |
cdf01226 | 3286 | child = d_alloc(dentry, &slash_name); |
af7bd4dc AG |
3287 | if (unlikely(!child)) |
3288 | goto out_err; | |
3289 | error = dir->i_op->tmpfile(dir, child, mode); | |
3290 | if (error) | |
3291 | goto out_err; | |
3292 | error = -ENOENT; | |
3293 | inode = child->d_inode; | |
3294 | if (unlikely(!inode)) | |
3295 | goto out_err; | |
3296 | if (!(open_flag & O_EXCL)) { | |
3297 | spin_lock(&inode->i_lock); | |
3298 | inode->i_state |= I_LINKABLE; | |
3299 | spin_unlock(&inode->i_lock); | |
3300 | } | |
fdb2410f | 3301 | ima_post_create_tmpfile(inode); |
af7bd4dc AG |
3302 | return child; |
3303 | ||
3304 | out_err: | |
3305 | dput(child); | |
3306 | return ERR_PTR(error); | |
3307 | } | |
3308 | EXPORT_SYMBOL(vfs_tmpfile); | |
3309 | ||
c8a53ee5 | 3310 | static int do_tmpfile(struct nameidata *nd, unsigned flags, |
60545d0d | 3311 | const struct open_flags *op, |
3ec2eef1 | 3312 | struct file *file) |
60545d0d | 3313 | { |
625b6d10 | 3314 | struct dentry *child; |
625b6d10 | 3315 | struct path path; |
c8a53ee5 | 3316 | int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); |
60545d0d AV |
3317 | if (unlikely(error)) |
3318 | return error; | |
625b6d10 | 3319 | error = mnt_want_write(path.mnt); |
60545d0d AV |
3320 | if (unlikely(error)) |
3321 | goto out; | |
af7bd4dc AG |
3322 | child = vfs_tmpfile(path.dentry, op->mode, op->open_flag); |
3323 | error = PTR_ERR(child); | |
684e73be | 3324 | if (IS_ERR(child)) |
60545d0d | 3325 | goto out2; |
625b6d10 AV |
3326 | dput(path.dentry); |
3327 | path.dentry = child; | |
c8a53ee5 | 3328 | audit_inode(nd->name, child, 0); |
69a91c23 | 3329 | /* Don't check for other permissions, the inode was just created */ |
62fb4a15 | 3330 | error = may_open(&path, 0, op->open_flag); |
60545d0d AV |
3331 | if (error) |
3332 | goto out2; | |
625b6d10 | 3333 | file->f_path.mnt = path.mnt; |
be12af3e | 3334 | error = finish_open(file, child, NULL); |
60545d0d | 3335 | out2: |
625b6d10 | 3336 | mnt_drop_write(path.mnt); |
60545d0d | 3337 | out: |
625b6d10 | 3338 | path_put(&path); |
60545d0d AV |
3339 | return error; |
3340 | } | |
3341 | ||
6ac08709 AV |
3342 | static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) |
3343 | { | |
3344 | struct path path; | |
3345 | int error = path_lookupat(nd, flags, &path); | |
3346 | if (!error) { | |
3347 | audit_inode(nd->name, path.dentry, 0); | |
ae2bb293 | 3348 | error = vfs_open(&path, file); |
6ac08709 AV |
3349 | path_put(&path); |
3350 | } | |
3351 | return error; | |
3352 | } | |
3353 | ||
c8a53ee5 AV |
3354 | static struct file *path_openat(struct nameidata *nd, |
3355 | const struct open_flags *op, unsigned flags) | |
1da177e4 | 3356 | { |
30d90494 | 3357 | struct file *file; |
13aab428 | 3358 | int error; |
31e6b01f | 3359 | |
ea73ea72 | 3360 | file = alloc_empty_file(op->open_flag, current_cred()); |
1afc99be AV |
3361 | if (IS_ERR(file)) |
3362 | return file; | |
31e6b01f | 3363 | |
bb458c64 | 3364 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
3ec2eef1 | 3365 | error = do_tmpfile(nd, flags, op, file); |
5f336e72 | 3366 | } else if (unlikely(file->f_flags & O_PATH)) { |
6ac08709 | 3367 | error = do_o_path(nd, flags, file); |
5f336e72 AV |
3368 | } else { |
3369 | const char *s = path_init(nd, flags); | |
3370 | while (!(error = link_path_walk(s, nd)) && | |
1ccac622 AV |
3371 | (s = do_last(nd, file, op)) != NULL) |
3372 | ; | |
5f336e72 | 3373 | terminate_walk(nd); |
806b681c | 3374 | } |
7c1c01ec | 3375 | if (likely(!error)) { |
aad888f8 | 3376 | if (likely(file->f_mode & FMODE_OPENED)) |
7c1c01ec AV |
3377 | return file; |
3378 | WARN_ON(1); | |
3379 | error = -EINVAL; | |
16b1c1cd | 3380 | } |
7c1c01ec AV |
3381 | fput(file); |
3382 | if (error == -EOPENSTALE) { | |
3383 | if (flags & LOOKUP_RCU) | |
3384 | error = -ECHILD; | |
3385 | else | |
3386 | error = -ESTALE; | |
2675a4eb | 3387 | } |
7c1c01ec | 3388 | return ERR_PTR(error); |
1da177e4 LT |
3389 | } |
3390 | ||
669abf4e | 3391 | struct file *do_filp_open(int dfd, struct filename *pathname, |
f9652e10 | 3392 | const struct open_flags *op) |
13aab428 | 3393 | { |
9883d185 | 3394 | struct nameidata nd; |
f9652e10 | 3395 | int flags = op->lookup_flags; |
13aab428 AV |
3396 | struct file *filp; |
3397 | ||
9883d185 | 3398 | set_nameidata(&nd, dfd, pathname); |
c8a53ee5 | 3399 | filp = path_openat(&nd, op, flags | LOOKUP_RCU); |
13aab428 | 3400 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
c8a53ee5 | 3401 | filp = path_openat(&nd, op, flags); |
13aab428 | 3402 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
c8a53ee5 | 3403 | filp = path_openat(&nd, op, flags | LOOKUP_REVAL); |
9883d185 | 3404 | restore_nameidata(); |
13aab428 AV |
3405 | return filp; |
3406 | } | |
3407 | ||
73d049a4 | 3408 | struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
f9652e10 | 3409 | const char *name, const struct open_flags *op) |
73d049a4 | 3410 | { |
9883d185 | 3411 | struct nameidata nd; |
73d049a4 | 3412 | struct file *file; |
51689104 | 3413 | struct filename *filename; |
f9652e10 | 3414 | int flags = op->lookup_flags | LOOKUP_ROOT; |
73d049a4 AV |
3415 | |
3416 | nd.root.mnt = mnt; | |
3417 | nd.root.dentry = dentry; | |
3418 | ||
b18825a7 | 3419 | if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN) |
73d049a4 AV |
3420 | return ERR_PTR(-ELOOP); |
3421 | ||
51689104 | 3422 | filename = getname_kernel(name); |
a1c83681 | 3423 | if (IS_ERR(filename)) |
51689104 PM |
3424 | return ERR_CAST(filename); |
3425 | ||
9883d185 | 3426 | set_nameidata(&nd, -1, filename); |
c8a53ee5 | 3427 | file = path_openat(&nd, op, flags | LOOKUP_RCU); |
73d049a4 | 3428 | if (unlikely(file == ERR_PTR(-ECHILD))) |
c8a53ee5 | 3429 | file = path_openat(&nd, op, flags); |
73d049a4 | 3430 | if (unlikely(file == ERR_PTR(-ESTALE))) |
c8a53ee5 | 3431 | file = path_openat(&nd, op, flags | LOOKUP_REVAL); |
9883d185 | 3432 | restore_nameidata(); |
51689104 | 3433 | putname(filename); |
73d049a4 AV |
3434 | return file; |
3435 | } | |
3436 | ||
fa14a0b8 | 3437 | static struct dentry *filename_create(int dfd, struct filename *name, |
1ac12b4b | 3438 | struct path *path, unsigned int lookup_flags) |
1da177e4 | 3439 | { |
c663e5d8 | 3440 | struct dentry *dentry = ERR_PTR(-EEXIST); |
391172c4 AV |
3441 | struct qstr last; |
3442 | int type; | |
c30dabfe | 3443 | int err2; |
1ac12b4b JL |
3444 | int error; |
3445 | bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); | |
3446 | ||
3447 | /* | |
3448 | * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any | |
3449 | * other flags passed in are ignored! | |
3450 | */ | |
3451 | lookup_flags &= LOOKUP_REVAL; | |
3452 | ||
5c31b6ce AV |
3453 | name = filename_parentat(dfd, name, lookup_flags, path, &last, &type); |
3454 | if (IS_ERR(name)) | |
3455 | return ERR_CAST(name); | |
1da177e4 | 3456 | |
c663e5d8 CH |
3457 | /* |
3458 | * Yucky last component or no last component at all? | |
3459 | * (foo/., foo/.., /////) | |
3460 | */ | |
5c31b6ce | 3461 | if (unlikely(type != LAST_NORM)) |
ed75e95d | 3462 | goto out; |
c663e5d8 | 3463 | |
c30dabfe | 3464 | /* don't fail immediately if it's r/o, at least try to report other errors */ |
391172c4 | 3465 | err2 = mnt_want_write(path->mnt); |
c663e5d8 CH |
3466 | /* |
3467 | * Do the final lookup. | |
3468 | */ | |
391172c4 | 3469 | lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL; |
5955102c | 3470 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
391172c4 | 3471 | dentry = __lookup_hash(&last, path->dentry, lookup_flags); |
1da177e4 | 3472 | if (IS_ERR(dentry)) |
a8104a9f | 3473 | goto unlock; |
c663e5d8 | 3474 | |
a8104a9f | 3475 | error = -EEXIST; |
b18825a7 | 3476 | if (d_is_positive(dentry)) |
a8104a9f | 3477 | goto fail; |
b18825a7 | 3478 | |
c663e5d8 CH |
3479 | /* |
3480 | * Special case - lookup gave negative, but... we had foo/bar/ | |
3481 | * From the vfs_mknod() POV we just have a negative dentry - | |
3482 | * all is fine. Let's be bastards - you had / on the end, you've | |
3483 | * been asking for (non-existent) directory. -ENOENT for you. | |
3484 | */ | |
391172c4 | 3485 | if (unlikely(!is_dir && last.name[last.len])) { |
a8104a9f | 3486 | error = -ENOENT; |
ed75e95d | 3487 | goto fail; |
e9baf6e5 | 3488 | } |
c30dabfe JK |
3489 | if (unlikely(err2)) { |
3490 | error = err2; | |
a8104a9f | 3491 | goto fail; |
c30dabfe | 3492 | } |
181c37b6 | 3493 | putname(name); |
1da177e4 | 3494 | return dentry; |
1da177e4 | 3495 | fail: |
a8104a9f AV |
3496 | dput(dentry); |
3497 | dentry = ERR_PTR(error); | |
3498 | unlock: | |
5955102c | 3499 | inode_unlock(path->dentry->d_inode); |
c30dabfe | 3500 | if (!err2) |
391172c4 | 3501 | mnt_drop_write(path->mnt); |
ed75e95d | 3502 | out: |
391172c4 | 3503 | path_put(path); |
181c37b6 | 3504 | putname(name); |
1da177e4 LT |
3505 | return dentry; |
3506 | } | |
fa14a0b8 AV |
3507 | |
3508 | struct dentry *kern_path_create(int dfd, const char *pathname, | |
3509 | struct path *path, unsigned int lookup_flags) | |
3510 | { | |
181c37b6 AV |
3511 | return filename_create(dfd, getname_kernel(pathname), |
3512 | path, lookup_flags); | |
fa14a0b8 | 3513 | } |
dae6ad8f AV |
3514 | EXPORT_SYMBOL(kern_path_create); |
3515 | ||
921a1650 AV |
3516 | void done_path_create(struct path *path, struct dentry *dentry) |
3517 | { | |
3518 | dput(dentry); | |
5955102c | 3519 | inode_unlock(path->dentry->d_inode); |
a8104a9f | 3520 | mnt_drop_write(path->mnt); |
921a1650 AV |
3521 | path_put(path); |
3522 | } | |
3523 | EXPORT_SYMBOL(done_path_create); | |
3524 | ||
520ae687 | 3525 | inline struct dentry *user_path_create(int dfd, const char __user *pathname, |
1ac12b4b | 3526 | struct path *path, unsigned int lookup_flags) |
dae6ad8f | 3527 | { |
181c37b6 | 3528 | return filename_create(dfd, getname(pathname), path, lookup_flags); |
dae6ad8f AV |
3529 | } |
3530 | EXPORT_SYMBOL(user_path_create); | |
3531 | ||
1a67aafb | 3532 | int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
1da177e4 | 3533 | { |
a95164d9 | 3534 | int error = may_create(dir, dentry); |
1da177e4 LT |
3535 | |
3536 | if (error) | |
3537 | return error; | |
3538 | ||
94f82008 | 3539 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) |
1da177e4 LT |
3540 | return -EPERM; |
3541 | ||
acfa4380 | 3542 | if (!dir->i_op->mknod) |
1da177e4 LT |
3543 | return -EPERM; |
3544 | ||
08ce5f16 SH |
3545 | error = devcgroup_inode_mknod(mode, dev); |
3546 | if (error) | |
3547 | return error; | |
3548 | ||
1da177e4 LT |
3549 | error = security_inode_mknod(dir, dentry, mode, dev); |
3550 | if (error) | |
3551 | return error; | |
3552 | ||
1da177e4 | 3553 | error = dir->i_op->mknod(dir, dentry, mode, dev); |
a74574aa | 3554 | if (!error) |
f38aa942 | 3555 | fsnotify_create(dir, dentry); |
1da177e4 LT |
3556 | return error; |
3557 | } | |
4d359507 | 3558 | EXPORT_SYMBOL(vfs_mknod); |
1da177e4 | 3559 | |
f69aac00 | 3560 | static int may_mknod(umode_t mode) |
463c3197 DH |
3561 | { |
3562 | switch (mode & S_IFMT) { | |
3563 | case S_IFREG: | |
3564 | case S_IFCHR: | |
3565 | case S_IFBLK: | |
3566 | case S_IFIFO: | |
3567 | case S_IFSOCK: | |
3568 | case 0: /* zero mode translates to S_IFREG */ | |
3569 | return 0; | |
3570 | case S_IFDIR: | |
3571 | return -EPERM; | |
3572 | default: | |
3573 | return -EINVAL; | |
3574 | } | |
3575 | } | |
3576 | ||
87c4e192 DB |
3577 | long do_mknodat(int dfd, const char __user *filename, umode_t mode, |
3578 | unsigned int dev) | |
1da177e4 | 3579 | { |
2ad94ae6 | 3580 | struct dentry *dentry; |
dae6ad8f AV |
3581 | struct path path; |
3582 | int error; | |
972567f1 | 3583 | unsigned int lookup_flags = 0; |
1da177e4 | 3584 | |
8e4bfca1 AV |
3585 | error = may_mknod(mode); |
3586 | if (error) | |
3587 | return error; | |
972567f1 JL |
3588 | retry: |
3589 | dentry = user_path_create(dfd, filename, &path, lookup_flags); | |
dae6ad8f AV |
3590 | if (IS_ERR(dentry)) |
3591 | return PTR_ERR(dentry); | |
2ad94ae6 | 3592 | |
dae6ad8f | 3593 | if (!IS_POSIXACL(path.dentry->d_inode)) |
ce3b0f8d | 3594 | mode &= ~current_umask(); |
dae6ad8f | 3595 | error = security_path_mknod(&path, dentry, mode, dev); |
be6d3e56 | 3596 | if (error) |
a8104a9f | 3597 | goto out; |
463c3197 | 3598 | switch (mode & S_IFMT) { |
1da177e4 | 3599 | case 0: case S_IFREG: |
312b63fb | 3600 | error = vfs_create(path.dentry->d_inode,dentry,mode,true); |
05d1a717 MZ |
3601 | if (!error) |
3602 | ima_post_path_mknod(dentry); | |
1da177e4 LT |
3603 | break; |
3604 | case S_IFCHR: case S_IFBLK: | |
dae6ad8f | 3605 | error = vfs_mknod(path.dentry->d_inode,dentry,mode, |
1da177e4 LT |
3606 | new_decode_dev(dev)); |
3607 | break; | |
3608 | case S_IFIFO: case S_IFSOCK: | |
dae6ad8f | 3609 | error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); |
1da177e4 | 3610 | break; |
1da177e4 | 3611 | } |
a8104a9f | 3612 | out: |
921a1650 | 3613 | done_path_create(&path, dentry); |
972567f1 JL |
3614 | if (retry_estale(error, lookup_flags)) { |
3615 | lookup_flags |= LOOKUP_REVAL; | |
3616 | goto retry; | |
3617 | } | |
1da177e4 LT |
3618 | return error; |
3619 | } | |
3620 | ||
87c4e192 DB |
3621 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
3622 | unsigned int, dev) | |
3623 | { | |
3624 | return do_mknodat(dfd, filename, mode, dev); | |
3625 | } | |
3626 | ||
8208a22b | 3627 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
5590ff0d | 3628 | { |
87c4e192 | 3629 | return do_mknodat(AT_FDCWD, filename, mode, dev); |
5590ff0d UD |
3630 | } |
3631 | ||
18bb1db3 | 3632 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1da177e4 | 3633 | { |
a95164d9 | 3634 | int error = may_create(dir, dentry); |
8de52778 | 3635 | unsigned max_links = dir->i_sb->s_max_links; |
1da177e4 LT |
3636 | |
3637 | if (error) | |
3638 | return error; | |
3639 | ||
acfa4380 | 3640 | if (!dir->i_op->mkdir) |
1da177e4 LT |
3641 | return -EPERM; |
3642 | ||
3643 | mode &= (S_IRWXUGO|S_ISVTX); | |
3644 | error = security_inode_mkdir(dir, dentry, mode); | |
3645 | if (error) | |
3646 | return error; | |
3647 | ||
8de52778 AV |
3648 | if (max_links && dir->i_nlink >= max_links) |
3649 | return -EMLINK; | |
3650 | ||
1da177e4 | 3651 | error = dir->i_op->mkdir(dir, dentry, mode); |
a74574aa | 3652 | if (!error) |
f38aa942 | 3653 | fsnotify_mkdir(dir, dentry); |
1da177e4 LT |
3654 | return error; |
3655 | } | |
4d359507 | 3656 | EXPORT_SYMBOL(vfs_mkdir); |
1da177e4 | 3657 | |
0101db7a | 3658 | long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) |
1da177e4 | 3659 | { |
6902d925 | 3660 | struct dentry *dentry; |
dae6ad8f AV |
3661 | struct path path; |
3662 | int error; | |
b76d8b82 | 3663 | unsigned int lookup_flags = LOOKUP_DIRECTORY; |
1da177e4 | 3664 | |
b76d8b82 JL |
3665 | retry: |
3666 | dentry = user_path_create(dfd, pathname, &path, lookup_flags); | |
6902d925 | 3667 | if (IS_ERR(dentry)) |
dae6ad8f | 3668 | return PTR_ERR(dentry); |
1da177e4 | 3669 | |
dae6ad8f | 3670 | if (!IS_POSIXACL(path.dentry->d_inode)) |
ce3b0f8d | 3671 | mode &= ~current_umask(); |
dae6ad8f | 3672 | error = security_path_mkdir(&path, dentry, mode); |
a8104a9f AV |
3673 | if (!error) |
3674 | error = vfs_mkdir(path.dentry->d_inode, dentry, mode); | |
921a1650 | 3675 | done_path_create(&path, dentry); |
b76d8b82 JL |
3676 | if (retry_estale(error, lookup_flags)) { |
3677 | lookup_flags |= LOOKUP_REVAL; | |
3678 | goto retry; | |
3679 | } | |
1da177e4 LT |
3680 | return error; |
3681 | } | |
3682 | ||
0101db7a DB |
3683 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
3684 | { | |
3685 | return do_mkdirat(dfd, pathname, mode); | |
3686 | } | |
3687 | ||
a218d0fd | 3688 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
5590ff0d | 3689 | { |
0101db7a | 3690 | return do_mkdirat(AT_FDCWD, pathname, mode); |
5590ff0d UD |
3691 | } |
3692 | ||
1da177e4 LT |
3693 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) |
3694 | { | |
3695 | int error = may_delete(dir, dentry, 1); | |
3696 | ||
3697 | if (error) | |
3698 | return error; | |
3699 | ||
acfa4380 | 3700 | if (!dir->i_op->rmdir) |
1da177e4 LT |
3701 | return -EPERM; |
3702 | ||
1d2ef590 | 3703 | dget(dentry); |
5955102c | 3704 | inode_lock(dentry->d_inode); |
912dbc15 SW |
3705 | |
3706 | error = -EBUSY; | |
7af1364f | 3707 | if (is_local_mountpoint(dentry)) |
912dbc15 SW |
3708 | goto out; |
3709 | ||
3710 | error = security_inode_rmdir(dir, dentry); | |
3711 | if (error) | |
3712 | goto out; | |
3713 | ||
3714 | error = dir->i_op->rmdir(dir, dentry); | |
3715 | if (error) | |
3716 | goto out; | |
3717 | ||
8767712f | 3718 | shrink_dcache_parent(dentry); |
912dbc15 SW |
3719 | dentry->d_inode->i_flags |= S_DEAD; |
3720 | dont_mount(dentry); | |
8ed936b5 | 3721 | detach_mounts(dentry); |
116b9731 | 3722 | fsnotify_rmdir(dir, dentry); |
912dbc15 SW |
3723 | |
3724 | out: | |
5955102c | 3725 | inode_unlock(dentry->d_inode); |
1d2ef590 | 3726 | dput(dentry); |
912dbc15 | 3727 | if (!error) |
1da177e4 | 3728 | d_delete(dentry); |
1da177e4 LT |
3729 | return error; |
3730 | } | |
4d359507 | 3731 | EXPORT_SYMBOL(vfs_rmdir); |
1da177e4 | 3732 | |
f459dffa | 3733 | long do_rmdir(int dfd, const char __user *pathname) |
1da177e4 LT |
3734 | { |
3735 | int error = 0; | |
91a27b2a | 3736 | struct filename *name; |
1da177e4 | 3737 | struct dentry *dentry; |
f5beed75 AV |
3738 | struct path path; |
3739 | struct qstr last; | |
3740 | int type; | |
c6ee9206 JL |
3741 | unsigned int lookup_flags = 0; |
3742 | retry: | |
c1d4dd27 AV |
3743 | name = filename_parentat(dfd, getname(pathname), lookup_flags, |
3744 | &path, &last, &type); | |
91a27b2a JL |
3745 | if (IS_ERR(name)) |
3746 | return PTR_ERR(name); | |
1da177e4 | 3747 | |
f5beed75 | 3748 | switch (type) { |
0612d9fb OH |
3749 | case LAST_DOTDOT: |
3750 | error = -ENOTEMPTY; | |
3751 | goto exit1; | |
3752 | case LAST_DOT: | |
3753 | error = -EINVAL; | |
3754 | goto exit1; | |
3755 | case LAST_ROOT: | |
3756 | error = -EBUSY; | |
3757 | goto exit1; | |
1da177e4 | 3758 | } |
0612d9fb | 3759 | |
f5beed75 | 3760 | error = mnt_want_write(path.mnt); |
c30dabfe JK |
3761 | if (error) |
3762 | goto exit1; | |
0612d9fb | 3763 | |
5955102c | 3764 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
f5beed75 | 3765 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
1da177e4 | 3766 | error = PTR_ERR(dentry); |
6902d925 DH |
3767 | if (IS_ERR(dentry)) |
3768 | goto exit2; | |
e6bc45d6 TT |
3769 | if (!dentry->d_inode) { |
3770 | error = -ENOENT; | |
3771 | goto exit3; | |
3772 | } | |
f5beed75 | 3773 | error = security_path_rmdir(&path, dentry); |
be6d3e56 | 3774 | if (error) |
c30dabfe | 3775 | goto exit3; |
f5beed75 | 3776 | error = vfs_rmdir(path.dentry->d_inode, dentry); |
0622753b | 3777 | exit3: |
6902d925 DH |
3778 | dput(dentry); |
3779 | exit2: | |
5955102c | 3780 | inode_unlock(path.dentry->d_inode); |
f5beed75 | 3781 | mnt_drop_write(path.mnt); |
1da177e4 | 3782 | exit1: |
f5beed75 | 3783 | path_put(&path); |
1da177e4 | 3784 | putname(name); |
c6ee9206 JL |
3785 | if (retry_estale(error, lookup_flags)) { |
3786 | lookup_flags |= LOOKUP_REVAL; | |
3787 | goto retry; | |
3788 | } | |
1da177e4 LT |
3789 | return error; |
3790 | } | |
3791 | ||
3cdad428 | 3792 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
5590ff0d UD |
3793 | { |
3794 | return do_rmdir(AT_FDCWD, pathname); | |
3795 | } | |
3796 | ||
b21996e3 BF |
3797 | /** |
3798 | * vfs_unlink - unlink a filesystem object | |
3799 | * @dir: parent directory | |
3800 | * @dentry: victim | |
3801 | * @delegated_inode: returns victim inode, if the inode is delegated. | |
3802 | * | |
3803 | * The caller must hold dir->i_mutex. | |
3804 | * | |
3805 | * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and | |
3806 | * return a reference to the inode in delegated_inode. The caller | |
3807 | * should then break the delegation on that inode and retry. Because | |
3808 | * breaking a delegation may take a long time, the caller should drop | |
3809 | * dir->i_mutex before doing so. | |
3810 | * | |
3811 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
3812 | * be appropriate for callers that expect the underlying filesystem not | |
3813 | * to be NFS exported. | |
3814 | */ | |
3815 | int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode) | |
1da177e4 | 3816 | { |
9accbb97 | 3817 | struct inode *target = dentry->d_inode; |
1da177e4 LT |
3818 | int error = may_delete(dir, dentry, 0); |
3819 | ||
3820 | if (error) | |
3821 | return error; | |
3822 | ||
acfa4380 | 3823 | if (!dir->i_op->unlink) |
1da177e4 LT |
3824 | return -EPERM; |
3825 | ||
5955102c | 3826 | inode_lock(target); |
8ed936b5 | 3827 | if (is_local_mountpoint(dentry)) |
1da177e4 LT |
3828 | error = -EBUSY; |
3829 | else { | |
3830 | error = security_inode_unlink(dir, dentry); | |
bec1052e | 3831 | if (!error) { |
5a14696c BF |
3832 | error = try_break_deleg(target, delegated_inode); |
3833 | if (error) | |
b21996e3 | 3834 | goto out; |
1da177e4 | 3835 | error = dir->i_op->unlink(dir, dentry); |
8ed936b5 | 3836 | if (!error) { |
d83c49f3 | 3837 | dont_mount(dentry); |
8ed936b5 | 3838 | detach_mounts(dentry); |
116b9731 | 3839 | fsnotify_unlink(dir, dentry); |
8ed936b5 | 3840 | } |
bec1052e | 3841 | } |
1da177e4 | 3842 | } |
b21996e3 | 3843 | out: |
5955102c | 3844 | inode_unlock(target); |
1da177e4 LT |
3845 | |
3846 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ | |
3847 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { | |
9accbb97 | 3848 | fsnotify_link_count(target); |
e234f35c | 3849 | d_delete(dentry); |
1da177e4 | 3850 | } |
0eeca283 | 3851 | |
1da177e4 LT |
3852 | return error; |
3853 | } | |
4d359507 | 3854 | EXPORT_SYMBOL(vfs_unlink); |
1da177e4 LT |
3855 | |
3856 | /* | |
3857 | * Make sure that the actual truncation of the file will occur outside its | |
1b1dcc1b | 3858 | * directory's i_mutex. Truncate can take a long time if there is a lot of |
1da177e4 LT |
3859 | * writeout happening, and we don't want to prevent access to the directory |
3860 | * while waiting on the I/O. | |
3861 | */ | |
da2f1362 | 3862 | long do_unlinkat(int dfd, struct filename *name) |
1da177e4 | 3863 | { |
2ad94ae6 | 3864 | int error; |
1da177e4 | 3865 | struct dentry *dentry; |
f5beed75 AV |
3866 | struct path path; |
3867 | struct qstr last; | |
3868 | int type; | |
1da177e4 | 3869 | struct inode *inode = NULL; |
b21996e3 | 3870 | struct inode *delegated_inode = NULL; |
5d18f813 JL |
3871 | unsigned int lookup_flags = 0; |
3872 | retry: | |
da2f1362 | 3873 | name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
91a27b2a JL |
3874 | if (IS_ERR(name)) |
3875 | return PTR_ERR(name); | |
2ad94ae6 | 3876 | |
1da177e4 | 3877 | error = -EISDIR; |
f5beed75 | 3878 | if (type != LAST_NORM) |
1da177e4 | 3879 | goto exit1; |
0612d9fb | 3880 | |
f5beed75 | 3881 | error = mnt_want_write(path.mnt); |
c30dabfe JK |
3882 | if (error) |
3883 | goto exit1; | |
b21996e3 | 3884 | retry_deleg: |
5955102c | 3885 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
f5beed75 | 3886 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
1da177e4 LT |
3887 | error = PTR_ERR(dentry); |
3888 | if (!IS_ERR(dentry)) { | |
3889 | /* Why not before? Because we want correct error value */ | |
f5beed75 | 3890 | if (last.name[last.len]) |
50338b88 | 3891 | goto slashes; |
1da177e4 | 3892 | inode = dentry->d_inode; |
b18825a7 | 3893 | if (d_is_negative(dentry)) |
e6bc45d6 TT |
3894 | goto slashes; |
3895 | ihold(inode); | |
f5beed75 | 3896 | error = security_path_unlink(&path, dentry); |
be6d3e56 | 3897 | if (error) |
c30dabfe | 3898 | goto exit2; |
f5beed75 | 3899 | error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode); |
c30dabfe | 3900 | exit2: |
1da177e4 LT |
3901 | dput(dentry); |
3902 | } | |
5955102c | 3903 | inode_unlock(path.dentry->d_inode); |
1da177e4 LT |
3904 | if (inode) |
3905 | iput(inode); /* truncate the inode here */ | |
b21996e3 BF |
3906 | inode = NULL; |
3907 | if (delegated_inode) { | |
5a14696c | 3908 | error = break_deleg_wait(&delegated_inode); |
b21996e3 BF |
3909 | if (!error) |
3910 | goto retry_deleg; | |
3911 | } | |
f5beed75 | 3912 | mnt_drop_write(path.mnt); |
1da177e4 | 3913 | exit1: |
f5beed75 | 3914 | path_put(&path); |
5d18f813 JL |
3915 | if (retry_estale(error, lookup_flags)) { |
3916 | lookup_flags |= LOOKUP_REVAL; | |
3917 | inode = NULL; | |
3918 | goto retry; | |
3919 | } | |
da2f1362 | 3920 | putname(name); |
1da177e4 LT |
3921 | return error; |
3922 | ||
3923 | slashes: | |
b18825a7 DH |
3924 | if (d_is_negative(dentry)) |
3925 | error = -ENOENT; | |
44b1d530 | 3926 | else if (d_is_dir(dentry)) |
b18825a7 DH |
3927 | error = -EISDIR; |
3928 | else | |
3929 | error = -ENOTDIR; | |
1da177e4 LT |
3930 | goto exit2; |
3931 | } | |
3932 | ||
2e4d0924 | 3933 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
5590ff0d UD |
3934 | { |
3935 | if ((flag & ~AT_REMOVEDIR) != 0) | |
3936 | return -EINVAL; | |
3937 | ||
3938 | if (flag & AT_REMOVEDIR) | |
3939 | return do_rmdir(dfd, pathname); | |
3940 | ||
da2f1362 | 3941 | return do_unlinkat(dfd, getname(pathname)); |
5590ff0d UD |
3942 | } |
3943 | ||
3480b257 | 3944 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
5590ff0d | 3945 | { |
da2f1362 | 3946 | return do_unlinkat(AT_FDCWD, getname(pathname)); |
5590ff0d UD |
3947 | } |
3948 | ||
db2e747b | 3949 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) |
1da177e4 | 3950 | { |
a95164d9 | 3951 | int error = may_create(dir, dentry); |
1da177e4 LT |
3952 | |
3953 | if (error) | |
3954 | return error; | |
3955 | ||
acfa4380 | 3956 | if (!dir->i_op->symlink) |
1da177e4 LT |
3957 | return -EPERM; |
3958 | ||
3959 | error = security_inode_symlink(dir, dentry, oldname); | |
3960 | if (error) | |
3961 | return error; | |
3962 | ||
1da177e4 | 3963 | error = dir->i_op->symlink(dir, dentry, oldname); |
a74574aa | 3964 | if (!error) |
f38aa942 | 3965 | fsnotify_create(dir, dentry); |
1da177e4 LT |
3966 | return error; |
3967 | } | |
4d359507 | 3968 | EXPORT_SYMBOL(vfs_symlink); |
1da177e4 | 3969 | |
b724e846 DB |
3970 | long do_symlinkat(const char __user *oldname, int newdfd, |
3971 | const char __user *newname) | |
1da177e4 | 3972 | { |
2ad94ae6 | 3973 | int error; |
91a27b2a | 3974 | struct filename *from; |
6902d925 | 3975 | struct dentry *dentry; |
dae6ad8f | 3976 | struct path path; |
f46d3567 | 3977 | unsigned int lookup_flags = 0; |
1da177e4 LT |
3978 | |
3979 | from = getname(oldname); | |
2ad94ae6 | 3980 | if (IS_ERR(from)) |
1da177e4 | 3981 | return PTR_ERR(from); |
f46d3567 JL |
3982 | retry: |
3983 | dentry = user_path_create(newdfd, newname, &path, lookup_flags); | |
6902d925 DH |
3984 | error = PTR_ERR(dentry); |
3985 | if (IS_ERR(dentry)) | |
dae6ad8f | 3986 | goto out_putname; |
6902d925 | 3987 | |
91a27b2a | 3988 | error = security_path_symlink(&path, dentry, from->name); |
a8104a9f | 3989 | if (!error) |
91a27b2a | 3990 | error = vfs_symlink(path.dentry->d_inode, dentry, from->name); |
921a1650 | 3991 | done_path_create(&path, dentry); |
f46d3567 JL |
3992 | if (retry_estale(error, lookup_flags)) { |
3993 | lookup_flags |= LOOKUP_REVAL; | |
3994 | goto retry; | |
3995 | } | |
6902d925 | 3996 | out_putname: |
1da177e4 LT |
3997 | putname(from); |
3998 | return error; | |
3999 | } | |
4000 | ||
b724e846 DB |
4001 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
4002 | int, newdfd, const char __user *, newname) | |
4003 | { | |
4004 | return do_symlinkat(oldname, newdfd, newname); | |
4005 | } | |
4006 | ||
3480b257 | 4007 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4008 | { |
b724e846 | 4009 | return do_symlinkat(oldname, AT_FDCWD, newname); |
5590ff0d UD |
4010 | } |
4011 | ||
146a8595 BF |
4012 | /** |
4013 | * vfs_link - create a new link | |
4014 | * @old_dentry: object to be linked | |
4015 | * @dir: new parent | |
4016 | * @new_dentry: where to create the new link | |
4017 | * @delegated_inode: returns inode needing a delegation break | |
4018 | * | |
4019 | * The caller must hold dir->i_mutex | |
4020 | * | |
4021 | * If vfs_link discovers a delegation on the to-be-linked file in need | |
4022 | * of breaking, it will return -EWOULDBLOCK and return a reference to the | |
4023 | * inode in delegated_inode. The caller should then break the delegation | |
4024 | * and retry. Because breaking a delegation may take a long time, the | |
4025 | * caller should drop the i_mutex before doing so. | |
4026 | * | |
4027 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4028 | * be appropriate for callers that expect the underlying filesystem not | |
4029 | * to be NFS exported. | |
4030 | */ | |
4031 | int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode) | |
1da177e4 LT |
4032 | { |
4033 | struct inode *inode = old_dentry->d_inode; | |
8de52778 | 4034 | unsigned max_links = dir->i_sb->s_max_links; |
1da177e4 LT |
4035 | int error; |
4036 | ||
4037 | if (!inode) | |
4038 | return -ENOENT; | |
4039 | ||
a95164d9 | 4040 | error = may_create(dir, new_dentry); |
1da177e4 LT |
4041 | if (error) |
4042 | return error; | |
4043 | ||
4044 | if (dir->i_sb != inode->i_sb) | |
4045 | return -EXDEV; | |
4046 | ||
4047 | /* | |
4048 | * A link to an append-only or immutable file cannot be created. | |
4049 | */ | |
4050 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
4051 | return -EPERM; | |
0bd23d09 EB |
4052 | /* |
4053 | * Updating the link count will likely cause i_uid and i_gid to | |
4054 | * be writen back improperly if their true value is unknown to | |
4055 | * the vfs. | |
4056 | */ | |
4057 | if (HAS_UNMAPPED_ID(inode)) | |
4058 | return -EPERM; | |
acfa4380 | 4059 | if (!dir->i_op->link) |
1da177e4 | 4060 | return -EPERM; |
7e79eedb | 4061 | if (S_ISDIR(inode->i_mode)) |
1da177e4 LT |
4062 | return -EPERM; |
4063 | ||
4064 | error = security_inode_link(old_dentry, dir, new_dentry); | |
4065 | if (error) | |
4066 | return error; | |
4067 | ||
5955102c | 4068 | inode_lock(inode); |
aae8a97d | 4069 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
f4e0c30c | 4070 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
aae8a97d | 4071 | error = -ENOENT; |
8de52778 AV |
4072 | else if (max_links && inode->i_nlink >= max_links) |
4073 | error = -EMLINK; | |
146a8595 BF |
4074 | else { |
4075 | error = try_break_deleg(inode, delegated_inode); | |
4076 | if (!error) | |
4077 | error = dir->i_op->link(old_dentry, dir, new_dentry); | |
4078 | } | |
f4e0c30c AV |
4079 | |
4080 | if (!error && (inode->i_state & I_LINKABLE)) { | |
4081 | spin_lock(&inode->i_lock); | |
4082 | inode->i_state &= ~I_LINKABLE; | |
4083 | spin_unlock(&inode->i_lock); | |
4084 | } | |
5955102c | 4085 | inode_unlock(inode); |
e31e14ec | 4086 | if (!error) |
7e79eedb | 4087 | fsnotify_link(dir, inode, new_dentry); |
1da177e4 LT |
4088 | return error; |
4089 | } | |
4d359507 | 4090 | EXPORT_SYMBOL(vfs_link); |
1da177e4 LT |
4091 | |
4092 | /* | |
4093 | * Hardlinks are often used in delicate situations. We avoid | |
4094 | * security-related surprises by not following symlinks on the | |
4095 | * newname. --KAB | |
4096 | * | |
4097 | * We don't follow them on the oldname either to be compatible | |
4098 | * with linux 2.0, and to avoid hard-linking to directories | |
4099 | * and other special files. --ADM | |
4100 | */ | |
46ea89eb DB |
4101 | int do_linkat(int olddfd, const char __user *oldname, int newdfd, |
4102 | const char __user *newname, int flags) | |
1da177e4 LT |
4103 | { |
4104 | struct dentry *new_dentry; | |
dae6ad8f | 4105 | struct path old_path, new_path; |
146a8595 | 4106 | struct inode *delegated_inode = NULL; |
11a7b371 | 4107 | int how = 0; |
1da177e4 | 4108 | int error; |
1da177e4 | 4109 | |
11a7b371 | 4110 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) |
c04030e1 | 4111 | return -EINVAL; |
11a7b371 | 4112 | /* |
f0cc6ffb LT |
4113 | * To use null names we require CAP_DAC_READ_SEARCH |
4114 | * This ensures that not everyone will be able to create | |
4115 | * handlink using the passed filedescriptor. | |
11a7b371 | 4116 | */ |
f0cc6ffb LT |
4117 | if (flags & AT_EMPTY_PATH) { |
4118 | if (!capable(CAP_DAC_READ_SEARCH)) | |
4119 | return -ENOENT; | |
11a7b371 | 4120 | how = LOOKUP_EMPTY; |
f0cc6ffb | 4121 | } |
11a7b371 AK |
4122 | |
4123 | if (flags & AT_SYMLINK_FOLLOW) | |
4124 | how |= LOOKUP_FOLLOW; | |
442e31ca | 4125 | retry: |
11a7b371 | 4126 | error = user_path_at(olddfd, oldname, how, &old_path); |
1da177e4 | 4127 | if (error) |
2ad94ae6 AV |
4128 | return error; |
4129 | ||
442e31ca JL |
4130 | new_dentry = user_path_create(newdfd, newname, &new_path, |
4131 | (how & LOOKUP_REVAL)); | |
1da177e4 | 4132 | error = PTR_ERR(new_dentry); |
6902d925 | 4133 | if (IS_ERR(new_dentry)) |
dae6ad8f AV |
4134 | goto out; |
4135 | ||
4136 | error = -EXDEV; | |
4137 | if (old_path.mnt != new_path.mnt) | |
4138 | goto out_dput; | |
800179c9 KC |
4139 | error = may_linkat(&old_path); |
4140 | if (unlikely(error)) | |
4141 | goto out_dput; | |
dae6ad8f | 4142 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
be6d3e56 | 4143 | if (error) |
a8104a9f | 4144 | goto out_dput; |
146a8595 | 4145 | error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode); |
75c3f29d | 4146 | out_dput: |
921a1650 | 4147 | done_path_create(&new_path, new_dentry); |
146a8595 BF |
4148 | if (delegated_inode) { |
4149 | error = break_deleg_wait(&delegated_inode); | |
d22e6338 OD |
4150 | if (!error) { |
4151 | path_put(&old_path); | |
146a8595 | 4152 | goto retry; |
d22e6338 | 4153 | } |
146a8595 | 4154 | } |
442e31ca | 4155 | if (retry_estale(error, how)) { |
d22e6338 | 4156 | path_put(&old_path); |
442e31ca JL |
4157 | how |= LOOKUP_REVAL; |
4158 | goto retry; | |
4159 | } | |
1da177e4 | 4160 | out: |
2d8f3038 | 4161 | path_put(&old_path); |
1da177e4 LT |
4162 | |
4163 | return error; | |
4164 | } | |
4165 | ||
46ea89eb DB |
4166 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
4167 | int, newdfd, const char __user *, newname, int, flags) | |
4168 | { | |
4169 | return do_linkat(olddfd, oldname, newdfd, newname, flags); | |
4170 | } | |
4171 | ||
3480b257 | 4172 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4173 | { |
46ea89eb | 4174 | return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
5590ff0d UD |
4175 | } |
4176 | ||
bc27027a MS |
4177 | /** |
4178 | * vfs_rename - rename a filesystem object | |
4179 | * @old_dir: parent of source | |
4180 | * @old_dentry: source | |
4181 | * @new_dir: parent of destination | |
4182 | * @new_dentry: destination | |
4183 | * @delegated_inode: returns an inode needing a delegation break | |
520c8b16 | 4184 | * @flags: rename flags |
bc27027a MS |
4185 | * |
4186 | * The caller must hold multiple mutexes--see lock_rename()). | |
4187 | * | |
4188 | * If vfs_rename discovers a delegation in need of breaking at either | |
4189 | * the source or destination, it will return -EWOULDBLOCK and return a | |
4190 | * reference to the inode in delegated_inode. The caller should then | |
4191 | * break the delegation and retry. Because breaking a delegation may | |
4192 | * take a long time, the caller should drop all locks before doing | |
4193 | * so. | |
4194 | * | |
4195 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4196 | * be appropriate for callers that expect the underlying filesystem not | |
4197 | * to be NFS exported. | |
4198 | * | |
1da177e4 LT |
4199 | * The worst of all namespace operations - renaming directory. "Perverted" |
4200 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... | |
4201 | * Problems: | |
0117d427 | 4202 | * |
d03b29a2 | 4203 | * a) we can get into loop creation. |
1da177e4 LT |
4204 | * b) race potential - two innocent renames can create a loop together. |
4205 | * That's where 4.4 screws up. Current fix: serialization on | |
a11f3a05 | 4206 | * sb->s_vfs_rename_mutex. We might be more accurate, but that's another |
1da177e4 | 4207 | * story. |
6cedba89 BF |
4208 | * c) we have to lock _four_ objects - parents and victim (if it exists), |
4209 | * and source (if it is not a directory). | |
1b1dcc1b | 4210 | * And that - after we got ->i_mutex on parents (until then we don't know |
1da177e4 LT |
4211 | * whether the target exists). Solution: try to be smart with locking |
4212 | * order for inodes. We rely on the fact that tree topology may change | |
a11f3a05 | 4213 | * only under ->s_vfs_rename_mutex _and_ that parent of the object we |
1da177e4 LT |
4214 | * move will be locked. Thus we can rank directories by the tree |
4215 | * (ancestors first) and rank all non-directories after them. | |
4216 | * That works since everybody except rename does "lock parent, lookup, | |
a11f3a05 | 4217 | * lock child" and rename is under ->s_vfs_rename_mutex. |
1da177e4 LT |
4218 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
4219 | * has no more than 1 dentry. If "hybrid" objects will ever appear, | |
4220 | * we'd better make sure that there's no link(2) for them. | |
e4eaac06 | 4221 | * d) conversion from fhandle to dentry may come in the wrong moment - when |
1b1dcc1b | 4222 | * we are removing the target. Solution: we will have to grab ->i_mutex |
1da177e4 | 4223 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
c41b20e7 | 4224 | * ->i_mutex on parents, which works but leads to some truly excessive |
1da177e4 LT |
4225 | * locking]. |
4226 | */ | |
bc27027a MS |
4227 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
4228 | struct inode *new_dir, struct dentry *new_dentry, | |
520c8b16 | 4229 | struct inode **delegated_inode, unsigned int flags) |
1da177e4 | 4230 | { |
bc27027a MS |
4231 | int error; |
4232 | bool is_dir = d_is_dir(old_dentry); | |
bc27027a | 4233 | struct inode *source = old_dentry->d_inode; |
9055cba7 | 4234 | struct inode *target = new_dentry->d_inode; |
da1ce067 MS |
4235 | bool new_is_dir = false; |
4236 | unsigned max_links = new_dir->i_sb->s_max_links; | |
49d31c2f | 4237 | struct name_snapshot old_name; |
bc27027a | 4238 | |
8d3e2936 | 4239 | if (source == target) |
bc27027a MS |
4240 | return 0; |
4241 | ||
4242 | error = may_delete(old_dir, old_dentry, is_dir); | |
4243 | if (error) | |
4244 | return error; | |
4245 | ||
da1ce067 | 4246 | if (!target) { |
bc27027a | 4247 | error = may_create(new_dir, new_dentry); |
da1ce067 MS |
4248 | } else { |
4249 | new_is_dir = d_is_dir(new_dentry); | |
4250 | ||
4251 | if (!(flags & RENAME_EXCHANGE)) | |
4252 | error = may_delete(new_dir, new_dentry, is_dir); | |
4253 | else | |
4254 | error = may_delete(new_dir, new_dentry, new_is_dir); | |
4255 | } | |
bc27027a MS |
4256 | if (error) |
4257 | return error; | |
4258 | ||
2773bf00 | 4259 | if (!old_dir->i_op->rename) |
bc27027a | 4260 | return -EPERM; |
1da177e4 LT |
4261 | |
4262 | /* | |
4263 | * If we are going to change the parent - check write permissions, | |
4264 | * we'll need to flip '..'. | |
4265 | */ | |
da1ce067 MS |
4266 | if (new_dir != old_dir) { |
4267 | if (is_dir) { | |
4268 | error = inode_permission(source, MAY_WRITE); | |
4269 | if (error) | |
4270 | return error; | |
4271 | } | |
4272 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { | |
4273 | error = inode_permission(target, MAY_WRITE); | |
4274 | if (error) | |
4275 | return error; | |
4276 | } | |
1da177e4 LT |
4277 | } |
4278 | ||
0b3974eb MS |
4279 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, |
4280 | flags); | |
1da177e4 LT |
4281 | if (error) |
4282 | return error; | |
4283 | ||
49d31c2f | 4284 | take_dentry_name_snapshot(&old_name, old_dentry); |
1d2ef590 | 4285 | dget(new_dentry); |
da1ce067 | 4286 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
bc27027a MS |
4287 | lock_two_nondirectories(source, target); |
4288 | else if (target) | |
5955102c | 4289 | inode_lock(target); |
9055cba7 SW |
4290 | |
4291 | error = -EBUSY; | |
7af1364f | 4292 | if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) |
9055cba7 SW |
4293 | goto out; |
4294 | ||
da1ce067 | 4295 | if (max_links && new_dir != old_dir) { |
bc27027a | 4296 | error = -EMLINK; |
da1ce067 | 4297 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) |
bc27027a | 4298 | goto out; |
da1ce067 MS |
4299 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && |
4300 | old_dir->i_nlink >= max_links) | |
4301 | goto out; | |
4302 | } | |
da1ce067 | 4303 | if (!is_dir) { |
bc27027a | 4304 | error = try_break_deleg(source, delegated_inode); |
8e6d782c BF |
4305 | if (error) |
4306 | goto out; | |
da1ce067 MS |
4307 | } |
4308 | if (target && !new_is_dir) { | |
4309 | error = try_break_deleg(target, delegated_inode); | |
4310 | if (error) | |
4311 | goto out; | |
8e6d782c | 4312 | } |
2773bf00 | 4313 | error = old_dir->i_op->rename(old_dir, old_dentry, |
18fc84da | 4314 | new_dir, new_dentry, flags); |
51892bbb SW |
4315 | if (error) |
4316 | goto out; | |
4317 | ||
da1ce067 | 4318 | if (!(flags & RENAME_EXCHANGE) && target) { |
8767712f AV |
4319 | if (is_dir) { |
4320 | shrink_dcache_parent(new_dentry); | |
bc27027a | 4321 | target->i_flags |= S_DEAD; |
8767712f | 4322 | } |
51892bbb | 4323 | dont_mount(new_dentry); |
8ed936b5 | 4324 | detach_mounts(new_dentry); |
bc27027a | 4325 | } |
da1ce067 MS |
4326 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { |
4327 | if (!(flags & RENAME_EXCHANGE)) | |
4328 | d_move(old_dentry, new_dentry); | |
4329 | else | |
4330 | d_exchange(old_dentry, new_dentry); | |
4331 | } | |
51892bbb | 4332 | out: |
da1ce067 | 4333 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
bc27027a MS |
4334 | unlock_two_nondirectories(source, target); |
4335 | else if (target) | |
5955102c | 4336 | inode_unlock(target); |
1da177e4 | 4337 | dput(new_dentry); |
da1ce067 | 4338 | if (!error) { |
f4ec3a3d | 4339 | fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, |
da1ce067 MS |
4340 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); |
4341 | if (flags & RENAME_EXCHANGE) { | |
f4ec3a3d | 4342 | fsnotify_move(new_dir, old_dir, &old_dentry->d_name, |
da1ce067 MS |
4343 | new_is_dir, NULL, new_dentry); |
4344 | } | |
4345 | } | |
49d31c2f | 4346 | release_dentry_name_snapshot(&old_name); |
0eeca283 | 4347 | |
1da177e4 LT |
4348 | return error; |
4349 | } | |
4d359507 | 4350 | EXPORT_SYMBOL(vfs_rename); |
1da177e4 | 4351 | |
ee81feb6 DB |
4352 | static int do_renameat2(int olddfd, const char __user *oldname, int newdfd, |
4353 | const char __user *newname, unsigned int flags) | |
1da177e4 | 4354 | { |
2ad94ae6 AV |
4355 | struct dentry *old_dentry, *new_dentry; |
4356 | struct dentry *trap; | |
f5beed75 AV |
4357 | struct path old_path, new_path; |
4358 | struct qstr old_last, new_last; | |
4359 | int old_type, new_type; | |
8e6d782c | 4360 | struct inode *delegated_inode = NULL; |
91a27b2a JL |
4361 | struct filename *from; |
4362 | struct filename *to; | |
f5beed75 | 4363 | unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; |
c6a94284 | 4364 | bool should_retry = false; |
2ad94ae6 | 4365 | int error; |
520c8b16 | 4366 | |
0d7a8555 | 4367 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
da1ce067 MS |
4368 | return -EINVAL; |
4369 | ||
0d7a8555 MS |
4370 | if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && |
4371 | (flags & RENAME_EXCHANGE)) | |
520c8b16 MS |
4372 | return -EINVAL; |
4373 | ||
0d7a8555 MS |
4374 | if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD)) |
4375 | return -EPERM; | |
4376 | ||
f5beed75 AV |
4377 | if (flags & RENAME_EXCHANGE) |
4378 | target_flags = 0; | |
4379 | ||
c6a94284 | 4380 | retry: |
c1d4dd27 AV |
4381 | from = filename_parentat(olddfd, getname(oldname), lookup_flags, |
4382 | &old_path, &old_last, &old_type); | |
91a27b2a JL |
4383 | if (IS_ERR(from)) { |
4384 | error = PTR_ERR(from); | |
1da177e4 | 4385 | goto exit; |
91a27b2a | 4386 | } |
1da177e4 | 4387 | |
c1d4dd27 AV |
4388 | to = filename_parentat(newdfd, getname(newname), lookup_flags, |
4389 | &new_path, &new_last, &new_type); | |
91a27b2a JL |
4390 | if (IS_ERR(to)) { |
4391 | error = PTR_ERR(to); | |
1da177e4 | 4392 | goto exit1; |
91a27b2a | 4393 | } |
1da177e4 LT |
4394 | |
4395 | error = -EXDEV; | |
f5beed75 | 4396 | if (old_path.mnt != new_path.mnt) |
1da177e4 LT |
4397 | goto exit2; |
4398 | ||
1da177e4 | 4399 | error = -EBUSY; |
f5beed75 | 4400 | if (old_type != LAST_NORM) |
1da177e4 LT |
4401 | goto exit2; |
4402 | ||
0a7c3937 MS |
4403 | if (flags & RENAME_NOREPLACE) |
4404 | error = -EEXIST; | |
f5beed75 | 4405 | if (new_type != LAST_NORM) |
1da177e4 LT |
4406 | goto exit2; |
4407 | ||
f5beed75 | 4408 | error = mnt_want_write(old_path.mnt); |
c30dabfe JK |
4409 | if (error) |
4410 | goto exit2; | |
4411 | ||
8e6d782c | 4412 | retry_deleg: |
f5beed75 | 4413 | trap = lock_rename(new_path.dentry, old_path.dentry); |
1da177e4 | 4414 | |
f5beed75 | 4415 | old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); |
1da177e4 LT |
4416 | error = PTR_ERR(old_dentry); |
4417 | if (IS_ERR(old_dentry)) | |
4418 | goto exit3; | |
4419 | /* source must exist */ | |
4420 | error = -ENOENT; | |
b18825a7 | 4421 | if (d_is_negative(old_dentry)) |
1da177e4 | 4422 | goto exit4; |
f5beed75 | 4423 | new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags); |
0a7c3937 MS |
4424 | error = PTR_ERR(new_dentry); |
4425 | if (IS_ERR(new_dentry)) | |
4426 | goto exit4; | |
4427 | error = -EEXIST; | |
4428 | if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) | |
4429 | goto exit5; | |
da1ce067 MS |
4430 | if (flags & RENAME_EXCHANGE) { |
4431 | error = -ENOENT; | |
4432 | if (d_is_negative(new_dentry)) | |
4433 | goto exit5; | |
4434 | ||
4435 | if (!d_is_dir(new_dentry)) { | |
4436 | error = -ENOTDIR; | |
f5beed75 | 4437 | if (new_last.name[new_last.len]) |
da1ce067 MS |
4438 | goto exit5; |
4439 | } | |
4440 | } | |
1da177e4 | 4441 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
44b1d530 | 4442 | if (!d_is_dir(old_dentry)) { |
1da177e4 | 4443 | error = -ENOTDIR; |
f5beed75 | 4444 | if (old_last.name[old_last.len]) |
0a7c3937 | 4445 | goto exit5; |
f5beed75 | 4446 | if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) |
0a7c3937 | 4447 | goto exit5; |
1da177e4 LT |
4448 | } |
4449 | /* source should not be ancestor of target */ | |
4450 | error = -EINVAL; | |
4451 | if (old_dentry == trap) | |
0a7c3937 | 4452 | goto exit5; |
1da177e4 | 4453 | /* target should not be an ancestor of source */ |
da1ce067 MS |
4454 | if (!(flags & RENAME_EXCHANGE)) |
4455 | error = -ENOTEMPTY; | |
1da177e4 LT |
4456 | if (new_dentry == trap) |
4457 | goto exit5; | |
4458 | ||
f5beed75 AV |
4459 | error = security_path_rename(&old_path, old_dentry, |
4460 | &new_path, new_dentry, flags); | |
be6d3e56 | 4461 | if (error) |
c30dabfe | 4462 | goto exit5; |
f5beed75 AV |
4463 | error = vfs_rename(old_path.dentry->d_inode, old_dentry, |
4464 | new_path.dentry->d_inode, new_dentry, | |
520c8b16 | 4465 | &delegated_inode, flags); |
1da177e4 LT |
4466 | exit5: |
4467 | dput(new_dentry); | |
4468 | exit4: | |
4469 | dput(old_dentry); | |
4470 | exit3: | |
f5beed75 | 4471 | unlock_rename(new_path.dentry, old_path.dentry); |
8e6d782c BF |
4472 | if (delegated_inode) { |
4473 | error = break_deleg_wait(&delegated_inode); | |
4474 | if (!error) | |
4475 | goto retry_deleg; | |
4476 | } | |
f5beed75 | 4477 | mnt_drop_write(old_path.mnt); |
1da177e4 | 4478 | exit2: |
c6a94284 JL |
4479 | if (retry_estale(error, lookup_flags)) |
4480 | should_retry = true; | |
f5beed75 | 4481 | path_put(&new_path); |
2ad94ae6 | 4482 | putname(to); |
1da177e4 | 4483 | exit1: |
f5beed75 | 4484 | path_put(&old_path); |
1da177e4 | 4485 | putname(from); |
c6a94284 JL |
4486 | if (should_retry) { |
4487 | should_retry = false; | |
4488 | lookup_flags |= LOOKUP_REVAL; | |
4489 | goto retry; | |
4490 | } | |
2ad94ae6 | 4491 | exit: |
1da177e4 LT |
4492 | return error; |
4493 | } | |
4494 | ||
ee81feb6 DB |
4495 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, |
4496 | int, newdfd, const char __user *, newname, unsigned int, flags) | |
4497 | { | |
4498 | return do_renameat2(olddfd, oldname, newdfd, newname, flags); | |
4499 | } | |
4500 | ||
520c8b16 MS |
4501 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
4502 | int, newdfd, const char __user *, newname) | |
4503 | { | |
ee81feb6 | 4504 | return do_renameat2(olddfd, oldname, newdfd, newname, 0); |
520c8b16 MS |
4505 | } |
4506 | ||
a26eab24 | 4507 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4508 | { |
ee81feb6 | 4509 | return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
5590ff0d UD |
4510 | } |
4511 | ||
787fb6bc MS |
4512 | int vfs_whiteout(struct inode *dir, struct dentry *dentry) |
4513 | { | |
4514 | int error = may_create(dir, dentry); | |
4515 | if (error) | |
4516 | return error; | |
4517 | ||
4518 | if (!dir->i_op->mknod) | |
4519 | return -EPERM; | |
4520 | ||
4521 | return dir->i_op->mknod(dir, dentry, | |
4522 | S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); | |
4523 | } | |
4524 | EXPORT_SYMBOL(vfs_whiteout); | |
4525 | ||
5d826c84 | 4526 | int readlink_copy(char __user *buffer, int buflen, const char *link) |
1da177e4 | 4527 | { |
5d826c84 | 4528 | int len = PTR_ERR(link); |
1da177e4 LT |
4529 | if (IS_ERR(link)) |
4530 | goto out; | |
4531 | ||
4532 | len = strlen(link); | |
4533 | if (len > (unsigned) buflen) | |
4534 | len = buflen; | |
4535 | if (copy_to_user(buffer, link, len)) | |
4536 | len = -EFAULT; | |
4537 | out: | |
4538 | return len; | |
4539 | } | |
4540 | ||
fd4a0edf MS |
4541 | /** |
4542 | * vfs_readlink - copy symlink body into userspace buffer | |
4543 | * @dentry: dentry on which to get symbolic link | |
4544 | * @buffer: user memory pointer | |
4545 | * @buflen: size of buffer | |
4546 | * | |
4547 | * Does not touch atime. That's up to the caller if necessary | |
4548 | * | |
4549 | * Does not call security hook. | |
4550 | */ | |
4551 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) | |
4552 | { | |
4553 | struct inode *inode = d_inode(dentry); | |
f2df5da6 AV |
4554 | DEFINE_DELAYED_CALL(done); |
4555 | const char *link; | |
4556 | int res; | |
fd4a0edf | 4557 | |
76fca90e MS |
4558 | if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { |
4559 | if (unlikely(inode->i_op->readlink)) | |
4560 | return inode->i_op->readlink(dentry, buffer, buflen); | |
4561 | ||
4562 | if (!d_is_symlink(dentry)) | |
4563 | return -EINVAL; | |
4564 | ||
4565 | spin_lock(&inode->i_lock); | |
4566 | inode->i_opflags |= IOP_DEFAULT_READLINK; | |
4567 | spin_unlock(&inode->i_lock); | |
4568 | } | |
fd4a0edf | 4569 | |
4c4f7c19 | 4570 | link = READ_ONCE(inode->i_link); |
f2df5da6 AV |
4571 | if (!link) { |
4572 | link = inode->i_op->get_link(dentry, inode, &done); | |
4573 | if (IS_ERR(link)) | |
4574 | return PTR_ERR(link); | |
4575 | } | |
4576 | res = readlink_copy(buffer, buflen, link); | |
4577 | do_delayed_call(&done); | |
4578 | return res; | |
fd4a0edf MS |
4579 | } |
4580 | EXPORT_SYMBOL(vfs_readlink); | |
1da177e4 | 4581 | |
d60874cd MS |
4582 | /** |
4583 | * vfs_get_link - get symlink body | |
4584 | * @dentry: dentry on which to get symbolic link | |
4585 | * @done: caller needs to free returned data with this | |
4586 | * | |
4587 | * Calls security hook and i_op->get_link() on the supplied inode. | |
4588 | * | |
4589 | * It does not touch atime. That's up to the caller if necessary. | |
4590 | * | |
4591 | * Does not work on "special" symlinks like /proc/$$/fd/N | |
4592 | */ | |
4593 | const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) | |
4594 | { | |
4595 | const char *res = ERR_PTR(-EINVAL); | |
4596 | struct inode *inode = d_inode(dentry); | |
4597 | ||
4598 | if (d_is_symlink(dentry)) { | |
4599 | res = ERR_PTR(security_inode_readlink(dentry)); | |
4600 | if (!res) | |
4601 | res = inode->i_op->get_link(dentry, inode, done); | |
4602 | } | |
4603 | return res; | |
4604 | } | |
4605 | EXPORT_SYMBOL(vfs_get_link); | |
4606 | ||
1da177e4 | 4607 | /* get the link contents into pagecache */ |
6b255391 | 4608 | const char *page_get_link(struct dentry *dentry, struct inode *inode, |
fceef393 | 4609 | struct delayed_call *callback) |
1da177e4 | 4610 | { |
ebd09abb DG |
4611 | char *kaddr; |
4612 | struct page *page; | |
6b255391 AV |
4613 | struct address_space *mapping = inode->i_mapping; |
4614 | ||
d3883d4f AV |
4615 | if (!dentry) { |
4616 | page = find_get_page(mapping, 0); | |
4617 | if (!page) | |
4618 | return ERR_PTR(-ECHILD); | |
4619 | if (!PageUptodate(page)) { | |
4620 | put_page(page); | |
4621 | return ERR_PTR(-ECHILD); | |
4622 | } | |
4623 | } else { | |
4624 | page = read_mapping_page(mapping, 0, NULL); | |
4625 | if (IS_ERR(page)) | |
4626 | return (char*)page; | |
4627 | } | |
fceef393 | 4628 | set_delayed_call(callback, page_put_link, page); |
21fc61c7 AV |
4629 | BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); |
4630 | kaddr = page_address(page); | |
6b255391 | 4631 | nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); |
ebd09abb | 4632 | return kaddr; |
1da177e4 LT |
4633 | } |
4634 | ||
6b255391 | 4635 | EXPORT_SYMBOL(page_get_link); |
1da177e4 | 4636 | |
fceef393 | 4637 | void page_put_link(void *arg) |
1da177e4 | 4638 | { |
fceef393 | 4639 | put_page(arg); |
1da177e4 | 4640 | } |
4d359507 | 4641 | EXPORT_SYMBOL(page_put_link); |
1da177e4 | 4642 | |
aa80deab AV |
4643 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
4644 | { | |
fceef393 | 4645 | DEFINE_DELAYED_CALL(done); |
6b255391 AV |
4646 | int res = readlink_copy(buffer, buflen, |
4647 | page_get_link(dentry, d_inode(dentry), | |
fceef393 AV |
4648 | &done)); |
4649 | do_delayed_call(&done); | |
aa80deab AV |
4650 | return res; |
4651 | } | |
4652 | EXPORT_SYMBOL(page_readlink); | |
4653 | ||
54566b2c NP |
4654 | /* |
4655 | * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS | |
4656 | */ | |
4657 | int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) | |
1da177e4 LT |
4658 | { |
4659 | struct address_space *mapping = inode->i_mapping; | |
0adb25d2 | 4660 | struct page *page; |
afddba49 | 4661 | void *fsdata; |
beb497ab | 4662 | int err; |
c718a975 | 4663 | unsigned int flags = 0; |
54566b2c NP |
4664 | if (nofs) |
4665 | flags |= AOP_FLAG_NOFS; | |
1da177e4 | 4666 | |
7e53cac4 | 4667 | retry: |
afddba49 | 4668 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
54566b2c | 4669 | flags, &page, &fsdata); |
1da177e4 | 4670 | if (err) |
afddba49 NP |
4671 | goto fail; |
4672 | ||
21fc61c7 | 4673 | memcpy(page_address(page), symname, len-1); |
afddba49 NP |
4674 | |
4675 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, | |
4676 | page, fsdata); | |
1da177e4 LT |
4677 | if (err < 0) |
4678 | goto fail; | |
afddba49 NP |
4679 | if (err < len-1) |
4680 | goto retry; | |
4681 | ||
1da177e4 LT |
4682 | mark_inode_dirty(inode); |
4683 | return 0; | |
1da177e4 LT |
4684 | fail: |
4685 | return err; | |
4686 | } | |
4d359507 | 4687 | EXPORT_SYMBOL(__page_symlink); |
1da177e4 | 4688 | |
0adb25d2 KK |
4689 | int page_symlink(struct inode *inode, const char *symname, int len) |
4690 | { | |
4691 | return __page_symlink(inode, symname, len, | |
c62d2555 | 4692 | !mapping_gfp_constraint(inode->i_mapping, __GFP_FS)); |
0adb25d2 | 4693 | } |
4d359507 | 4694 | EXPORT_SYMBOL(page_symlink); |
0adb25d2 | 4695 | |
92e1d5be | 4696 | const struct inode_operations page_symlink_inode_operations = { |
6b255391 | 4697 | .get_link = page_get_link, |
1da177e4 | 4698 | }; |
1da177e4 | 4699 | EXPORT_SYMBOL(page_symlink_inode_operations); |