]>
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 | ||
7521f22b | 1690 | static const char *follow_dotdot_rcu(struct nameidata *nd) |
957dd41d AV |
1691 | { |
1692 | struct dentry *parent = NULL; | |
1693 | struct inode *inode = nd->inode; | |
1694 | unsigned seq; | |
1695 | ||
1696 | while (1) { | |
1697 | if (path_equal(&nd->path, &nd->root)) | |
1698 | break; | |
1699 | if (nd->path.dentry != nd->path.mnt->mnt_root) { | |
1700 | struct dentry *old = nd->path.dentry; | |
1701 | ||
1702 | parent = old->d_parent; | |
1703 | inode = parent->d_inode; | |
1704 | seq = read_seqcount_begin(&parent->d_seq); | |
1705 | if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) | |
7521f22b | 1706 | return ERR_PTR(-ECHILD); |
957dd41d | 1707 | if (unlikely(!path_connected(nd->path.mnt, parent))) |
7521f22b | 1708 | return ERR_PTR(-ECHILD); |
957dd41d AV |
1709 | break; |
1710 | } else { | |
1711 | struct mount *mnt = real_mount(nd->path.mnt); | |
1712 | struct mount *mparent = mnt->mnt_parent; | |
1713 | struct dentry *mountpoint = mnt->mnt_mountpoint; | |
1714 | struct inode *inode2 = mountpoint->d_inode; | |
1715 | unsigned seq = read_seqcount_begin(&mountpoint->d_seq); | |
1716 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) | |
7521f22b | 1717 | return ERR_PTR(-ECHILD); |
957dd41d AV |
1718 | if (&mparent->mnt == nd->path.mnt) |
1719 | break; | |
1720 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) | |
7521f22b | 1721 | return ERR_PTR(-ECHILD); |
957dd41d AV |
1722 | /* we know that mountpoint was pinned */ |
1723 | nd->path.dentry = mountpoint; | |
1724 | nd->path.mnt = &mparent->mnt; | |
6dfd9fe5 | 1725 | inode = nd->inode = inode2; |
957dd41d AV |
1726 | nd->seq = seq; |
1727 | } | |
1728 | } | |
1729 | if (unlikely(!parent)) { | |
1730 | if (unlikely(nd->flags & LOOKUP_BENEATH)) | |
7521f22b | 1731 | return ERR_PTR(-ECHILD); |
6dfd9fe5 AV |
1732 | return step_into(nd, WALK_NOFOLLOW, |
1733 | nd->path.dentry, nd->inode, nd->seq); | |
957dd41d | 1734 | } else { |
6dfd9fe5 | 1735 | return step_into(nd, WALK_NOFOLLOW, parent, inode, seq); |
957dd41d | 1736 | } |
957dd41d AV |
1737 | } |
1738 | ||
7521f22b | 1739 | static const char *follow_dotdot(struct nameidata *nd) |
957dd41d AV |
1740 | { |
1741 | struct dentry *parent = NULL; | |
1742 | while (1) { | |
1743 | if (path_equal(&nd->path, &nd->root)) | |
1744 | break; | |
1745 | if (nd->path.dentry != nd->path.mnt->mnt_root) { | |
1746 | /* rare case of legitimate dget_parent()... */ | |
1747 | parent = dget_parent(nd->path.dentry); | |
1748 | if (unlikely(!path_connected(nd->path.mnt, parent))) { | |
1749 | dput(parent); | |
7521f22b | 1750 | return ERR_PTR(-ENOENT); |
957dd41d AV |
1751 | } |
1752 | break; | |
1753 | } | |
1754 | if (!follow_up(&nd->path)) | |
1755 | break; | |
1756 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) | |
7521f22b | 1757 | return ERR_PTR(-EXDEV); |
957dd41d AV |
1758 | } |
1759 | if (unlikely(!parent)) { | |
1760 | if (unlikely(nd->flags & LOOKUP_BENEATH)) | |
7521f22b | 1761 | return ERR_PTR(-EXDEV); |
6dfd9fe5 AV |
1762 | return step_into(nd, WALK_NOFOLLOW, |
1763 | dget(nd->path.dentry), nd->inode, nd->seq); | |
957dd41d | 1764 | } else { |
6dfd9fe5 | 1765 | return step_into(nd, WALK_NOFOLLOW, parent, parent->d_inode, 0); |
957dd41d | 1766 | } |
957dd41d AV |
1767 | } |
1768 | ||
7521f22b | 1769 | static const char *handle_dots(struct nameidata *nd, int type) |
957dd41d AV |
1770 | { |
1771 | if (type == LAST_DOTDOT) { | |
7521f22b | 1772 | const char *error = NULL; |
957dd41d AV |
1773 | |
1774 | if (!nd->root.mnt) { | |
7521f22b | 1775 | error = ERR_PTR(set_root(nd)); |
957dd41d AV |
1776 | if (error) |
1777 | return error; | |
1778 | } | |
1779 | if (nd->flags & LOOKUP_RCU) | |
1780 | error = follow_dotdot_rcu(nd); | |
1781 | else | |
1782 | error = follow_dotdot(nd); | |
1783 | if (error) | |
1784 | return error; | |
1785 | ||
1786 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { | |
1787 | /* | |
1788 | * If there was a racing rename or mount along our | |
1789 | * path, then we can't be sure that ".." hasn't jumped | |
1790 | * above nd->root (and so userspace should retry or use | |
1791 | * some fallback). | |
1792 | */ | |
1793 | smp_rmb(); | |
1794 | if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq))) | |
7521f22b | 1795 | return ERR_PTR(-EAGAIN); |
957dd41d | 1796 | if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq))) |
7521f22b | 1797 | return ERR_PTR(-EAGAIN); |
957dd41d AV |
1798 | } |
1799 | } | |
7521f22b | 1800 | return NULL; |
957dd41d AV |
1801 | } |
1802 | ||
92d27016 | 1803 | static const char *walk_component(struct nameidata *nd, int flags) |
ce57dfc1 | 1804 | { |
db3c9ade | 1805 | struct dentry *dentry; |
ce57dfc1 | 1806 | struct inode *inode; |
254cf582 | 1807 | unsigned seq; |
ce57dfc1 AV |
1808 | /* |
1809 | * "." and ".." are special - ".." especially so because it has | |
1810 | * to be able to know about the current root directory and | |
1811 | * parent relationships. | |
1812 | */ | |
4693a547 | 1813 | if (unlikely(nd->last_type != LAST_NORM)) { |
1c4ff1a8 | 1814 | if (!(flags & WALK_MORE) && nd->depth) |
4693a547 | 1815 | put_link(nd); |
7521f22b | 1816 | return handle_dots(nd, nd->last_type); |
4693a547 | 1817 | } |
20e34357 AV |
1818 | dentry = lookup_fast(nd, &inode, &seq); |
1819 | if (IS_ERR(dentry)) | |
92d27016 | 1820 | return ERR_CAST(dentry); |
20e34357 | 1821 | if (unlikely(!dentry)) { |
db3c9ade AV |
1822 | dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); |
1823 | if (IS_ERR(dentry)) | |
92d27016 | 1824 | return ERR_CAST(dentry); |
ce57dfc1 | 1825 | } |
56676ec3 AV |
1826 | if (!(flags & WALK_MORE) && nd->depth) |
1827 | put_link(nd); | |
b0417d2c | 1828 | return step_into(nd, flags, dentry, inode, seq); |
ce57dfc1 AV |
1829 | } |
1830 | ||
bfcfaa77 LT |
1831 | /* |
1832 | * We can do the critical dentry name comparison and hashing | |
1833 | * operations one word at a time, but we are limited to: | |
1834 | * | |
1835 | * - Architectures with fast unaligned word accesses. We could | |
1836 | * do a "get_unaligned()" if this helps and is sufficiently | |
1837 | * fast. | |
1838 | * | |
bfcfaa77 LT |
1839 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
1840 | * do not trap on the (extremely unlikely) case of a page | |
1841 | * crossing operation. | |
1842 | * | |
1843 | * - Furthermore, we need an efficient 64-bit compile for the | |
1844 | * 64-bit case in order to generate the "number of bytes in | |
1845 | * the final mask". Again, that could be replaced with a | |
1846 | * efficient population count instruction or similar. | |
1847 | */ | |
1848 | #ifdef CONFIG_DCACHE_WORD_ACCESS | |
1849 | ||
f68e556e | 1850 | #include <asm/word-at-a-time.h> |
bfcfaa77 | 1851 | |
468a9428 | 1852 | #ifdef HASH_MIX |
bfcfaa77 | 1853 | |
468a9428 | 1854 | /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ |
bfcfaa77 | 1855 | |
468a9428 | 1856 | #elif defined(CONFIG_64BIT) |
0fed3ac8 | 1857 | /* |
2a18da7a GS |
1858 | * Register pressure in the mixing function is an issue, particularly |
1859 | * on 32-bit x86, but almost any function requires one state value and | |
1860 | * one temporary. Instead, use a function designed for two state values | |
1861 | * and no temporaries. | |
1862 | * | |
1863 | * This function cannot create a collision in only two iterations, so | |
1864 | * we have two iterations to achieve avalanche. In those two iterations, | |
1865 | * we have six layers of mixing, which is enough to spread one bit's | |
1866 | * influence out to 2^6 = 64 state bits. | |
1867 | * | |
1868 | * Rotate constants are scored by considering either 64 one-bit input | |
1869 | * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the | |
1870 | * probability of that delta causing a change to each of the 128 output | |
1871 | * bits, using a sample of random initial states. | |
1872 | * | |
1873 | * The Shannon entropy of the computed probabilities is then summed | |
1874 | * to produce a score. Ideally, any input change has a 50% chance of | |
1875 | * toggling any given output bit. | |
1876 | * | |
1877 | * Mixing scores (in bits) for (12,45): | |
1878 | * Input delta: 1-bit 2-bit | |
1879 | * 1 round: 713.3 42542.6 | |
1880 | * 2 rounds: 2753.7 140389.8 | |
1881 | * 3 rounds: 5954.1 233458.2 | |
1882 | * 4 rounds: 7862.6 256672.2 | |
1883 | * Perfect: 8192 258048 | |
1884 | * (64*128) (64*63/2 * 128) | |
0fed3ac8 | 1885 | */ |
2a18da7a GS |
1886 | #define HASH_MIX(x, y, a) \ |
1887 | ( x ^= (a), \ | |
1888 | y ^= x, x = rol64(x,12),\ | |
1889 | x += y, y = rol64(y,45),\ | |
1890 | y *= 9 ) | |
bfcfaa77 | 1891 | |
0fed3ac8 | 1892 | /* |
2a18da7a GS |
1893 | * Fold two longs into one 32-bit hash value. This must be fast, but |
1894 | * latency isn't quite as critical, as there is a fair bit of additional | |
1895 | * work done before the hash value is used. | |
0fed3ac8 | 1896 | */ |
2a18da7a | 1897 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
0fed3ac8 | 1898 | { |
2a18da7a GS |
1899 | y ^= x * GOLDEN_RATIO_64; |
1900 | y *= GOLDEN_RATIO_64; | |
1901 | return y >> 32; | |
0fed3ac8 GS |
1902 | } |
1903 | ||
bfcfaa77 LT |
1904 | #else /* 32-bit case */ |
1905 | ||
2a18da7a GS |
1906 | /* |
1907 | * Mixing scores (in bits) for (7,20): | |
1908 | * Input delta: 1-bit 2-bit | |
1909 | * 1 round: 330.3 9201.6 | |
1910 | * 2 rounds: 1246.4 25475.4 | |
1911 | * 3 rounds: 1907.1 31295.1 | |
1912 | * 4 rounds: 2042.3 31718.6 | |
1913 | * Perfect: 2048 31744 | |
1914 | * (32*64) (32*31/2 * 64) | |
1915 | */ | |
1916 | #define HASH_MIX(x, y, a) \ | |
1917 | ( x ^= (a), \ | |
1918 | y ^= x, x = rol32(x, 7),\ | |
1919 | x += y, y = rol32(y,20),\ | |
1920 | y *= 9 ) | |
bfcfaa77 | 1921 | |
2a18da7a | 1922 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
0fed3ac8 | 1923 | { |
2a18da7a GS |
1924 | /* Use arch-optimized multiply if one exists */ |
1925 | return __hash_32(y ^ __hash_32(x)); | |
0fed3ac8 GS |
1926 | } |
1927 | ||
bfcfaa77 LT |
1928 | #endif |
1929 | ||
2a18da7a GS |
1930 | /* |
1931 | * Return the hash of a string of known length. This is carfully | |
1932 | * designed to match hash_name(), which is the more critical function. | |
1933 | * In particular, we must end by hashing a final word containing 0..7 | |
1934 | * payload bytes, to match the way that hash_name() iterates until it | |
1935 | * finds the delimiter after the name. | |
1936 | */ | |
8387ff25 | 1937 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
bfcfaa77 | 1938 | { |
8387ff25 | 1939 | unsigned long a, x = 0, y = (unsigned long)salt; |
bfcfaa77 LT |
1940 | |
1941 | for (;;) { | |
fcfd2fbf GS |
1942 | if (!len) |
1943 | goto done; | |
e419b4cc | 1944 | a = load_unaligned_zeropad(name); |
bfcfaa77 LT |
1945 | if (len < sizeof(unsigned long)) |
1946 | break; | |
2a18da7a | 1947 | HASH_MIX(x, y, a); |
bfcfaa77 LT |
1948 | name += sizeof(unsigned long); |
1949 | len -= sizeof(unsigned long); | |
bfcfaa77 | 1950 | } |
2a18da7a | 1951 | x ^= a & bytemask_from_count(len); |
bfcfaa77 | 1952 | done: |
2a18da7a | 1953 | return fold_hash(x, y); |
bfcfaa77 LT |
1954 | } |
1955 | EXPORT_SYMBOL(full_name_hash); | |
1956 | ||
fcfd2fbf | 1957 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
8387ff25 | 1958 | u64 hashlen_string(const void *salt, const char *name) |
fcfd2fbf | 1959 | { |
8387ff25 LT |
1960 | unsigned long a = 0, x = 0, y = (unsigned long)salt; |
1961 | unsigned long adata, mask, len; | |
fcfd2fbf GS |
1962 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
1963 | ||
8387ff25 LT |
1964 | len = 0; |
1965 | goto inside; | |
1966 | ||
fcfd2fbf | 1967 | do { |
2a18da7a | 1968 | HASH_MIX(x, y, a); |
fcfd2fbf | 1969 | len += sizeof(unsigned long); |
8387ff25 | 1970 | inside: |
fcfd2fbf GS |
1971 | a = load_unaligned_zeropad(name+len); |
1972 | } while (!has_zero(a, &adata, &constants)); | |
1973 | ||
1974 | adata = prep_zero_mask(a, adata, &constants); | |
1975 | mask = create_zero_mask(adata); | |
2a18da7a | 1976 | x ^= a & zero_bytemask(mask); |
fcfd2fbf | 1977 | |
2a18da7a | 1978 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
fcfd2fbf GS |
1979 | } |
1980 | EXPORT_SYMBOL(hashlen_string); | |
1981 | ||
bfcfaa77 LT |
1982 | /* |
1983 | * Calculate the length and hash of the path component, and | |
d6bb3e90 | 1984 | * return the "hash_len" as the result. |
bfcfaa77 | 1985 | */ |
8387ff25 | 1986 | static inline u64 hash_name(const void *salt, const char *name) |
bfcfaa77 | 1987 | { |
8387ff25 LT |
1988 | unsigned long a = 0, b, x = 0, y = (unsigned long)salt; |
1989 | unsigned long adata, bdata, mask, len; | |
36126f8f | 1990 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
bfcfaa77 | 1991 | |
8387ff25 LT |
1992 | len = 0; |
1993 | goto inside; | |
1994 | ||
bfcfaa77 | 1995 | do { |
2a18da7a | 1996 | HASH_MIX(x, y, a); |
bfcfaa77 | 1997 | len += sizeof(unsigned long); |
8387ff25 | 1998 | inside: |
e419b4cc | 1999 | a = load_unaligned_zeropad(name+len); |
36126f8f LT |
2000 | b = a ^ REPEAT_BYTE('/'); |
2001 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); | |
2002 | ||
2003 | adata = prep_zero_mask(a, adata, &constants); | |
2004 | bdata = prep_zero_mask(b, bdata, &constants); | |
36126f8f | 2005 | mask = create_zero_mask(adata | bdata); |
2a18da7a | 2006 | x ^= a & zero_bytemask(mask); |
36126f8f | 2007 | |
2a18da7a | 2008 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
bfcfaa77 LT |
2009 | } |
2010 | ||
2a18da7a | 2011 | #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ |
bfcfaa77 | 2012 | |
fcfd2fbf | 2013 | /* Return the hash of a string of known length */ |
8387ff25 | 2014 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
0145acc2 | 2015 | { |
8387ff25 | 2016 | unsigned long hash = init_name_hash(salt); |
0145acc2 | 2017 | while (len--) |
fcfd2fbf | 2018 | hash = partial_name_hash((unsigned char)*name++, hash); |
0145acc2 LT |
2019 | return end_name_hash(hash); |
2020 | } | |
ae942ae7 | 2021 | EXPORT_SYMBOL(full_name_hash); |
0145acc2 | 2022 | |
fcfd2fbf | 2023 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
8387ff25 | 2024 | u64 hashlen_string(const void *salt, const char *name) |
fcfd2fbf | 2025 | { |
8387ff25 | 2026 | unsigned long hash = init_name_hash(salt); |
fcfd2fbf GS |
2027 | unsigned long len = 0, c; |
2028 | ||
2029 | c = (unsigned char)*name; | |
e0ab7af9 | 2030 | while (c) { |
fcfd2fbf GS |
2031 | len++; |
2032 | hash = partial_name_hash(c, hash); | |
2033 | c = (unsigned char)name[len]; | |
e0ab7af9 | 2034 | } |
fcfd2fbf GS |
2035 | return hashlen_create(end_name_hash(hash), len); |
2036 | } | |
f2a031b6 | 2037 | EXPORT_SYMBOL(hashlen_string); |
fcfd2fbf | 2038 | |
200e9ef7 LT |
2039 | /* |
2040 | * We know there's a real path component here of at least | |
2041 | * one character. | |
2042 | */ | |
8387ff25 | 2043 | static inline u64 hash_name(const void *salt, const char *name) |
200e9ef7 | 2044 | { |
8387ff25 | 2045 | unsigned long hash = init_name_hash(salt); |
200e9ef7 LT |
2046 | unsigned long len = 0, c; |
2047 | ||
2048 | c = (unsigned char)*name; | |
2049 | do { | |
2050 | len++; | |
2051 | hash = partial_name_hash(c, hash); | |
2052 | c = (unsigned char)name[len]; | |
2053 | } while (c && c != '/'); | |
d6bb3e90 | 2054 | return hashlen_create(end_name_hash(hash), len); |
200e9ef7 LT |
2055 | } |
2056 | ||
bfcfaa77 LT |
2057 | #endif |
2058 | ||
1da177e4 LT |
2059 | /* |
2060 | * Name resolution. | |
ea3834d9 PM |
2061 | * This is the basic name resolution function, turning a pathname into |
2062 | * the final dentry. We expect 'base' to be positive and a directory. | |
1da177e4 | 2063 | * |
ea3834d9 PM |
2064 | * Returns 0 and nd will have valid dentry and mnt on success. |
2065 | * Returns error and drops reference to input namei data on failure. | |
1da177e4 | 2066 | */ |
6de88d72 | 2067 | static int link_path_walk(const char *name, struct nameidata *nd) |
1da177e4 | 2068 | { |
d8d4611a | 2069 | int depth = 0; // depth <= nd->depth |
1da177e4 | 2070 | int err; |
32cd7468 | 2071 | |
b4c03536 | 2072 | nd->last_type = LAST_ROOT; |
c108837e | 2073 | nd->flags |= LOOKUP_PARENT; |
9b5858e9 AV |
2074 | if (IS_ERR(name)) |
2075 | return PTR_ERR(name); | |
1da177e4 LT |
2076 | while (*name=='/') |
2077 | name++; | |
2078 | if (!*name) | |
9e18f10a | 2079 | return 0; |
1da177e4 | 2080 | |
1da177e4 LT |
2081 | /* At this point we know we have a real path component. */ |
2082 | for(;;) { | |
92d27016 | 2083 | const char *link; |
d6bb3e90 | 2084 | u64 hash_len; |
fe479a58 | 2085 | int type; |
1da177e4 | 2086 | |
52094c8a | 2087 | err = may_lookup(nd); |
2a18da7a | 2088 | if (err) |
3595e234 | 2089 | return err; |
1da177e4 | 2090 | |
8387ff25 | 2091 | hash_len = hash_name(nd->path.dentry, name); |
1da177e4 | 2092 | |
fe479a58 | 2093 | type = LAST_NORM; |
d6bb3e90 | 2094 | if (name[0] == '.') switch (hashlen_len(hash_len)) { |
fe479a58 | 2095 | case 2: |
200e9ef7 | 2096 | if (name[1] == '.') { |
fe479a58 | 2097 | type = LAST_DOTDOT; |
16c2cd71 AV |
2098 | nd->flags |= LOOKUP_JUMPED; |
2099 | } | |
fe479a58 AV |
2100 | break; |
2101 | case 1: | |
2102 | type = LAST_DOT; | |
2103 | } | |
5a202bcd AV |
2104 | if (likely(type == LAST_NORM)) { |
2105 | struct dentry *parent = nd->path.dentry; | |
16c2cd71 | 2106 | nd->flags &= ~LOOKUP_JUMPED; |
5a202bcd | 2107 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
a060dc50 | 2108 | struct qstr this = { { .hash_len = hash_len }, .name = name }; |
da53be12 | 2109 | err = parent->d_op->d_hash(parent, &this); |
5a202bcd | 2110 | if (err < 0) |
3595e234 | 2111 | return err; |
d6bb3e90 LT |
2112 | hash_len = this.hash_len; |
2113 | name = this.name; | |
5a202bcd AV |
2114 | } |
2115 | } | |
fe479a58 | 2116 | |
d6bb3e90 LT |
2117 | nd->last.hash_len = hash_len; |
2118 | nd->last.name = name; | |
5f4a6a69 AV |
2119 | nd->last_type = type; |
2120 | ||
d6bb3e90 LT |
2121 | name += hashlen_len(hash_len); |
2122 | if (!*name) | |
bdf6cbf1 | 2123 | goto OK; |
200e9ef7 LT |
2124 | /* |
2125 | * If it wasn't NUL, we know it was '/'. Skip that | |
2126 | * slash, and continue until no more slashes. | |
2127 | */ | |
2128 | do { | |
d6bb3e90 LT |
2129 | name++; |
2130 | } while (unlikely(*name == '/')); | |
8620c238 AV |
2131 | if (unlikely(!*name)) { |
2132 | OK: | |
d8d4611a | 2133 | /* pathname or trailing symlink, done */ |
c108837e AV |
2134 | if (!depth) { |
2135 | nd->flags &= ~LOOKUP_PARENT; | |
8620c238 | 2136 | return 0; |
c108837e | 2137 | } |
8620c238 | 2138 | /* last component of nested symlink */ |
d8d4611a | 2139 | name = nd->stack[--depth].name; |
8c4efe22 | 2140 | link = walk_component(nd, 0); |
1c4ff1a8 AV |
2141 | } else { |
2142 | /* not the last component */ | |
8c4efe22 | 2143 | link = walk_component(nd, WALK_MORE); |
8620c238 | 2144 | } |
92d27016 AV |
2145 | if (unlikely(link)) { |
2146 | if (IS_ERR(link)) | |
2147 | return PTR_ERR(link); | |
2148 | /* a symlink to follow */ | |
d8d4611a | 2149 | nd->stack[depth++].name = name; |
92d27016 AV |
2150 | name = link; |
2151 | continue; | |
31e6b01f | 2152 | } |
97242f99 AV |
2153 | if (unlikely(!d_can_lookup(nd->path.dentry))) { |
2154 | if (nd->flags & LOOKUP_RCU) { | |
4675ac39 | 2155 | if (unlazy_walk(nd)) |
97242f99 AV |
2156 | return -ECHILD; |
2157 | } | |
3595e234 | 2158 | return -ENOTDIR; |
97242f99 | 2159 | } |
1da177e4 | 2160 | } |
1da177e4 LT |
2161 | } |
2162 | ||
edc2b1da | 2163 | /* must be paired with terminate_walk() */ |
c8a53ee5 | 2164 | static const char *path_init(struct nameidata *nd, unsigned flags) |
31e6b01f | 2165 | { |
740a1678 | 2166 | int error; |
c8a53ee5 | 2167 | const char *s = nd->name->name; |
31e6b01f | 2168 | |
c0eb027e LT |
2169 | if (!*s) |
2170 | flags &= ~LOOKUP_RCU; | |
edc2b1da AV |
2171 | if (flags & LOOKUP_RCU) |
2172 | rcu_read_lock(); | |
c0eb027e | 2173 | |
c108837e | 2174 | nd->flags = flags | LOOKUP_JUMPED; |
31e6b01f | 2175 | nd->depth = 0; |
ab87f9a5 AS |
2176 | |
2177 | nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); | |
2178 | nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); | |
2179 | smp_rmb(); | |
2180 | ||
5b6ca027 | 2181 | if (flags & LOOKUP_ROOT) { |
b18825a7 DH |
2182 | struct dentry *root = nd->root.dentry; |
2183 | struct inode *inode = root->d_inode; | |
93893862 AV |
2184 | if (*s && unlikely(!d_can_lookup(root))) |
2185 | return ERR_PTR(-ENOTDIR); | |
5b6ca027 AV |
2186 | nd->path = nd->root; |
2187 | nd->inode = inode; | |
2188 | if (flags & LOOKUP_RCU) { | |
ab87f9a5 | 2189 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
8f47a016 | 2190 | nd->root_seq = nd->seq; |
5b6ca027 AV |
2191 | } else { |
2192 | path_get(&nd->path); | |
2193 | } | |
368ee9ba | 2194 | return s; |
5b6ca027 AV |
2195 | } |
2196 | ||
31e6b01f | 2197 | nd->root.mnt = NULL; |
248fb5b9 AV |
2198 | nd->path.mnt = NULL; |
2199 | nd->path.dentry = NULL; | |
31e6b01f | 2200 | |
8db52c7e AS |
2201 | /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ |
2202 | if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { | |
740a1678 AS |
2203 | error = nd_jump_root(nd); |
2204 | if (unlikely(error)) | |
2205 | return ERR_PTR(error); | |
2206 | return s; | |
8db52c7e AS |
2207 | } |
2208 | ||
2209 | /* Relative pathname -- get the starting-point it is relative to. */ | |
2210 | if (nd->dfd == AT_FDCWD) { | |
e41f7d4e AV |
2211 | if (flags & LOOKUP_RCU) { |
2212 | struct fs_struct *fs = current->fs; | |
2213 | unsigned seq; | |
31e6b01f | 2214 | |
e41f7d4e AV |
2215 | do { |
2216 | seq = read_seqcount_begin(&fs->seq); | |
2217 | nd->path = fs->pwd; | |
ef55d917 | 2218 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e AV |
2219 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
2220 | } while (read_seqcount_retry(&fs->seq, seq)); | |
2221 | } else { | |
2222 | get_fs_pwd(current->fs, &nd->path); | |
ef55d917 | 2223 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e | 2224 | } |
31e6b01f | 2225 | } else { |
582aa64a | 2226 | /* Caller must check execute permissions on the starting path component */ |
c8a53ee5 | 2227 | struct fd f = fdget_raw(nd->dfd); |
31e6b01f NP |
2228 | struct dentry *dentry; |
2229 | ||
2903ff01 | 2230 | if (!f.file) |
368ee9ba | 2231 | return ERR_PTR(-EBADF); |
31e6b01f | 2232 | |
2903ff01 | 2233 | dentry = f.file->f_path.dentry; |
31e6b01f | 2234 | |
edc2b1da AV |
2235 | if (*s && unlikely(!d_can_lookup(dentry))) { |
2236 | fdput(f); | |
2237 | return ERR_PTR(-ENOTDIR); | |
f52e0c11 | 2238 | } |
31e6b01f | 2239 | |
2903ff01 | 2240 | nd->path = f.file->f_path; |
e41f7d4e | 2241 | if (flags & LOOKUP_RCU) { |
34a26b99 AV |
2242 | nd->inode = nd->path.dentry->d_inode; |
2243 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); | |
e41f7d4e | 2244 | } else { |
2903ff01 | 2245 | path_get(&nd->path); |
34a26b99 | 2246 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e | 2247 | } |
34a26b99 | 2248 | fdput(f); |
31e6b01f | 2249 | } |
8db52c7e | 2250 | |
adb21d2b AS |
2251 | /* For scoped-lookups we need to set the root to the dirfd as well. */ |
2252 | if (flags & LOOKUP_IS_SCOPED) { | |
2253 | nd->root = nd->path; | |
2254 | if (flags & LOOKUP_RCU) { | |
2255 | nd->root_seq = nd->seq; | |
2256 | } else { | |
2257 | path_get(&nd->root); | |
2258 | nd->flags |= LOOKUP_ROOT_GRABBED; | |
2259 | } | |
2260 | } | |
2261 | return s; | |
9b4a9b14 AV |
2262 | } |
2263 | ||
1ccac622 | 2264 | static inline const char *lookup_last(struct nameidata *nd) |
bd92d7fe AV |
2265 | { |
2266 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) | |
2267 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
2268 | ||
c108837e | 2269 | return walk_component(nd, WALK_TRAILING); |
bd92d7fe AV |
2270 | } |
2271 | ||
4f757f3c AV |
2272 | static int handle_lookup_down(struct nameidata *nd) |
2273 | { | |
c153007b | 2274 | if (!(nd->flags & LOOKUP_RCU)) |
db3c9ade | 2275 | dget(nd->path.dentry); |
b0417d2c AV |
2276 | return PTR_ERR(step_into(nd, WALK_NOFOLLOW, |
2277 | nd->path.dentry, nd->inode, nd->seq)); | |
4f757f3c AV |
2278 | } |
2279 | ||
9b4a9b14 | 2280 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
c8a53ee5 | 2281 | static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) |
9b4a9b14 | 2282 | { |
c8a53ee5 | 2283 | const char *s = path_init(nd, flags); |
bd92d7fe | 2284 | int err; |
31e6b01f | 2285 | |
9b5858e9 | 2286 | if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { |
4f757f3c | 2287 | err = handle_lookup_down(nd); |
5f336e72 AV |
2288 | if (unlikely(err < 0)) |
2289 | s = ERR_PTR(err); | |
4f757f3c AV |
2290 | } |
2291 | ||
1ccac622 AV |
2292 | while (!(err = link_path_walk(s, nd)) && |
2293 | (s = lookup_last(nd)) != NULL) | |
2294 | ; | |
9f1fafee AV |
2295 | if (!err) |
2296 | err = complete_walk(nd); | |
bd92d7fe | 2297 | |
deb106c6 AV |
2298 | if (!err && nd->flags & LOOKUP_DIRECTORY) |
2299 | if (!d_can_lookup(nd->path.dentry)) | |
bd23a539 | 2300 | err = -ENOTDIR; |
161aff1d AV |
2301 | if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { |
2302 | err = handle_lookup_down(nd); | |
2303 | nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please... | |
2304 | } | |
625b6d10 AV |
2305 | if (!err) { |
2306 | *path = nd->path; | |
2307 | nd->path.mnt = NULL; | |
2308 | nd->path.dentry = NULL; | |
2309 | } | |
2310 | terminate_walk(nd); | |
bd92d7fe | 2311 | return err; |
ee0827cd | 2312 | } |
31e6b01f | 2313 | |
31d921c7 DH |
2314 | int filename_lookup(int dfd, struct filename *name, unsigned flags, |
2315 | struct path *path, struct path *root) | |
ee0827cd | 2316 | { |
894bc8c4 | 2317 | int retval; |
9883d185 | 2318 | struct nameidata nd; |
abc9f5be AV |
2319 | if (IS_ERR(name)) |
2320 | return PTR_ERR(name); | |
9ad1aaa6 AV |
2321 | if (unlikely(root)) { |
2322 | nd.root = *root; | |
2323 | flags |= LOOKUP_ROOT; | |
2324 | } | |
9883d185 | 2325 | set_nameidata(&nd, dfd, name); |
c8a53ee5 | 2326 | retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); |
ee0827cd | 2327 | if (unlikely(retval == -ECHILD)) |
c8a53ee5 | 2328 | retval = path_lookupat(&nd, flags, path); |
ee0827cd | 2329 | if (unlikely(retval == -ESTALE)) |
c8a53ee5 | 2330 | retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); |
31e6b01f | 2331 | |
f78570dd | 2332 | if (likely(!retval)) |
161aff1d AV |
2333 | audit_inode(name, path->dentry, |
2334 | flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); | |
9883d185 | 2335 | restore_nameidata(); |
e4bd1c1a | 2336 | putname(name); |
170aa3d0 | 2337 | return retval; |
1da177e4 LT |
2338 | } |
2339 | ||
8bcb77fa | 2340 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
c8a53ee5 | 2341 | static int path_parentat(struct nameidata *nd, unsigned flags, |
391172c4 | 2342 | struct path *parent) |
8bcb77fa | 2343 | { |
c8a53ee5 | 2344 | const char *s = path_init(nd, flags); |
9b5858e9 | 2345 | int err = link_path_walk(s, nd); |
8bcb77fa AV |
2346 | if (!err) |
2347 | err = complete_walk(nd); | |
391172c4 AV |
2348 | if (!err) { |
2349 | *parent = nd->path; | |
2350 | nd->path.mnt = NULL; | |
2351 | nd->path.dentry = NULL; | |
2352 | } | |
2353 | terminate_walk(nd); | |
8bcb77fa AV |
2354 | return err; |
2355 | } | |
2356 | ||
5c31b6ce | 2357 | static struct filename *filename_parentat(int dfd, struct filename *name, |
391172c4 AV |
2358 | unsigned int flags, struct path *parent, |
2359 | struct qstr *last, int *type) | |
8bcb77fa AV |
2360 | { |
2361 | int retval; | |
9883d185 | 2362 | struct nameidata nd; |
8bcb77fa | 2363 | |
5c31b6ce AV |
2364 | if (IS_ERR(name)) |
2365 | return name; | |
9883d185 | 2366 | set_nameidata(&nd, dfd, name); |
c8a53ee5 | 2367 | retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); |
8bcb77fa | 2368 | if (unlikely(retval == -ECHILD)) |
c8a53ee5 | 2369 | retval = path_parentat(&nd, flags, parent); |
8bcb77fa | 2370 | if (unlikely(retval == -ESTALE)) |
c8a53ee5 | 2371 | retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); |
391172c4 AV |
2372 | if (likely(!retval)) { |
2373 | *last = nd.last; | |
2374 | *type = nd.last_type; | |
c9b07eab | 2375 | audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); |
5c31b6ce AV |
2376 | } else { |
2377 | putname(name); | |
2378 | name = ERR_PTR(retval); | |
391172c4 | 2379 | } |
9883d185 | 2380 | restore_nameidata(); |
5c31b6ce | 2381 | return name; |
8bcb77fa AV |
2382 | } |
2383 | ||
79714f72 AV |
2384 | /* does lookup, returns the object with parent locked */ |
2385 | struct dentry *kern_path_locked(const char *name, struct path *path) | |
5590ff0d | 2386 | { |
5c31b6ce AV |
2387 | struct filename *filename; |
2388 | struct dentry *d; | |
391172c4 AV |
2389 | struct qstr last; |
2390 | int type; | |
51689104 | 2391 | |
5c31b6ce AV |
2392 | filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path, |
2393 | &last, &type); | |
51689104 PM |
2394 | if (IS_ERR(filename)) |
2395 | return ERR_CAST(filename); | |
5c31b6ce | 2396 | if (unlikely(type != LAST_NORM)) { |
391172c4 | 2397 | path_put(path); |
5c31b6ce AV |
2398 | putname(filename); |
2399 | return ERR_PTR(-EINVAL); | |
79714f72 | 2400 | } |
5955102c | 2401 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
391172c4 | 2402 | d = __lookup_hash(&last, path->dentry, 0); |
79714f72 | 2403 | if (IS_ERR(d)) { |
5955102c | 2404 | inode_unlock(path->dentry->d_inode); |
391172c4 | 2405 | path_put(path); |
79714f72 | 2406 | } |
51689104 | 2407 | putname(filename); |
79714f72 | 2408 | return d; |
5590ff0d UD |
2409 | } |
2410 | ||
d1811465 AV |
2411 | int kern_path(const char *name, unsigned int flags, struct path *path) |
2412 | { | |
abc9f5be AV |
2413 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
2414 | flags, path, NULL); | |
d1811465 | 2415 | } |
4d359507 | 2416 | EXPORT_SYMBOL(kern_path); |
d1811465 | 2417 | |
16f18200 JJS |
2418 | /** |
2419 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair | |
2420 | * @dentry: pointer to dentry of the base directory | |
2421 | * @mnt: pointer to vfs mount of the base directory | |
2422 | * @name: pointer to file name | |
2423 | * @flags: lookup flags | |
e0a01249 | 2424 | * @path: pointer to struct path to fill |
16f18200 JJS |
2425 | */ |
2426 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, | |
2427 | const char *name, unsigned int flags, | |
e0a01249 | 2428 | struct path *path) |
16f18200 | 2429 | { |
9ad1aaa6 | 2430 | struct path root = {.mnt = mnt, .dentry = dentry}; |
9ad1aaa6 | 2431 | /* the first argument of filename_lookup() is ignored with root */ |
abc9f5be AV |
2432 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
2433 | flags , path, &root); | |
16f18200 | 2434 | } |
4d359507 | 2435 | EXPORT_SYMBOL(vfs_path_lookup); |
16f18200 | 2436 | |
3c95f0dc AV |
2437 | static int lookup_one_len_common(const char *name, struct dentry *base, |
2438 | int len, struct qstr *this) | |
057f6c01 | 2439 | { |
3c95f0dc AV |
2440 | this->name = name; |
2441 | this->len = len; | |
2442 | this->hash = full_name_hash(base, name, len); | |
6a96ba54 | 2443 | if (!len) |
3c95f0dc | 2444 | return -EACCES; |
6a96ba54 | 2445 | |
21d8a15a AV |
2446 | if (unlikely(name[0] == '.')) { |
2447 | if (len < 2 || (len == 2 && name[1] == '.')) | |
3c95f0dc | 2448 | return -EACCES; |
21d8a15a AV |
2449 | } |
2450 | ||
6a96ba54 | 2451 | while (len--) { |
3c95f0dc | 2452 | unsigned int c = *(const unsigned char *)name++; |
6a96ba54 | 2453 | if (c == '/' || c == '\0') |
3c95f0dc | 2454 | return -EACCES; |
6a96ba54 | 2455 | } |
5a202bcd AV |
2456 | /* |
2457 | * See if the low-level filesystem might want | |
2458 | * to use its own hash.. | |
2459 | */ | |
2460 | if (base->d_flags & DCACHE_OP_HASH) { | |
3c95f0dc | 2461 | int err = base->d_op->d_hash(base, this); |
5a202bcd | 2462 | if (err < 0) |
3c95f0dc | 2463 | return err; |
5a202bcd | 2464 | } |
eead1911 | 2465 | |
3c95f0dc AV |
2466 | return inode_permission(base->d_inode, MAY_EXEC); |
2467 | } | |
2468 | ||
0da0b7fd DH |
2469 | /** |
2470 | * try_lookup_one_len - filesystem helper to lookup single pathname component | |
2471 | * @name: pathname component to lookup | |
2472 | * @base: base directory to lookup from | |
2473 | * @len: maximum length @len should be interpreted to | |
2474 | * | |
2475 | * Look up a dentry by name in the dcache, returning NULL if it does not | |
2476 | * currently exist. The function does not try to create a dentry. | |
2477 | * | |
2478 | * Note that this routine is purely a helper for filesystem usage and should | |
2479 | * not be called by generic code. | |
2480 | * | |
2481 | * The caller must hold base->i_mutex. | |
2482 | */ | |
2483 | struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len) | |
2484 | { | |
2485 | struct qstr this; | |
2486 | int err; | |
2487 | ||
2488 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); | |
2489 | ||
2490 | err = lookup_one_len_common(name, base, len, &this); | |
2491 | if (err) | |
2492 | return ERR_PTR(err); | |
2493 | ||
2494 | return lookup_dcache(&this, base, 0); | |
2495 | } | |
2496 | EXPORT_SYMBOL(try_lookup_one_len); | |
2497 | ||
3c95f0dc AV |
2498 | /** |
2499 | * lookup_one_len - filesystem helper to lookup single pathname component | |
2500 | * @name: pathname component to lookup | |
2501 | * @base: base directory to lookup from | |
2502 | * @len: maximum length @len should be interpreted to | |
2503 | * | |
2504 | * Note that this routine is purely a helper for filesystem usage and should | |
2505 | * not be called by generic code. | |
2506 | * | |
2507 | * The caller must hold base->i_mutex. | |
2508 | */ | |
2509 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) | |
2510 | { | |
8613a209 | 2511 | struct dentry *dentry; |
3c95f0dc AV |
2512 | struct qstr this; |
2513 | int err; | |
2514 | ||
2515 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); | |
2516 | ||
2517 | err = lookup_one_len_common(name, base, len, &this); | |
cda309de MS |
2518 | if (err) |
2519 | return ERR_PTR(err); | |
2520 | ||
8613a209 AV |
2521 | dentry = lookup_dcache(&this, base, 0); |
2522 | return dentry ? dentry : __lookup_slow(&this, base, 0); | |
057f6c01 | 2523 | } |
4d359507 | 2524 | EXPORT_SYMBOL(lookup_one_len); |
057f6c01 | 2525 | |
bbddca8e N |
2526 | /** |
2527 | * lookup_one_len_unlocked - filesystem helper to lookup single pathname component | |
2528 | * @name: pathname component to lookup | |
2529 | * @base: base directory to lookup from | |
2530 | * @len: maximum length @len should be interpreted to | |
2531 | * | |
2532 | * Note that this routine is purely a helper for filesystem usage and should | |
2533 | * not be called by generic code. | |
2534 | * | |
2535 | * Unlike lookup_one_len, it should be called without the parent | |
2536 | * i_mutex held, and will take the i_mutex itself if necessary. | |
2537 | */ | |
2538 | struct dentry *lookup_one_len_unlocked(const char *name, | |
2539 | struct dentry *base, int len) | |
2540 | { | |
2541 | struct qstr this; | |
bbddca8e | 2542 | int err; |
20d00ee8 | 2543 | struct dentry *ret; |
bbddca8e | 2544 | |
3c95f0dc | 2545 | err = lookup_one_len_common(name, base, len, &this); |
bbddca8e N |
2546 | if (err) |
2547 | return ERR_PTR(err); | |
2548 | ||
20d00ee8 LT |
2549 | ret = lookup_dcache(&this, base, 0); |
2550 | if (!ret) | |
2551 | ret = lookup_slow(&this, base, 0); | |
2552 | return ret; | |
bbddca8e N |
2553 | } |
2554 | EXPORT_SYMBOL(lookup_one_len_unlocked); | |
2555 | ||
6c2d4798 AV |
2556 | /* |
2557 | * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT) | |
2558 | * on negatives. Returns known positive or ERR_PTR(); that's what | |
2559 | * most of the users want. Note that pinned negative with unlocked parent | |
2560 | * _can_ become positive at any time, so callers of lookup_one_len_unlocked() | |
2561 | * need to be very careful; pinned positives have ->d_inode stable, so | |
2562 | * this one avoids such problems. | |
2563 | */ | |
2564 | struct dentry *lookup_positive_unlocked(const char *name, | |
2565 | struct dentry *base, int len) | |
2566 | { | |
2567 | struct dentry *ret = lookup_one_len_unlocked(name, base, len); | |
2fa6b1e0 | 2568 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { |
6c2d4798 AV |
2569 | dput(ret); |
2570 | ret = ERR_PTR(-ENOENT); | |
2571 | } | |
2572 | return ret; | |
2573 | } | |
2574 | EXPORT_SYMBOL(lookup_positive_unlocked); | |
2575 | ||
eedf265a EB |
2576 | #ifdef CONFIG_UNIX98_PTYS |
2577 | int path_pts(struct path *path) | |
2578 | { | |
2579 | /* Find something mounted on "pts" in the same directory as | |
2580 | * the input path. | |
2581 | */ | |
a6a7eb76 AV |
2582 | struct dentry *parent = dget_parent(path->dentry); |
2583 | struct dentry *child; | |
eedf265a | 2584 | struct qstr this; |
eedf265a | 2585 | |
a6a7eb76 AV |
2586 | if (unlikely(!path_connected(path->mnt, parent))) { |
2587 | dput(parent); | |
63b27720 | 2588 | return -ENOENT; |
a6a7eb76 | 2589 | } |
63b27720 AV |
2590 | dput(path->dentry); |
2591 | path->dentry = parent; | |
eedf265a EB |
2592 | this.name = "pts"; |
2593 | this.len = 3; | |
2594 | child = d_hash_and_lookup(parent, &this); | |
2595 | if (!child) | |
2596 | return -ENOENT; | |
2597 | ||
2598 | path->dentry = child; | |
2599 | dput(parent); | |
2600 | follow_mount(path); | |
2601 | return 0; | |
2602 | } | |
2603 | #endif | |
2604 | ||
1fa1e7f6 AW |
2605 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
2606 | struct path *path, int *empty) | |
1da177e4 | 2607 | { |
abc9f5be AV |
2608 | return filename_lookup(dfd, getname_flags(name, flags, empty), |
2609 | flags, path, NULL); | |
1da177e4 | 2610 | } |
b853a161 | 2611 | EXPORT_SYMBOL(user_path_at_empty); |
1fa1e7f6 | 2612 | |
cbdf35bc | 2613 | int __check_sticky(struct inode *dir, struct inode *inode) |
1da177e4 | 2614 | { |
8e96e3b7 | 2615 | kuid_t fsuid = current_fsuid(); |
da9592ed | 2616 | |
8e96e3b7 | 2617 | if (uid_eq(inode->i_uid, fsuid)) |
1da177e4 | 2618 | return 0; |
8e96e3b7 | 2619 | if (uid_eq(dir->i_uid, fsuid)) |
1da177e4 | 2620 | return 0; |
23adbe12 | 2621 | return !capable_wrt_inode_uidgid(inode, CAP_FOWNER); |
1da177e4 | 2622 | } |
cbdf35bc | 2623 | EXPORT_SYMBOL(__check_sticky); |
1da177e4 LT |
2624 | |
2625 | /* | |
2626 | * Check whether we can remove a link victim from directory dir, check | |
2627 | * whether the type of victim is right. | |
2628 | * 1. We can't do it if dir is read-only (done in permission()) | |
2629 | * 2. We should have write and exec permissions on dir | |
2630 | * 3. We can't remove anything from append-only dir | |
2631 | * 4. We can't do anything with immutable dir (done in permission()) | |
2632 | * 5. If the sticky bit on dir is set we should either | |
2633 | * a. be owner of dir, or | |
2634 | * b. be owner of victim, or | |
2635 | * c. have CAP_FOWNER capability | |
2636 | * 6. If the victim is append-only or immutable we can't do antyhing with | |
2637 | * links pointing to it. | |
0bd23d09 EB |
2638 | * 7. If the victim has an unknown uid or gid we can't change the inode. |
2639 | * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
2640 | * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
2641 | * 10. We can't remove a root or mountpoint. | |
2642 | * 11. We don't allow removal of NFS sillyrenamed files; it's handled by | |
1da177e4 LT |
2643 | * nfs_async_unlink(). |
2644 | */ | |
b18825a7 | 2645 | static int may_delete(struct inode *dir, struct dentry *victim, bool isdir) |
1da177e4 | 2646 | { |
63afdfc7 | 2647 | struct inode *inode = d_backing_inode(victim); |
1da177e4 LT |
2648 | int error; |
2649 | ||
b18825a7 | 2650 | if (d_is_negative(victim)) |
1da177e4 | 2651 | return -ENOENT; |
b18825a7 | 2652 | BUG_ON(!inode); |
1da177e4 LT |
2653 | |
2654 | BUG_ON(victim->d_parent->d_inode != dir); | |
593d1ce8 EB |
2655 | |
2656 | /* Inode writeback is not safe when the uid or gid are invalid. */ | |
2657 | if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid)) | |
2658 | return -EOVERFLOW; | |
2659 | ||
4fa6b5ec | 2660 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
1da177e4 | 2661 | |
f419a2e3 | 2662 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
1da177e4 LT |
2663 | if (error) |
2664 | return error; | |
2665 | if (IS_APPEND(dir)) | |
2666 | return -EPERM; | |
b18825a7 DH |
2667 | |
2668 | if (check_sticky(dir, inode) || IS_APPEND(inode) || | |
0bd23d09 | 2669 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode)) |
1da177e4 LT |
2670 | return -EPERM; |
2671 | if (isdir) { | |
44b1d530 | 2672 | if (!d_is_dir(victim)) |
1da177e4 LT |
2673 | return -ENOTDIR; |
2674 | if (IS_ROOT(victim)) | |
2675 | return -EBUSY; | |
44b1d530 | 2676 | } else if (d_is_dir(victim)) |
1da177e4 LT |
2677 | return -EISDIR; |
2678 | if (IS_DEADDIR(dir)) | |
2679 | return -ENOENT; | |
2680 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
2681 | return -EBUSY; | |
2682 | return 0; | |
2683 | } | |
2684 | ||
2685 | /* Check whether we can create an object with dentry child in directory | |
2686 | * dir. | |
2687 | * 1. We can't do it if child already exists (open has special treatment for | |
2688 | * this case, but since we are inlined it's OK) | |
2689 | * 2. We can't do it if dir is read-only (done in permission()) | |
036d5236 EB |
2690 | * 3. We can't do it if the fs can't represent the fsuid or fsgid. |
2691 | * 4. We should have write and exec permissions on dir | |
2692 | * 5. We can't do it if dir is immutable (done in permission()) | |
1da177e4 | 2693 | */ |
a95164d9 | 2694 | static inline int may_create(struct inode *dir, struct dentry *child) |
1da177e4 | 2695 | { |
036d5236 | 2696 | struct user_namespace *s_user_ns; |
14e972b4 | 2697 | audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); |
1da177e4 LT |
2698 | if (child->d_inode) |
2699 | return -EEXIST; | |
2700 | if (IS_DEADDIR(dir)) | |
2701 | return -ENOENT; | |
036d5236 EB |
2702 | s_user_ns = dir->i_sb->s_user_ns; |
2703 | if (!kuid_has_mapping(s_user_ns, current_fsuid()) || | |
2704 | !kgid_has_mapping(s_user_ns, current_fsgid())) | |
2705 | return -EOVERFLOW; | |
f419a2e3 | 2706 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
1da177e4 LT |
2707 | } |
2708 | ||
1da177e4 LT |
2709 | /* |
2710 | * p1 and p2 should be directories on the same fs. | |
2711 | */ | |
2712 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) | |
2713 | { | |
2714 | struct dentry *p; | |
2715 | ||
2716 | if (p1 == p2) { | |
5955102c | 2717 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
1da177e4 LT |
2718 | return NULL; |
2719 | } | |
2720 | ||
fc64005c | 2721 | mutex_lock(&p1->d_sb->s_vfs_rename_mutex); |
1da177e4 | 2722 | |
e2761a11 OH |
2723 | p = d_ancestor(p2, p1); |
2724 | if (p) { | |
5955102c AV |
2725 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); |
2726 | inode_lock_nested(p1->d_inode, I_MUTEX_CHILD); | |
e2761a11 | 2727 | return p; |
1da177e4 LT |
2728 | } |
2729 | ||
e2761a11 OH |
2730 | p = d_ancestor(p1, p2); |
2731 | if (p) { | |
5955102c AV |
2732 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
2733 | inode_lock_nested(p2->d_inode, I_MUTEX_CHILD); | |
e2761a11 | 2734 | return p; |
1da177e4 LT |
2735 | } |
2736 | ||
5955102c AV |
2737 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
2738 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); | |
1da177e4 LT |
2739 | return NULL; |
2740 | } | |
4d359507 | 2741 | EXPORT_SYMBOL(lock_rename); |
1da177e4 LT |
2742 | |
2743 | void unlock_rename(struct dentry *p1, struct dentry *p2) | |
2744 | { | |
5955102c | 2745 | inode_unlock(p1->d_inode); |
1da177e4 | 2746 | if (p1 != p2) { |
5955102c | 2747 | inode_unlock(p2->d_inode); |
fc64005c | 2748 | mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); |
1da177e4 LT |
2749 | } |
2750 | } | |
4d359507 | 2751 | EXPORT_SYMBOL(unlock_rename); |
1da177e4 | 2752 | |
4acdaf27 | 2753 | int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
312b63fb | 2754 | bool want_excl) |
1da177e4 | 2755 | { |
a95164d9 | 2756 | int error = may_create(dir, dentry); |
1da177e4 LT |
2757 | if (error) |
2758 | return error; | |
2759 | ||
acfa4380 | 2760 | if (!dir->i_op->create) |
1da177e4 LT |
2761 | return -EACCES; /* shouldn't it be ENOSYS? */ |
2762 | mode &= S_IALLUGO; | |
2763 | mode |= S_IFREG; | |
2764 | error = security_inode_create(dir, dentry, mode); | |
2765 | if (error) | |
2766 | return error; | |
312b63fb | 2767 | error = dir->i_op->create(dir, dentry, mode, want_excl); |
a74574aa | 2768 | if (!error) |
f38aa942 | 2769 | fsnotify_create(dir, dentry); |
1da177e4 LT |
2770 | return error; |
2771 | } | |
4d359507 | 2772 | EXPORT_SYMBOL(vfs_create); |
1da177e4 | 2773 | |
8e6c848e AV |
2774 | int vfs_mkobj(struct dentry *dentry, umode_t mode, |
2775 | int (*f)(struct dentry *, umode_t, void *), | |
2776 | void *arg) | |
2777 | { | |
2778 | struct inode *dir = dentry->d_parent->d_inode; | |
2779 | int error = may_create(dir, dentry); | |
2780 | if (error) | |
2781 | return error; | |
2782 | ||
2783 | mode &= S_IALLUGO; | |
2784 | mode |= S_IFREG; | |
2785 | error = security_inode_create(dir, dentry, mode); | |
2786 | if (error) | |
2787 | return error; | |
2788 | error = f(dentry, mode, arg); | |
2789 | if (!error) | |
2790 | fsnotify_create(dir, dentry); | |
2791 | return error; | |
2792 | } | |
2793 | EXPORT_SYMBOL(vfs_mkobj); | |
2794 | ||
a2982cc9 EB |
2795 | bool may_open_dev(const struct path *path) |
2796 | { | |
2797 | return !(path->mnt->mnt_flags & MNT_NODEV) && | |
2798 | !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); | |
2799 | } | |
2800 | ||
f0bb5aaf | 2801 | static int may_open(const struct path *path, int acc_mode, int flag) |
1da177e4 | 2802 | { |
3fb64190 | 2803 | struct dentry *dentry = path->dentry; |
1da177e4 LT |
2804 | struct inode *inode = dentry->d_inode; |
2805 | int error; | |
2806 | ||
2807 | if (!inode) | |
2808 | return -ENOENT; | |
2809 | ||
c8fe8f30 CH |
2810 | switch (inode->i_mode & S_IFMT) { |
2811 | case S_IFLNK: | |
1da177e4 | 2812 | return -ELOOP; |
c8fe8f30 CH |
2813 | case S_IFDIR: |
2814 | if (acc_mode & MAY_WRITE) | |
2815 | return -EISDIR; | |
2816 | break; | |
2817 | case S_IFBLK: | |
2818 | case S_IFCHR: | |
a2982cc9 | 2819 | if (!may_open_dev(path)) |
1da177e4 | 2820 | return -EACCES; |
c8fe8f30 CH |
2821 | /*FALLTHRU*/ |
2822 | case S_IFIFO: | |
2823 | case S_IFSOCK: | |
1da177e4 | 2824 | flag &= ~O_TRUNC; |
c8fe8f30 | 2825 | break; |
4a3fd211 | 2826 | } |
b41572e9 | 2827 | |
62fb4a15 | 2828 | error = inode_permission(inode, MAY_OPEN | acc_mode); |
b41572e9 DH |
2829 | if (error) |
2830 | return error; | |
6146f0d5 | 2831 | |
1da177e4 LT |
2832 | /* |
2833 | * An append-only file must be opened in append mode for writing. | |
2834 | */ | |
2835 | if (IS_APPEND(inode)) { | |
8737c930 | 2836 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
7715b521 | 2837 | return -EPERM; |
1da177e4 | 2838 | if (flag & O_TRUNC) |
7715b521 | 2839 | return -EPERM; |
1da177e4 LT |
2840 | } |
2841 | ||
2842 | /* O_NOATIME can only be set by the owner or superuser */ | |
2e149670 | 2843 | if (flag & O_NOATIME && !inode_owner_or_capable(inode)) |
7715b521 | 2844 | return -EPERM; |
1da177e4 | 2845 | |
f3c7691e | 2846 | return 0; |
7715b521 | 2847 | } |
1da177e4 | 2848 | |
e1181ee6 | 2849 | static int handle_truncate(struct file *filp) |
7715b521 | 2850 | { |
f0bb5aaf | 2851 | const struct path *path = &filp->f_path; |
7715b521 AV |
2852 | struct inode *inode = path->dentry->d_inode; |
2853 | int error = get_write_access(inode); | |
2854 | if (error) | |
2855 | return error; | |
2856 | /* | |
2857 | * Refuse to truncate files with mandatory locks held on them. | |
2858 | */ | |
d7a06983 | 2859 | error = locks_verify_locked(filp); |
7715b521 | 2860 | if (!error) |
ea0d3ab2 | 2861 | error = security_path_truncate(path); |
7715b521 AV |
2862 | if (!error) { |
2863 | error = do_truncate(path->dentry, 0, | |
2864 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, | |
e1181ee6 | 2865 | filp); |
7715b521 AV |
2866 | } |
2867 | put_write_access(inode); | |
acd0c935 | 2868 | return error; |
1da177e4 LT |
2869 | } |
2870 | ||
d57999e1 DH |
2871 | static inline int open_to_namei_flags(int flag) |
2872 | { | |
8a5e929d AV |
2873 | if ((flag & O_ACCMODE) == 3) |
2874 | flag--; | |
d57999e1 DH |
2875 | return flag; |
2876 | } | |
2877 | ||
d3607752 | 2878 | static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode) |
d18e9008 | 2879 | { |
1328c727 | 2880 | struct user_namespace *s_user_ns; |
d18e9008 MS |
2881 | int error = security_path_mknod(dir, dentry, mode, 0); |
2882 | if (error) | |
2883 | return error; | |
2884 | ||
1328c727 SF |
2885 | s_user_ns = dir->dentry->d_sb->s_user_ns; |
2886 | if (!kuid_has_mapping(s_user_ns, current_fsuid()) || | |
2887 | !kgid_has_mapping(s_user_ns, current_fsgid())) | |
2888 | return -EOVERFLOW; | |
2889 | ||
d18e9008 MS |
2890 | error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); |
2891 | if (error) | |
2892 | return error; | |
2893 | ||
2894 | return security_inode_create(dir->dentry->d_inode, dentry, mode); | |
2895 | } | |
2896 | ||
1acf0af9 DH |
2897 | /* |
2898 | * Attempt to atomically look up, create and open a file from a negative | |
2899 | * dentry. | |
2900 | * | |
2901 | * Returns 0 if successful. The file will have been created and attached to | |
2902 | * @file by the filesystem calling finish_open(). | |
2903 | * | |
00a07c15 AV |
2904 | * If the file was looked up only or didn't need creating, FMODE_OPENED won't |
2905 | * be set. The caller will need to perform the open themselves. @path will | |
2906 | * have been updated to point to the new dentry. This may be negative. | |
1acf0af9 DH |
2907 | * |
2908 | * Returns an error code otherwise. | |
2909 | */ | |
239eb983 AV |
2910 | static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, |
2911 | struct file *file, | |
2912 | const struct open_flags *op, | |
2913 | int open_flag, umode_t mode) | |
d18e9008 | 2914 | { |
384f26e2 | 2915 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
d18e9008 | 2916 | struct inode *dir = nd->path.dentry->d_inode; |
d18e9008 | 2917 | int error; |
d18e9008 | 2918 | |
384f26e2 | 2919 | if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */ |
d18e9008 | 2920 | open_flag &= ~O_TRUNC; |
d18e9008 | 2921 | |
d18e9008 MS |
2922 | if (nd->flags & LOOKUP_DIRECTORY) |
2923 | open_flag |= O_DIRECTORY; | |
2924 | ||
30d90494 AV |
2925 | file->f_path.dentry = DENTRY_NOT_SET; |
2926 | file->f_path.mnt = nd->path.mnt; | |
0fb1ea09 | 2927 | error = dir->i_op->atomic_open(dir, dentry, file, |
44907d79 | 2928 | open_to_namei_flags(open_flag), mode); |
6fbd0714 | 2929 | d_lookup_done(dentry); |
384f26e2 | 2930 | if (!error) { |
64e1ac4d | 2931 | if (file->f_mode & FMODE_OPENED) { |
6fb968cd AV |
2932 | if (unlikely(dentry != file->f_path.dentry)) { |
2933 | dput(dentry); | |
2934 | dentry = dget(file->f_path.dentry); | |
2935 | } | |
7be219b4 | 2936 | if (file->f_mode & FMODE_CREATED) |
64e1ac4d | 2937 | fsnotify_create(dir, dentry); |
64e1ac4d | 2938 | } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
2675a4eb | 2939 | error = -EIO; |
03da633a | 2940 | } else { |
384f26e2 AV |
2941 | if (file->f_path.dentry) { |
2942 | dput(dentry); | |
2943 | dentry = file->f_path.dentry; | |
03da633a | 2944 | } |
73a09dd9 | 2945 | if (file->f_mode & FMODE_CREATED) |
384f26e2 | 2946 | fsnotify_create(dir, dentry); |
239eb983 | 2947 | if (unlikely(d_is_negative(dentry))) |
a01e718f | 2948 | error = -ENOENT; |
62b2ce96 | 2949 | } |
d18e9008 | 2950 | } |
239eb983 AV |
2951 | if (error) { |
2952 | dput(dentry); | |
2953 | dentry = ERR_PTR(error); | |
2954 | } | |
2955 | return dentry; | |
d18e9008 MS |
2956 | } |
2957 | ||
d58ffd35 | 2958 | /* |
1acf0af9 | 2959 | * Look up and maybe create and open the last component. |
d58ffd35 | 2960 | * |
00a07c15 | 2961 | * Must be called with parent locked (exclusive in O_CREAT case). |
1acf0af9 | 2962 | * |
00a07c15 AV |
2963 | * Returns 0 on success, that is, if |
2964 | * the file was successfully atomically created (if necessary) and opened, or | |
2965 | * the file was not completely opened at this time, though lookups and | |
2966 | * creations were performed. | |
2967 | * These case are distinguished by presence of FMODE_OPENED on file->f_mode. | |
2968 | * In the latter case dentry returned in @path might be negative if O_CREAT | |
2969 | * hadn't been specified. | |
1acf0af9 | 2970 | * |
00a07c15 | 2971 | * An error code is returned on failure. |
d58ffd35 | 2972 | */ |
da5ebf5a AV |
2973 | static struct dentry *lookup_open(struct nameidata *nd, struct file *file, |
2974 | const struct open_flags *op, | |
2975 | bool got_write) | |
d58ffd35 MS |
2976 | { |
2977 | struct dentry *dir = nd->path.dentry; | |
54ef4872 | 2978 | struct inode *dir_inode = dir->d_inode; |
1643b43f | 2979 | int open_flag = op->open_flag; |
d58ffd35 | 2980 | struct dentry *dentry; |
1643b43f | 2981 | int error, create_error = 0; |
1643b43f | 2982 | umode_t mode = op->mode; |
6fbd0714 | 2983 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
d58ffd35 | 2984 | |
ce8644fc | 2985 | if (unlikely(IS_DEADDIR(dir_inode))) |
da5ebf5a | 2986 | return ERR_PTR(-ENOENT); |
d58ffd35 | 2987 | |
73a09dd9 | 2988 | file->f_mode &= ~FMODE_CREATED; |
6fbd0714 AV |
2989 | dentry = d_lookup(dir, &nd->last); |
2990 | for (;;) { | |
2991 | if (!dentry) { | |
2992 | dentry = d_alloc_parallel(dir, &nd->last, &wq); | |
2993 | if (IS_ERR(dentry)) | |
da5ebf5a | 2994 | return dentry; |
6fbd0714 AV |
2995 | } |
2996 | if (d_in_lookup(dentry)) | |
2997 | break; | |
d58ffd35 | 2998 | |
6fbd0714 AV |
2999 | error = d_revalidate(dentry, nd->flags); |
3000 | if (likely(error > 0)) | |
3001 | break; | |
3002 | if (error) | |
3003 | goto out_dput; | |
3004 | d_invalidate(dentry); | |
3005 | dput(dentry); | |
3006 | dentry = NULL; | |
3007 | } | |
3008 | if (dentry->d_inode) { | |
6c51e513 | 3009 | /* Cached positive dentry: will open in f_op->open */ |
da5ebf5a | 3010 | return dentry; |
6c51e513 | 3011 | } |
d18e9008 | 3012 | |
1643b43f AV |
3013 | /* |
3014 | * Checking write permission is tricky, bacuse we don't know if we are | |
3015 | * going to actually need it: O_CREAT opens should work as long as the | |
3016 | * file exists. But checking existence breaks atomicity. The trick is | |
3017 | * to check access and if not granted clear O_CREAT from the flags. | |
3018 | * | |
3019 | * Another problem is returing the "right" error value (e.g. for an | |
3020 | * O_EXCL open we want to return EEXIST not EROFS). | |
3021 | */ | |
3022 | if (open_flag & O_CREAT) { | |
3023 | if (!IS_POSIXACL(dir->d_inode)) | |
3024 | mode &= ~current_umask(); | |
3025 | if (unlikely(!got_write)) { | |
3026 | create_error = -EROFS; | |
3027 | open_flag &= ~O_CREAT; | |
3028 | if (open_flag & (O_EXCL | O_TRUNC)) | |
3029 | goto no_open; | |
3030 | /* No side effects, safe to clear O_CREAT */ | |
3031 | } else { | |
3032 | create_error = may_o_create(&nd->path, dentry, mode); | |
3033 | if (create_error) { | |
3034 | open_flag &= ~O_CREAT; | |
3035 | if (open_flag & O_EXCL) | |
3036 | goto no_open; | |
3037 | } | |
3038 | } | |
3039 | } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) && | |
3040 | unlikely(!got_write)) { | |
3041 | /* | |
3042 | * No O_CREATE -> atomicity not a requirement -> fall | |
3043 | * back to lookup + open | |
3044 | */ | |
3045 | goto no_open; | |
d18e9008 MS |
3046 | } |
3047 | ||
6ac08709 | 3048 | if (dir_inode->i_op->atomic_open) { |
239eb983 | 3049 | dentry = atomic_open(nd, dentry, file, op, open_flag, mode); |
da5ebf5a AV |
3050 | if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) |
3051 | dentry = ERR_PTR(create_error); | |
3052 | return dentry; | |
d18e9008 | 3053 | } |
54ef4872 | 3054 | |
1643b43f | 3055 | no_open: |
6fbd0714 | 3056 | if (d_in_lookup(dentry)) { |
12fa5e24 AV |
3057 | struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, |
3058 | nd->flags); | |
6fbd0714 | 3059 | d_lookup_done(dentry); |
12fa5e24 AV |
3060 | if (unlikely(res)) { |
3061 | if (IS_ERR(res)) { | |
3062 | error = PTR_ERR(res); | |
3063 | goto out_dput; | |
3064 | } | |
3065 | dput(dentry); | |
3066 | dentry = res; | |
3067 | } | |
54ef4872 MS |
3068 | } |
3069 | ||
d58ffd35 | 3070 | /* Negative dentry, just create the file */ |
1643b43f | 3071 | if (!dentry->d_inode && (open_flag & O_CREAT)) { |
73a09dd9 | 3072 | file->f_mode |= FMODE_CREATED; |
ce8644fc | 3073 | audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
ce8644fc AV |
3074 | if (!dir_inode->i_op->create) { |
3075 | error = -EACCES; | |
d58ffd35 | 3076 | goto out_dput; |
ce8644fc AV |
3077 | } |
3078 | error = dir_inode->i_op->create(dir_inode, dentry, mode, | |
1643b43f | 3079 | open_flag & O_EXCL); |
d58ffd35 MS |
3080 | if (error) |
3081 | goto out_dput; | |
ce8644fc | 3082 | fsnotify_create(dir_inode, dentry); |
d58ffd35 | 3083 | } |
1643b43f AV |
3084 | if (unlikely(create_error) && !dentry->d_inode) { |
3085 | error = create_error; | |
3086 | goto out_dput; | |
d58ffd35 | 3087 | } |
da5ebf5a | 3088 | return dentry; |
d58ffd35 MS |
3089 | |
3090 | out_dput: | |
3091 | dput(dentry); | |
da5ebf5a | 3092 | return ERR_PTR(error); |
d58ffd35 MS |
3093 | } |
3094 | ||
c981a482 | 3095 | static const char *open_last_lookups(struct nameidata *nd, |
3ec2eef1 | 3096 | struct file *file, const struct open_flags *op) |
fb1cc555 | 3097 | { |
a1e28038 | 3098 | struct dentry *dir = nd->path.dentry; |
ca344a89 | 3099 | int open_flag = op->open_flag; |
64894cf8 | 3100 | bool got_write = false; |
254cf582 | 3101 | unsigned seq; |
a1eb3315 | 3102 | struct inode *inode; |
da5ebf5a | 3103 | struct dentry *dentry; |
b0417d2c | 3104 | const char *res; |
16c2cd71 | 3105 | int error; |
1f36f774 | 3106 | |
c3e380b0 AV |
3107 | nd->flags |= op->intent; |
3108 | ||
bc77daa7 | 3109 | if (nd->last_type != LAST_NORM) { |
56676ec3 AV |
3110 | if (nd->depth) |
3111 | put_link(nd); | |
7521f22b AV |
3112 | res = handle_dots(nd, nd->last_type); |
3113 | if (likely(!res)) | |
3114 | res = ERR_PTR(complete_walk(nd)); | |
3115 | return res; | |
1f36f774 | 3116 | } |
67ee3ad2 | 3117 | |
ca344a89 | 3118 | if (!(open_flag & O_CREAT)) { |
fe2d35ff AV |
3119 | if (nd->last.name[nd->last.len]) |
3120 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
3121 | /* we _can_ be in RCU mode here */ | |
20e34357 AV |
3122 | dentry = lookup_fast(nd, &inode, &seq); |
3123 | if (IS_ERR(dentry)) | |
1ccac622 | 3124 | return ERR_CAST(dentry); |
20e34357 | 3125 | if (likely(dentry)) |
71574865 MS |
3126 | goto finish_lookup; |
3127 | ||
6583fe22 | 3128 | BUG_ON(nd->flags & LOOKUP_RCU); |
b6183df7 MS |
3129 | } else { |
3130 | /* create side of things */ | |
3131 | /* | |
3132 | * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED | |
3133 | * has been cleared when we got to the last component we are | |
3134 | * about to look up | |
3135 | */ | |
3136 | error = complete_walk(nd); | |
c981a482 | 3137 | if (unlikely(error)) |
1ccac622 | 3138 | return ERR_PTR(error); |
fe2d35ff | 3139 | |
c9b07eab | 3140 | audit_inode(nd->name, dir, AUDIT_INODE_PARENT); |
b6183df7 | 3141 | /* trailing slashes? */ |
deb106c6 | 3142 | if (unlikely(nd->last.name[nd->last.len])) |
1ccac622 | 3143 | return ERR_PTR(-EISDIR); |
b6183df7 | 3144 | } |
a2c36b45 | 3145 | |
9cf843e3 | 3146 | if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
64894cf8 AV |
3147 | error = mnt_want_write(nd->path.mnt); |
3148 | if (!error) | |
3149 | got_write = true; | |
3150 | /* | |
3151 | * do _not_ fail yet - we might not need that or fail with | |
3152 | * a different error; let lookup_open() decide; we'll be | |
3153 | * dropping this one anyway. | |
3154 | */ | |
3155 | } | |
9cf843e3 AV |
3156 | if (open_flag & O_CREAT) |
3157 | inode_lock(dir->d_inode); | |
3158 | else | |
3159 | inode_lock_shared(dir->d_inode); | |
da5ebf5a | 3160 | dentry = lookup_open(nd, file, op, got_write); |
9cf843e3 AV |
3161 | if (open_flag & O_CREAT) |
3162 | inode_unlock(dir->d_inode); | |
3163 | else | |
3164 | inode_unlock_shared(dir->d_inode); | |
a1e28038 | 3165 | |
c981a482 | 3166 | if (got_write) |
59e96e65 | 3167 | mnt_drop_write(nd->path.mnt); |
d18e9008 | 3168 | |
59e96e65 AV |
3169 | if (IS_ERR(dentry)) |
3170 | return ERR_CAST(dentry); | |
3171 | ||
973d4b73 | 3172 | if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { |
e73cabff AV |
3173 | dput(nd->path.dentry); |
3174 | nd->path.dentry = dentry; | |
c981a482 | 3175 | return NULL; |
fb1cc555 AV |
3176 | } |
3177 | ||
20e34357 | 3178 | finish_lookup: |
56676ec3 AV |
3179 | if (nd->depth) |
3180 | put_link(nd); | |
8c4efe22 | 3181 | res = step_into(nd, WALK_TRAILING, dentry, inode, seq); |
b0417d2c | 3182 | if (unlikely(res)) { |
b0417d2c | 3183 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
b0417d2c | 3184 | return res; |
1ccac622 | 3185 | } |
31d1726d AV |
3186 | |
3187 | if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) { | |
3188 | audit_inode(nd->name, nd->path.dentry, 0); | |
1ccac622 | 3189 | return ERR_PTR(-EEXIST); |
31d1726d | 3190 | } |
c981a482 | 3191 | |
8f64fb1c | 3192 | /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ |
c981a482 AV |
3193 | return ERR_PTR(complete_walk(nd)); |
3194 | } | |
3195 | ||
3196 | /* | |
3197 | * Handle the last step of open() | |
3198 | */ | |
3199 | static const char *do_last(struct nameidata *nd, | |
3200 | struct file *file, const struct open_flags *op) | |
3201 | { | |
3202 | kuid_t dir_uid = nd->inode->i_uid; | |
3203 | umode_t dir_mode = nd->inode->i_mode; | |
3204 | int open_flag = op->open_flag; | |
3205 | bool do_truncate; | |
3206 | int acc_mode; | |
3207 | const char *link; | |
3208 | int error; | |
3209 | ||
3210 | link = open_last_lookups(nd, file, op); | |
3211 | if (unlikely(link)) | |
3212 | return link; | |
3213 | ||
973d4b73 AV |
3214 | if (!(file->f_mode & FMODE_CREATED)) |
3215 | audit_inode(nd->name, nd->path.dentry, 0); | |
30aba665 | 3216 | if (open_flag & O_CREAT) { |
30aba665 | 3217 | if (d_is_dir(nd->path.dentry)) |
59e96e65 | 3218 | return ERR_PTR(-EISDIR); |
d0cb5018 | 3219 | error = may_create_in_sticky(dir_mode, dir_uid, |
30aba665 SM |
3220 | d_backing_inode(nd->path.dentry)); |
3221 | if (unlikely(error)) | |
59e96e65 | 3222 | return ERR_PTR(error); |
30aba665 | 3223 | } |
44b1d530 | 3224 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) |
59e96e65 | 3225 | return ERR_PTR(-ENOTDIR); |
6c0d46c4 | 3226 | |
8795e7d4 AV |
3227 | do_truncate = false; |
3228 | acc_mode = op->acc_mode; | |
5a2d3edd AV |
3229 | if (file->f_mode & FMODE_CREATED) { |
3230 | /* Don't check for write permission, don't truncate */ | |
3231 | open_flag &= ~O_TRUNC; | |
5a2d3edd | 3232 | acc_mode = 0; |
8795e7d4 | 3233 | } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { |
0f9d1a10 AV |
3234 | error = mnt_want_write(nd->path.mnt); |
3235 | if (error) | |
59e96e65 | 3236 | return ERR_PTR(error); |
8795e7d4 | 3237 | do_truncate = true; |
0f9d1a10 | 3238 | } |
6ac08709 | 3239 | error = may_open(&nd->path, acc_mode, open_flag); |
8795e7d4 | 3240 | if (!error && !(file->f_mode & FMODE_OPENED)) |
3ad5615a | 3241 | error = vfs_open(&nd->path, file); |
8795e7d4 AV |
3242 | if (!error) |
3243 | error = ima_file_check(file, op->acc_mode); | |
3244 | if (!error && do_truncate) | |
2675a4eb | 3245 | error = handle_truncate(file); |
c80567c8 AV |
3246 | if (unlikely(error > 0)) { |
3247 | WARN_ON(1); | |
3248 | error = -EINVAL; | |
3249 | } | |
8795e7d4 | 3250 | if (do_truncate) |
0f9d1a10 | 3251 | mnt_drop_write(nd->path.mnt); |
1ccac622 | 3252 | return ERR_PTR(error); |
fb1cc555 AV |
3253 | } |
3254 | ||
af7bd4dc AG |
3255 | struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag) |
3256 | { | |
af7bd4dc AG |
3257 | struct dentry *child = NULL; |
3258 | struct inode *dir = dentry->d_inode; | |
3259 | struct inode *inode; | |
3260 | int error; | |
3261 | ||
3262 | /* we want directory to be writable */ | |
3263 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
3264 | if (error) | |
3265 | goto out_err; | |
3266 | error = -EOPNOTSUPP; | |
3267 | if (!dir->i_op->tmpfile) | |
3268 | goto out_err; | |
3269 | error = -ENOMEM; | |
cdf01226 | 3270 | child = d_alloc(dentry, &slash_name); |
af7bd4dc AG |
3271 | if (unlikely(!child)) |
3272 | goto out_err; | |
3273 | error = dir->i_op->tmpfile(dir, child, mode); | |
3274 | if (error) | |
3275 | goto out_err; | |
3276 | error = -ENOENT; | |
3277 | inode = child->d_inode; | |
3278 | if (unlikely(!inode)) | |
3279 | goto out_err; | |
3280 | if (!(open_flag & O_EXCL)) { | |
3281 | spin_lock(&inode->i_lock); | |
3282 | inode->i_state |= I_LINKABLE; | |
3283 | spin_unlock(&inode->i_lock); | |
3284 | } | |
fdb2410f | 3285 | ima_post_create_tmpfile(inode); |
af7bd4dc AG |
3286 | return child; |
3287 | ||
3288 | out_err: | |
3289 | dput(child); | |
3290 | return ERR_PTR(error); | |
3291 | } | |
3292 | EXPORT_SYMBOL(vfs_tmpfile); | |
3293 | ||
c8a53ee5 | 3294 | static int do_tmpfile(struct nameidata *nd, unsigned flags, |
60545d0d | 3295 | const struct open_flags *op, |
3ec2eef1 | 3296 | struct file *file) |
60545d0d | 3297 | { |
625b6d10 | 3298 | struct dentry *child; |
625b6d10 | 3299 | struct path path; |
c8a53ee5 | 3300 | int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); |
60545d0d AV |
3301 | if (unlikely(error)) |
3302 | return error; | |
625b6d10 | 3303 | error = mnt_want_write(path.mnt); |
60545d0d AV |
3304 | if (unlikely(error)) |
3305 | goto out; | |
af7bd4dc AG |
3306 | child = vfs_tmpfile(path.dentry, op->mode, op->open_flag); |
3307 | error = PTR_ERR(child); | |
684e73be | 3308 | if (IS_ERR(child)) |
60545d0d | 3309 | goto out2; |
625b6d10 AV |
3310 | dput(path.dentry); |
3311 | path.dentry = child; | |
c8a53ee5 | 3312 | audit_inode(nd->name, child, 0); |
69a91c23 | 3313 | /* Don't check for other permissions, the inode was just created */ |
62fb4a15 | 3314 | error = may_open(&path, 0, op->open_flag); |
60545d0d AV |
3315 | if (error) |
3316 | goto out2; | |
625b6d10 | 3317 | file->f_path.mnt = path.mnt; |
be12af3e | 3318 | error = finish_open(file, child, NULL); |
60545d0d | 3319 | out2: |
625b6d10 | 3320 | mnt_drop_write(path.mnt); |
60545d0d | 3321 | out: |
625b6d10 | 3322 | path_put(&path); |
60545d0d AV |
3323 | return error; |
3324 | } | |
3325 | ||
6ac08709 AV |
3326 | static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) |
3327 | { | |
3328 | struct path path; | |
3329 | int error = path_lookupat(nd, flags, &path); | |
3330 | if (!error) { | |
3331 | audit_inode(nd->name, path.dentry, 0); | |
ae2bb293 | 3332 | error = vfs_open(&path, file); |
6ac08709 AV |
3333 | path_put(&path); |
3334 | } | |
3335 | return error; | |
3336 | } | |
3337 | ||
c8a53ee5 AV |
3338 | static struct file *path_openat(struct nameidata *nd, |
3339 | const struct open_flags *op, unsigned flags) | |
1da177e4 | 3340 | { |
30d90494 | 3341 | struct file *file; |
13aab428 | 3342 | int error; |
31e6b01f | 3343 | |
ea73ea72 | 3344 | file = alloc_empty_file(op->open_flag, current_cred()); |
1afc99be AV |
3345 | if (IS_ERR(file)) |
3346 | return file; | |
31e6b01f | 3347 | |
bb458c64 | 3348 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
3ec2eef1 | 3349 | error = do_tmpfile(nd, flags, op, file); |
5f336e72 | 3350 | } else if (unlikely(file->f_flags & O_PATH)) { |
6ac08709 | 3351 | error = do_o_path(nd, flags, file); |
5f336e72 AV |
3352 | } else { |
3353 | const char *s = path_init(nd, flags); | |
3354 | while (!(error = link_path_walk(s, nd)) && | |
1ccac622 AV |
3355 | (s = do_last(nd, file, op)) != NULL) |
3356 | ; | |
5f336e72 | 3357 | terminate_walk(nd); |
806b681c | 3358 | } |
7c1c01ec | 3359 | if (likely(!error)) { |
aad888f8 | 3360 | if (likely(file->f_mode & FMODE_OPENED)) |
7c1c01ec AV |
3361 | return file; |
3362 | WARN_ON(1); | |
3363 | error = -EINVAL; | |
16b1c1cd | 3364 | } |
7c1c01ec AV |
3365 | fput(file); |
3366 | if (error == -EOPENSTALE) { | |
3367 | if (flags & LOOKUP_RCU) | |
3368 | error = -ECHILD; | |
3369 | else | |
3370 | error = -ESTALE; | |
2675a4eb | 3371 | } |
7c1c01ec | 3372 | return ERR_PTR(error); |
1da177e4 LT |
3373 | } |
3374 | ||
669abf4e | 3375 | struct file *do_filp_open(int dfd, struct filename *pathname, |
f9652e10 | 3376 | const struct open_flags *op) |
13aab428 | 3377 | { |
9883d185 | 3378 | struct nameidata nd; |
f9652e10 | 3379 | int flags = op->lookup_flags; |
13aab428 AV |
3380 | struct file *filp; |
3381 | ||
9883d185 | 3382 | set_nameidata(&nd, dfd, pathname); |
c8a53ee5 | 3383 | filp = path_openat(&nd, op, flags | LOOKUP_RCU); |
13aab428 | 3384 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
c8a53ee5 | 3385 | filp = path_openat(&nd, op, flags); |
13aab428 | 3386 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
c8a53ee5 | 3387 | filp = path_openat(&nd, op, flags | LOOKUP_REVAL); |
9883d185 | 3388 | restore_nameidata(); |
13aab428 AV |
3389 | return filp; |
3390 | } | |
3391 | ||
73d049a4 | 3392 | struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
f9652e10 | 3393 | const char *name, const struct open_flags *op) |
73d049a4 | 3394 | { |
9883d185 | 3395 | struct nameidata nd; |
73d049a4 | 3396 | struct file *file; |
51689104 | 3397 | struct filename *filename; |
f9652e10 | 3398 | int flags = op->lookup_flags | LOOKUP_ROOT; |
73d049a4 AV |
3399 | |
3400 | nd.root.mnt = mnt; | |
3401 | nd.root.dentry = dentry; | |
3402 | ||
b18825a7 | 3403 | if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN) |
73d049a4 AV |
3404 | return ERR_PTR(-ELOOP); |
3405 | ||
51689104 | 3406 | filename = getname_kernel(name); |
a1c83681 | 3407 | if (IS_ERR(filename)) |
51689104 PM |
3408 | return ERR_CAST(filename); |
3409 | ||
9883d185 | 3410 | set_nameidata(&nd, -1, filename); |
c8a53ee5 | 3411 | file = path_openat(&nd, op, flags | LOOKUP_RCU); |
73d049a4 | 3412 | if (unlikely(file == ERR_PTR(-ECHILD))) |
c8a53ee5 | 3413 | file = path_openat(&nd, op, flags); |
73d049a4 | 3414 | if (unlikely(file == ERR_PTR(-ESTALE))) |
c8a53ee5 | 3415 | file = path_openat(&nd, op, flags | LOOKUP_REVAL); |
9883d185 | 3416 | restore_nameidata(); |
51689104 | 3417 | putname(filename); |
73d049a4 AV |
3418 | return file; |
3419 | } | |
3420 | ||
fa14a0b8 | 3421 | static struct dentry *filename_create(int dfd, struct filename *name, |
1ac12b4b | 3422 | struct path *path, unsigned int lookup_flags) |
1da177e4 | 3423 | { |
c663e5d8 | 3424 | struct dentry *dentry = ERR_PTR(-EEXIST); |
391172c4 AV |
3425 | struct qstr last; |
3426 | int type; | |
c30dabfe | 3427 | int err2; |
1ac12b4b JL |
3428 | int error; |
3429 | bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); | |
3430 | ||
3431 | /* | |
3432 | * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any | |
3433 | * other flags passed in are ignored! | |
3434 | */ | |
3435 | lookup_flags &= LOOKUP_REVAL; | |
3436 | ||
5c31b6ce AV |
3437 | name = filename_parentat(dfd, name, lookup_flags, path, &last, &type); |
3438 | if (IS_ERR(name)) | |
3439 | return ERR_CAST(name); | |
1da177e4 | 3440 | |
c663e5d8 CH |
3441 | /* |
3442 | * Yucky last component or no last component at all? | |
3443 | * (foo/., foo/.., /////) | |
3444 | */ | |
5c31b6ce | 3445 | if (unlikely(type != LAST_NORM)) |
ed75e95d | 3446 | goto out; |
c663e5d8 | 3447 | |
c30dabfe | 3448 | /* don't fail immediately if it's r/o, at least try to report other errors */ |
391172c4 | 3449 | err2 = mnt_want_write(path->mnt); |
c663e5d8 CH |
3450 | /* |
3451 | * Do the final lookup. | |
3452 | */ | |
391172c4 | 3453 | lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL; |
5955102c | 3454 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
391172c4 | 3455 | dentry = __lookup_hash(&last, path->dentry, lookup_flags); |
1da177e4 | 3456 | if (IS_ERR(dentry)) |
a8104a9f | 3457 | goto unlock; |
c663e5d8 | 3458 | |
a8104a9f | 3459 | error = -EEXIST; |
b18825a7 | 3460 | if (d_is_positive(dentry)) |
a8104a9f | 3461 | goto fail; |
b18825a7 | 3462 | |
c663e5d8 CH |
3463 | /* |
3464 | * Special case - lookup gave negative, but... we had foo/bar/ | |
3465 | * From the vfs_mknod() POV we just have a negative dentry - | |
3466 | * all is fine. Let's be bastards - you had / on the end, you've | |
3467 | * been asking for (non-existent) directory. -ENOENT for you. | |
3468 | */ | |
391172c4 | 3469 | if (unlikely(!is_dir && last.name[last.len])) { |
a8104a9f | 3470 | error = -ENOENT; |
ed75e95d | 3471 | goto fail; |
e9baf6e5 | 3472 | } |
c30dabfe JK |
3473 | if (unlikely(err2)) { |
3474 | error = err2; | |
a8104a9f | 3475 | goto fail; |
c30dabfe | 3476 | } |
181c37b6 | 3477 | putname(name); |
1da177e4 | 3478 | return dentry; |
1da177e4 | 3479 | fail: |
a8104a9f AV |
3480 | dput(dentry); |
3481 | dentry = ERR_PTR(error); | |
3482 | unlock: | |
5955102c | 3483 | inode_unlock(path->dentry->d_inode); |
c30dabfe | 3484 | if (!err2) |
391172c4 | 3485 | mnt_drop_write(path->mnt); |
ed75e95d | 3486 | out: |
391172c4 | 3487 | path_put(path); |
181c37b6 | 3488 | putname(name); |
1da177e4 LT |
3489 | return dentry; |
3490 | } | |
fa14a0b8 AV |
3491 | |
3492 | struct dentry *kern_path_create(int dfd, const char *pathname, | |
3493 | struct path *path, unsigned int lookup_flags) | |
3494 | { | |
181c37b6 AV |
3495 | return filename_create(dfd, getname_kernel(pathname), |
3496 | path, lookup_flags); | |
fa14a0b8 | 3497 | } |
dae6ad8f AV |
3498 | EXPORT_SYMBOL(kern_path_create); |
3499 | ||
921a1650 AV |
3500 | void done_path_create(struct path *path, struct dentry *dentry) |
3501 | { | |
3502 | dput(dentry); | |
5955102c | 3503 | inode_unlock(path->dentry->d_inode); |
a8104a9f | 3504 | mnt_drop_write(path->mnt); |
921a1650 AV |
3505 | path_put(path); |
3506 | } | |
3507 | EXPORT_SYMBOL(done_path_create); | |
3508 | ||
520ae687 | 3509 | inline struct dentry *user_path_create(int dfd, const char __user *pathname, |
1ac12b4b | 3510 | struct path *path, unsigned int lookup_flags) |
dae6ad8f | 3511 | { |
181c37b6 | 3512 | return filename_create(dfd, getname(pathname), path, lookup_flags); |
dae6ad8f AV |
3513 | } |
3514 | EXPORT_SYMBOL(user_path_create); | |
3515 | ||
1a67aafb | 3516 | int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
1da177e4 | 3517 | { |
a95164d9 | 3518 | int error = may_create(dir, dentry); |
1da177e4 LT |
3519 | |
3520 | if (error) | |
3521 | return error; | |
3522 | ||
94f82008 | 3523 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) |
1da177e4 LT |
3524 | return -EPERM; |
3525 | ||
acfa4380 | 3526 | if (!dir->i_op->mknod) |
1da177e4 LT |
3527 | return -EPERM; |
3528 | ||
08ce5f16 SH |
3529 | error = devcgroup_inode_mknod(mode, dev); |
3530 | if (error) | |
3531 | return error; | |
3532 | ||
1da177e4 LT |
3533 | error = security_inode_mknod(dir, dentry, mode, dev); |
3534 | if (error) | |
3535 | return error; | |
3536 | ||
1da177e4 | 3537 | error = dir->i_op->mknod(dir, dentry, mode, dev); |
a74574aa | 3538 | if (!error) |
f38aa942 | 3539 | fsnotify_create(dir, dentry); |
1da177e4 LT |
3540 | return error; |
3541 | } | |
4d359507 | 3542 | EXPORT_SYMBOL(vfs_mknod); |
1da177e4 | 3543 | |
f69aac00 | 3544 | static int may_mknod(umode_t mode) |
463c3197 DH |
3545 | { |
3546 | switch (mode & S_IFMT) { | |
3547 | case S_IFREG: | |
3548 | case S_IFCHR: | |
3549 | case S_IFBLK: | |
3550 | case S_IFIFO: | |
3551 | case S_IFSOCK: | |
3552 | case 0: /* zero mode translates to S_IFREG */ | |
3553 | return 0; | |
3554 | case S_IFDIR: | |
3555 | return -EPERM; | |
3556 | default: | |
3557 | return -EINVAL; | |
3558 | } | |
3559 | } | |
3560 | ||
87c4e192 DB |
3561 | long do_mknodat(int dfd, const char __user *filename, umode_t mode, |
3562 | unsigned int dev) | |
1da177e4 | 3563 | { |
2ad94ae6 | 3564 | struct dentry *dentry; |
dae6ad8f AV |
3565 | struct path path; |
3566 | int error; | |
972567f1 | 3567 | unsigned int lookup_flags = 0; |
1da177e4 | 3568 | |
8e4bfca1 AV |
3569 | error = may_mknod(mode); |
3570 | if (error) | |
3571 | return error; | |
972567f1 JL |
3572 | retry: |
3573 | dentry = user_path_create(dfd, filename, &path, lookup_flags); | |
dae6ad8f AV |
3574 | if (IS_ERR(dentry)) |
3575 | return PTR_ERR(dentry); | |
2ad94ae6 | 3576 | |
dae6ad8f | 3577 | if (!IS_POSIXACL(path.dentry->d_inode)) |
ce3b0f8d | 3578 | mode &= ~current_umask(); |
dae6ad8f | 3579 | error = security_path_mknod(&path, dentry, mode, dev); |
be6d3e56 | 3580 | if (error) |
a8104a9f | 3581 | goto out; |
463c3197 | 3582 | switch (mode & S_IFMT) { |
1da177e4 | 3583 | case 0: case S_IFREG: |
312b63fb | 3584 | error = vfs_create(path.dentry->d_inode,dentry,mode,true); |
05d1a717 MZ |
3585 | if (!error) |
3586 | ima_post_path_mknod(dentry); | |
1da177e4 LT |
3587 | break; |
3588 | case S_IFCHR: case S_IFBLK: | |
dae6ad8f | 3589 | error = vfs_mknod(path.dentry->d_inode,dentry,mode, |
1da177e4 LT |
3590 | new_decode_dev(dev)); |
3591 | break; | |
3592 | case S_IFIFO: case S_IFSOCK: | |
dae6ad8f | 3593 | error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); |
1da177e4 | 3594 | break; |
1da177e4 | 3595 | } |
a8104a9f | 3596 | out: |
921a1650 | 3597 | done_path_create(&path, dentry); |
972567f1 JL |
3598 | if (retry_estale(error, lookup_flags)) { |
3599 | lookup_flags |= LOOKUP_REVAL; | |
3600 | goto retry; | |
3601 | } | |
1da177e4 LT |
3602 | return error; |
3603 | } | |
3604 | ||
87c4e192 DB |
3605 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
3606 | unsigned int, dev) | |
3607 | { | |
3608 | return do_mknodat(dfd, filename, mode, dev); | |
3609 | } | |
3610 | ||
8208a22b | 3611 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
5590ff0d | 3612 | { |
87c4e192 | 3613 | return do_mknodat(AT_FDCWD, filename, mode, dev); |
5590ff0d UD |
3614 | } |
3615 | ||
18bb1db3 | 3616 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1da177e4 | 3617 | { |
a95164d9 | 3618 | int error = may_create(dir, dentry); |
8de52778 | 3619 | unsigned max_links = dir->i_sb->s_max_links; |
1da177e4 LT |
3620 | |
3621 | if (error) | |
3622 | return error; | |
3623 | ||
acfa4380 | 3624 | if (!dir->i_op->mkdir) |
1da177e4 LT |
3625 | return -EPERM; |
3626 | ||
3627 | mode &= (S_IRWXUGO|S_ISVTX); | |
3628 | error = security_inode_mkdir(dir, dentry, mode); | |
3629 | if (error) | |
3630 | return error; | |
3631 | ||
8de52778 AV |
3632 | if (max_links && dir->i_nlink >= max_links) |
3633 | return -EMLINK; | |
3634 | ||
1da177e4 | 3635 | error = dir->i_op->mkdir(dir, dentry, mode); |
a74574aa | 3636 | if (!error) |
f38aa942 | 3637 | fsnotify_mkdir(dir, dentry); |
1da177e4 LT |
3638 | return error; |
3639 | } | |
4d359507 | 3640 | EXPORT_SYMBOL(vfs_mkdir); |
1da177e4 | 3641 | |
0101db7a | 3642 | long do_mkdirat(int dfd, const char __user *pathname, umode_t mode) |
1da177e4 | 3643 | { |
6902d925 | 3644 | struct dentry *dentry; |
dae6ad8f AV |
3645 | struct path path; |
3646 | int error; | |
b76d8b82 | 3647 | unsigned int lookup_flags = LOOKUP_DIRECTORY; |
1da177e4 | 3648 | |
b76d8b82 JL |
3649 | retry: |
3650 | dentry = user_path_create(dfd, pathname, &path, lookup_flags); | |
6902d925 | 3651 | if (IS_ERR(dentry)) |
dae6ad8f | 3652 | return PTR_ERR(dentry); |
1da177e4 | 3653 | |
dae6ad8f | 3654 | if (!IS_POSIXACL(path.dentry->d_inode)) |
ce3b0f8d | 3655 | mode &= ~current_umask(); |
dae6ad8f | 3656 | error = security_path_mkdir(&path, dentry, mode); |
a8104a9f AV |
3657 | if (!error) |
3658 | error = vfs_mkdir(path.dentry->d_inode, dentry, mode); | |
921a1650 | 3659 | done_path_create(&path, dentry); |
b76d8b82 JL |
3660 | if (retry_estale(error, lookup_flags)) { |
3661 | lookup_flags |= LOOKUP_REVAL; | |
3662 | goto retry; | |
3663 | } | |
1da177e4 LT |
3664 | return error; |
3665 | } | |
3666 | ||
0101db7a DB |
3667 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
3668 | { | |
3669 | return do_mkdirat(dfd, pathname, mode); | |
3670 | } | |
3671 | ||
a218d0fd | 3672 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
5590ff0d | 3673 | { |
0101db7a | 3674 | return do_mkdirat(AT_FDCWD, pathname, mode); |
5590ff0d UD |
3675 | } |
3676 | ||
1da177e4 LT |
3677 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) |
3678 | { | |
3679 | int error = may_delete(dir, dentry, 1); | |
3680 | ||
3681 | if (error) | |
3682 | return error; | |
3683 | ||
acfa4380 | 3684 | if (!dir->i_op->rmdir) |
1da177e4 LT |
3685 | return -EPERM; |
3686 | ||
1d2ef590 | 3687 | dget(dentry); |
5955102c | 3688 | inode_lock(dentry->d_inode); |
912dbc15 SW |
3689 | |
3690 | error = -EBUSY; | |
7af1364f | 3691 | if (is_local_mountpoint(dentry)) |
912dbc15 SW |
3692 | goto out; |
3693 | ||
3694 | error = security_inode_rmdir(dir, dentry); | |
3695 | if (error) | |
3696 | goto out; | |
3697 | ||
3698 | error = dir->i_op->rmdir(dir, dentry); | |
3699 | if (error) | |
3700 | goto out; | |
3701 | ||
8767712f | 3702 | shrink_dcache_parent(dentry); |
912dbc15 SW |
3703 | dentry->d_inode->i_flags |= S_DEAD; |
3704 | dont_mount(dentry); | |
8ed936b5 | 3705 | detach_mounts(dentry); |
116b9731 | 3706 | fsnotify_rmdir(dir, dentry); |
912dbc15 SW |
3707 | |
3708 | out: | |
5955102c | 3709 | inode_unlock(dentry->d_inode); |
1d2ef590 | 3710 | dput(dentry); |
912dbc15 | 3711 | if (!error) |
1da177e4 | 3712 | d_delete(dentry); |
1da177e4 LT |
3713 | return error; |
3714 | } | |
4d359507 | 3715 | EXPORT_SYMBOL(vfs_rmdir); |
1da177e4 | 3716 | |
f459dffa | 3717 | long do_rmdir(int dfd, const char __user *pathname) |
1da177e4 LT |
3718 | { |
3719 | int error = 0; | |
91a27b2a | 3720 | struct filename *name; |
1da177e4 | 3721 | struct dentry *dentry; |
f5beed75 AV |
3722 | struct path path; |
3723 | struct qstr last; | |
3724 | int type; | |
c6ee9206 JL |
3725 | unsigned int lookup_flags = 0; |
3726 | retry: | |
c1d4dd27 AV |
3727 | name = filename_parentat(dfd, getname(pathname), lookup_flags, |
3728 | &path, &last, &type); | |
91a27b2a JL |
3729 | if (IS_ERR(name)) |
3730 | return PTR_ERR(name); | |
1da177e4 | 3731 | |
f5beed75 | 3732 | switch (type) { |
0612d9fb OH |
3733 | case LAST_DOTDOT: |
3734 | error = -ENOTEMPTY; | |
3735 | goto exit1; | |
3736 | case LAST_DOT: | |
3737 | error = -EINVAL; | |
3738 | goto exit1; | |
3739 | case LAST_ROOT: | |
3740 | error = -EBUSY; | |
3741 | goto exit1; | |
1da177e4 | 3742 | } |
0612d9fb | 3743 | |
f5beed75 | 3744 | error = mnt_want_write(path.mnt); |
c30dabfe JK |
3745 | if (error) |
3746 | goto exit1; | |
0612d9fb | 3747 | |
5955102c | 3748 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
f5beed75 | 3749 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
1da177e4 | 3750 | error = PTR_ERR(dentry); |
6902d925 DH |
3751 | if (IS_ERR(dentry)) |
3752 | goto exit2; | |
e6bc45d6 TT |
3753 | if (!dentry->d_inode) { |
3754 | error = -ENOENT; | |
3755 | goto exit3; | |
3756 | } | |
f5beed75 | 3757 | error = security_path_rmdir(&path, dentry); |
be6d3e56 | 3758 | if (error) |
c30dabfe | 3759 | goto exit3; |
f5beed75 | 3760 | error = vfs_rmdir(path.dentry->d_inode, dentry); |
0622753b | 3761 | exit3: |
6902d925 DH |
3762 | dput(dentry); |
3763 | exit2: | |
5955102c | 3764 | inode_unlock(path.dentry->d_inode); |
f5beed75 | 3765 | mnt_drop_write(path.mnt); |
1da177e4 | 3766 | exit1: |
f5beed75 | 3767 | path_put(&path); |
1da177e4 | 3768 | putname(name); |
c6ee9206 JL |
3769 | if (retry_estale(error, lookup_flags)) { |
3770 | lookup_flags |= LOOKUP_REVAL; | |
3771 | goto retry; | |
3772 | } | |
1da177e4 LT |
3773 | return error; |
3774 | } | |
3775 | ||
3cdad428 | 3776 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
5590ff0d UD |
3777 | { |
3778 | return do_rmdir(AT_FDCWD, pathname); | |
3779 | } | |
3780 | ||
b21996e3 BF |
3781 | /** |
3782 | * vfs_unlink - unlink a filesystem object | |
3783 | * @dir: parent directory | |
3784 | * @dentry: victim | |
3785 | * @delegated_inode: returns victim inode, if the inode is delegated. | |
3786 | * | |
3787 | * The caller must hold dir->i_mutex. | |
3788 | * | |
3789 | * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and | |
3790 | * return a reference to the inode in delegated_inode. The caller | |
3791 | * should then break the delegation on that inode and retry. Because | |
3792 | * breaking a delegation may take a long time, the caller should drop | |
3793 | * dir->i_mutex before doing so. | |
3794 | * | |
3795 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
3796 | * be appropriate for callers that expect the underlying filesystem not | |
3797 | * to be NFS exported. | |
3798 | */ | |
3799 | int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode) | |
1da177e4 | 3800 | { |
9accbb97 | 3801 | struct inode *target = dentry->d_inode; |
1da177e4 LT |
3802 | int error = may_delete(dir, dentry, 0); |
3803 | ||
3804 | if (error) | |
3805 | return error; | |
3806 | ||
acfa4380 | 3807 | if (!dir->i_op->unlink) |
1da177e4 LT |
3808 | return -EPERM; |
3809 | ||
5955102c | 3810 | inode_lock(target); |
8ed936b5 | 3811 | if (is_local_mountpoint(dentry)) |
1da177e4 LT |
3812 | error = -EBUSY; |
3813 | else { | |
3814 | error = security_inode_unlink(dir, dentry); | |
bec1052e | 3815 | if (!error) { |
5a14696c BF |
3816 | error = try_break_deleg(target, delegated_inode); |
3817 | if (error) | |
b21996e3 | 3818 | goto out; |
1da177e4 | 3819 | error = dir->i_op->unlink(dir, dentry); |
8ed936b5 | 3820 | if (!error) { |
d83c49f3 | 3821 | dont_mount(dentry); |
8ed936b5 | 3822 | detach_mounts(dentry); |
116b9731 | 3823 | fsnotify_unlink(dir, dentry); |
8ed936b5 | 3824 | } |
bec1052e | 3825 | } |
1da177e4 | 3826 | } |
b21996e3 | 3827 | out: |
5955102c | 3828 | inode_unlock(target); |
1da177e4 LT |
3829 | |
3830 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ | |
3831 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { | |
9accbb97 | 3832 | fsnotify_link_count(target); |
e234f35c | 3833 | d_delete(dentry); |
1da177e4 | 3834 | } |
0eeca283 | 3835 | |
1da177e4 LT |
3836 | return error; |
3837 | } | |
4d359507 | 3838 | EXPORT_SYMBOL(vfs_unlink); |
1da177e4 LT |
3839 | |
3840 | /* | |
3841 | * Make sure that the actual truncation of the file will occur outside its | |
1b1dcc1b | 3842 | * directory's i_mutex. Truncate can take a long time if there is a lot of |
1da177e4 LT |
3843 | * writeout happening, and we don't want to prevent access to the directory |
3844 | * while waiting on the I/O. | |
3845 | */ | |
da2f1362 | 3846 | long do_unlinkat(int dfd, struct filename *name) |
1da177e4 | 3847 | { |
2ad94ae6 | 3848 | int error; |
1da177e4 | 3849 | struct dentry *dentry; |
f5beed75 AV |
3850 | struct path path; |
3851 | struct qstr last; | |
3852 | int type; | |
1da177e4 | 3853 | struct inode *inode = NULL; |
b21996e3 | 3854 | struct inode *delegated_inode = NULL; |
5d18f813 JL |
3855 | unsigned int lookup_flags = 0; |
3856 | retry: | |
da2f1362 | 3857 | name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
91a27b2a JL |
3858 | if (IS_ERR(name)) |
3859 | return PTR_ERR(name); | |
2ad94ae6 | 3860 | |
1da177e4 | 3861 | error = -EISDIR; |
f5beed75 | 3862 | if (type != LAST_NORM) |
1da177e4 | 3863 | goto exit1; |
0612d9fb | 3864 | |
f5beed75 | 3865 | error = mnt_want_write(path.mnt); |
c30dabfe JK |
3866 | if (error) |
3867 | goto exit1; | |
b21996e3 | 3868 | retry_deleg: |
5955102c | 3869 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
f5beed75 | 3870 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); |
1da177e4 LT |
3871 | error = PTR_ERR(dentry); |
3872 | if (!IS_ERR(dentry)) { | |
3873 | /* Why not before? Because we want correct error value */ | |
f5beed75 | 3874 | if (last.name[last.len]) |
50338b88 | 3875 | goto slashes; |
1da177e4 | 3876 | inode = dentry->d_inode; |
b18825a7 | 3877 | if (d_is_negative(dentry)) |
e6bc45d6 TT |
3878 | goto slashes; |
3879 | ihold(inode); | |
f5beed75 | 3880 | error = security_path_unlink(&path, dentry); |
be6d3e56 | 3881 | if (error) |
c30dabfe | 3882 | goto exit2; |
f5beed75 | 3883 | error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode); |
c30dabfe | 3884 | exit2: |
1da177e4 LT |
3885 | dput(dentry); |
3886 | } | |
5955102c | 3887 | inode_unlock(path.dentry->d_inode); |
1da177e4 LT |
3888 | if (inode) |
3889 | iput(inode); /* truncate the inode here */ | |
b21996e3 BF |
3890 | inode = NULL; |
3891 | if (delegated_inode) { | |
5a14696c | 3892 | error = break_deleg_wait(&delegated_inode); |
b21996e3 BF |
3893 | if (!error) |
3894 | goto retry_deleg; | |
3895 | } | |
f5beed75 | 3896 | mnt_drop_write(path.mnt); |
1da177e4 | 3897 | exit1: |
f5beed75 | 3898 | path_put(&path); |
5d18f813 JL |
3899 | if (retry_estale(error, lookup_flags)) { |
3900 | lookup_flags |= LOOKUP_REVAL; | |
3901 | inode = NULL; | |
3902 | goto retry; | |
3903 | } | |
da2f1362 | 3904 | putname(name); |
1da177e4 LT |
3905 | return error; |
3906 | ||
3907 | slashes: | |
b18825a7 DH |
3908 | if (d_is_negative(dentry)) |
3909 | error = -ENOENT; | |
44b1d530 | 3910 | else if (d_is_dir(dentry)) |
b18825a7 DH |
3911 | error = -EISDIR; |
3912 | else | |
3913 | error = -ENOTDIR; | |
1da177e4 LT |
3914 | goto exit2; |
3915 | } | |
3916 | ||
2e4d0924 | 3917 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
5590ff0d UD |
3918 | { |
3919 | if ((flag & ~AT_REMOVEDIR) != 0) | |
3920 | return -EINVAL; | |
3921 | ||
3922 | if (flag & AT_REMOVEDIR) | |
3923 | return do_rmdir(dfd, pathname); | |
3924 | ||
da2f1362 | 3925 | return do_unlinkat(dfd, getname(pathname)); |
5590ff0d UD |
3926 | } |
3927 | ||
3480b257 | 3928 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
5590ff0d | 3929 | { |
da2f1362 | 3930 | return do_unlinkat(AT_FDCWD, getname(pathname)); |
5590ff0d UD |
3931 | } |
3932 | ||
db2e747b | 3933 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) |
1da177e4 | 3934 | { |
a95164d9 | 3935 | int error = may_create(dir, dentry); |
1da177e4 LT |
3936 | |
3937 | if (error) | |
3938 | return error; | |
3939 | ||
acfa4380 | 3940 | if (!dir->i_op->symlink) |
1da177e4 LT |
3941 | return -EPERM; |
3942 | ||
3943 | error = security_inode_symlink(dir, dentry, oldname); | |
3944 | if (error) | |
3945 | return error; | |
3946 | ||
1da177e4 | 3947 | error = dir->i_op->symlink(dir, dentry, oldname); |
a74574aa | 3948 | if (!error) |
f38aa942 | 3949 | fsnotify_create(dir, dentry); |
1da177e4 LT |
3950 | return error; |
3951 | } | |
4d359507 | 3952 | EXPORT_SYMBOL(vfs_symlink); |
1da177e4 | 3953 | |
b724e846 DB |
3954 | long do_symlinkat(const char __user *oldname, int newdfd, |
3955 | const char __user *newname) | |
1da177e4 | 3956 | { |
2ad94ae6 | 3957 | int error; |
91a27b2a | 3958 | struct filename *from; |
6902d925 | 3959 | struct dentry *dentry; |
dae6ad8f | 3960 | struct path path; |
f46d3567 | 3961 | unsigned int lookup_flags = 0; |
1da177e4 LT |
3962 | |
3963 | from = getname(oldname); | |
2ad94ae6 | 3964 | if (IS_ERR(from)) |
1da177e4 | 3965 | return PTR_ERR(from); |
f46d3567 JL |
3966 | retry: |
3967 | dentry = user_path_create(newdfd, newname, &path, lookup_flags); | |
6902d925 DH |
3968 | error = PTR_ERR(dentry); |
3969 | if (IS_ERR(dentry)) | |
dae6ad8f | 3970 | goto out_putname; |
6902d925 | 3971 | |
91a27b2a | 3972 | error = security_path_symlink(&path, dentry, from->name); |
a8104a9f | 3973 | if (!error) |
91a27b2a | 3974 | error = vfs_symlink(path.dentry->d_inode, dentry, from->name); |
921a1650 | 3975 | done_path_create(&path, dentry); |
f46d3567 JL |
3976 | if (retry_estale(error, lookup_flags)) { |
3977 | lookup_flags |= LOOKUP_REVAL; | |
3978 | goto retry; | |
3979 | } | |
6902d925 | 3980 | out_putname: |
1da177e4 LT |
3981 | putname(from); |
3982 | return error; | |
3983 | } | |
3984 | ||
b724e846 DB |
3985 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
3986 | int, newdfd, const char __user *, newname) | |
3987 | { | |
3988 | return do_symlinkat(oldname, newdfd, newname); | |
3989 | } | |
3990 | ||
3480b257 | 3991 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 3992 | { |
b724e846 | 3993 | return do_symlinkat(oldname, AT_FDCWD, newname); |
5590ff0d UD |
3994 | } |
3995 | ||
146a8595 BF |
3996 | /** |
3997 | * vfs_link - create a new link | |
3998 | * @old_dentry: object to be linked | |
3999 | * @dir: new parent | |
4000 | * @new_dentry: where to create the new link | |
4001 | * @delegated_inode: returns inode needing a delegation break | |
4002 | * | |
4003 | * The caller must hold dir->i_mutex | |
4004 | * | |
4005 | * If vfs_link discovers a delegation on the to-be-linked file in need | |
4006 | * of breaking, it will return -EWOULDBLOCK and return a reference to the | |
4007 | * inode in delegated_inode. The caller should then break the delegation | |
4008 | * and retry. Because breaking a delegation may take a long time, the | |
4009 | * caller should drop the i_mutex before doing so. | |
4010 | * | |
4011 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4012 | * be appropriate for callers that expect the underlying filesystem not | |
4013 | * to be NFS exported. | |
4014 | */ | |
4015 | int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode) | |
1da177e4 LT |
4016 | { |
4017 | struct inode *inode = old_dentry->d_inode; | |
8de52778 | 4018 | unsigned max_links = dir->i_sb->s_max_links; |
1da177e4 LT |
4019 | int error; |
4020 | ||
4021 | if (!inode) | |
4022 | return -ENOENT; | |
4023 | ||
a95164d9 | 4024 | error = may_create(dir, new_dentry); |
1da177e4 LT |
4025 | if (error) |
4026 | return error; | |
4027 | ||
4028 | if (dir->i_sb != inode->i_sb) | |
4029 | return -EXDEV; | |
4030 | ||
4031 | /* | |
4032 | * A link to an append-only or immutable file cannot be created. | |
4033 | */ | |
4034 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
4035 | return -EPERM; | |
0bd23d09 EB |
4036 | /* |
4037 | * Updating the link count will likely cause i_uid and i_gid to | |
4038 | * be writen back improperly if their true value is unknown to | |
4039 | * the vfs. | |
4040 | */ | |
4041 | if (HAS_UNMAPPED_ID(inode)) | |
4042 | return -EPERM; | |
acfa4380 | 4043 | if (!dir->i_op->link) |
1da177e4 | 4044 | return -EPERM; |
7e79eedb | 4045 | if (S_ISDIR(inode->i_mode)) |
1da177e4 LT |
4046 | return -EPERM; |
4047 | ||
4048 | error = security_inode_link(old_dentry, dir, new_dentry); | |
4049 | if (error) | |
4050 | return error; | |
4051 | ||
5955102c | 4052 | inode_lock(inode); |
aae8a97d | 4053 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
f4e0c30c | 4054 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
aae8a97d | 4055 | error = -ENOENT; |
8de52778 AV |
4056 | else if (max_links && inode->i_nlink >= max_links) |
4057 | error = -EMLINK; | |
146a8595 BF |
4058 | else { |
4059 | error = try_break_deleg(inode, delegated_inode); | |
4060 | if (!error) | |
4061 | error = dir->i_op->link(old_dentry, dir, new_dentry); | |
4062 | } | |
f4e0c30c AV |
4063 | |
4064 | if (!error && (inode->i_state & I_LINKABLE)) { | |
4065 | spin_lock(&inode->i_lock); | |
4066 | inode->i_state &= ~I_LINKABLE; | |
4067 | spin_unlock(&inode->i_lock); | |
4068 | } | |
5955102c | 4069 | inode_unlock(inode); |
e31e14ec | 4070 | if (!error) |
7e79eedb | 4071 | fsnotify_link(dir, inode, new_dentry); |
1da177e4 LT |
4072 | return error; |
4073 | } | |
4d359507 | 4074 | EXPORT_SYMBOL(vfs_link); |
1da177e4 LT |
4075 | |
4076 | /* | |
4077 | * Hardlinks are often used in delicate situations. We avoid | |
4078 | * security-related surprises by not following symlinks on the | |
4079 | * newname. --KAB | |
4080 | * | |
4081 | * We don't follow them on the oldname either to be compatible | |
4082 | * with linux 2.0, and to avoid hard-linking to directories | |
4083 | * and other special files. --ADM | |
4084 | */ | |
46ea89eb DB |
4085 | int do_linkat(int olddfd, const char __user *oldname, int newdfd, |
4086 | const char __user *newname, int flags) | |
1da177e4 LT |
4087 | { |
4088 | struct dentry *new_dentry; | |
dae6ad8f | 4089 | struct path old_path, new_path; |
146a8595 | 4090 | struct inode *delegated_inode = NULL; |
11a7b371 | 4091 | int how = 0; |
1da177e4 | 4092 | int error; |
1da177e4 | 4093 | |
11a7b371 | 4094 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) |
c04030e1 | 4095 | return -EINVAL; |
11a7b371 | 4096 | /* |
f0cc6ffb LT |
4097 | * To use null names we require CAP_DAC_READ_SEARCH |
4098 | * This ensures that not everyone will be able to create | |
4099 | * handlink using the passed filedescriptor. | |
11a7b371 | 4100 | */ |
f0cc6ffb LT |
4101 | if (flags & AT_EMPTY_PATH) { |
4102 | if (!capable(CAP_DAC_READ_SEARCH)) | |
4103 | return -ENOENT; | |
11a7b371 | 4104 | how = LOOKUP_EMPTY; |
f0cc6ffb | 4105 | } |
11a7b371 AK |
4106 | |
4107 | if (flags & AT_SYMLINK_FOLLOW) | |
4108 | how |= LOOKUP_FOLLOW; | |
442e31ca | 4109 | retry: |
11a7b371 | 4110 | error = user_path_at(olddfd, oldname, how, &old_path); |
1da177e4 | 4111 | if (error) |
2ad94ae6 AV |
4112 | return error; |
4113 | ||
442e31ca JL |
4114 | new_dentry = user_path_create(newdfd, newname, &new_path, |
4115 | (how & LOOKUP_REVAL)); | |
1da177e4 | 4116 | error = PTR_ERR(new_dentry); |
6902d925 | 4117 | if (IS_ERR(new_dentry)) |
dae6ad8f AV |
4118 | goto out; |
4119 | ||
4120 | error = -EXDEV; | |
4121 | if (old_path.mnt != new_path.mnt) | |
4122 | goto out_dput; | |
800179c9 KC |
4123 | error = may_linkat(&old_path); |
4124 | if (unlikely(error)) | |
4125 | goto out_dput; | |
dae6ad8f | 4126 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
be6d3e56 | 4127 | if (error) |
a8104a9f | 4128 | goto out_dput; |
146a8595 | 4129 | error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode); |
75c3f29d | 4130 | out_dput: |
921a1650 | 4131 | done_path_create(&new_path, new_dentry); |
146a8595 BF |
4132 | if (delegated_inode) { |
4133 | error = break_deleg_wait(&delegated_inode); | |
d22e6338 OD |
4134 | if (!error) { |
4135 | path_put(&old_path); | |
146a8595 | 4136 | goto retry; |
d22e6338 | 4137 | } |
146a8595 | 4138 | } |
442e31ca | 4139 | if (retry_estale(error, how)) { |
d22e6338 | 4140 | path_put(&old_path); |
442e31ca JL |
4141 | how |= LOOKUP_REVAL; |
4142 | goto retry; | |
4143 | } | |
1da177e4 | 4144 | out: |
2d8f3038 | 4145 | path_put(&old_path); |
1da177e4 LT |
4146 | |
4147 | return error; | |
4148 | } | |
4149 | ||
46ea89eb DB |
4150 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
4151 | int, newdfd, const char __user *, newname, int, flags) | |
4152 | { | |
4153 | return do_linkat(olddfd, oldname, newdfd, newname, flags); | |
4154 | } | |
4155 | ||
3480b257 | 4156 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4157 | { |
46ea89eb | 4158 | return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
5590ff0d UD |
4159 | } |
4160 | ||
bc27027a MS |
4161 | /** |
4162 | * vfs_rename - rename a filesystem object | |
4163 | * @old_dir: parent of source | |
4164 | * @old_dentry: source | |
4165 | * @new_dir: parent of destination | |
4166 | * @new_dentry: destination | |
4167 | * @delegated_inode: returns an inode needing a delegation break | |
520c8b16 | 4168 | * @flags: rename flags |
bc27027a MS |
4169 | * |
4170 | * The caller must hold multiple mutexes--see lock_rename()). | |
4171 | * | |
4172 | * If vfs_rename discovers a delegation in need of breaking at either | |
4173 | * the source or destination, it will return -EWOULDBLOCK and return a | |
4174 | * reference to the inode in delegated_inode. The caller should then | |
4175 | * break the delegation and retry. Because breaking a delegation may | |
4176 | * take a long time, the caller should drop all locks before doing | |
4177 | * so. | |
4178 | * | |
4179 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4180 | * be appropriate for callers that expect the underlying filesystem not | |
4181 | * to be NFS exported. | |
4182 | * | |
1da177e4 LT |
4183 | * The worst of all namespace operations - renaming directory. "Perverted" |
4184 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... | |
4185 | * Problems: | |
0117d427 | 4186 | * |
d03b29a2 | 4187 | * a) we can get into loop creation. |
1da177e4 LT |
4188 | * b) race potential - two innocent renames can create a loop together. |
4189 | * That's where 4.4 screws up. Current fix: serialization on | |
a11f3a05 | 4190 | * sb->s_vfs_rename_mutex. We might be more accurate, but that's another |
1da177e4 | 4191 | * story. |
6cedba89 BF |
4192 | * c) we have to lock _four_ objects - parents and victim (if it exists), |
4193 | * and source (if it is not a directory). | |
1b1dcc1b | 4194 | * And that - after we got ->i_mutex on parents (until then we don't know |
1da177e4 LT |
4195 | * whether the target exists). Solution: try to be smart with locking |
4196 | * order for inodes. We rely on the fact that tree topology may change | |
a11f3a05 | 4197 | * only under ->s_vfs_rename_mutex _and_ that parent of the object we |
1da177e4 LT |
4198 | * move will be locked. Thus we can rank directories by the tree |
4199 | * (ancestors first) and rank all non-directories after them. | |
4200 | * That works since everybody except rename does "lock parent, lookup, | |
a11f3a05 | 4201 | * lock child" and rename is under ->s_vfs_rename_mutex. |
1da177e4 LT |
4202 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
4203 | * has no more than 1 dentry. If "hybrid" objects will ever appear, | |
4204 | * we'd better make sure that there's no link(2) for them. | |
e4eaac06 | 4205 | * d) conversion from fhandle to dentry may come in the wrong moment - when |
1b1dcc1b | 4206 | * we are removing the target. Solution: we will have to grab ->i_mutex |
1da177e4 | 4207 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
c41b20e7 | 4208 | * ->i_mutex on parents, which works but leads to some truly excessive |
1da177e4 LT |
4209 | * locking]. |
4210 | */ | |
bc27027a MS |
4211 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
4212 | struct inode *new_dir, struct dentry *new_dentry, | |
520c8b16 | 4213 | struct inode **delegated_inode, unsigned int flags) |
1da177e4 | 4214 | { |
bc27027a MS |
4215 | int error; |
4216 | bool is_dir = d_is_dir(old_dentry); | |
bc27027a | 4217 | struct inode *source = old_dentry->d_inode; |
9055cba7 | 4218 | struct inode *target = new_dentry->d_inode; |
da1ce067 MS |
4219 | bool new_is_dir = false; |
4220 | unsigned max_links = new_dir->i_sb->s_max_links; | |
49d31c2f | 4221 | struct name_snapshot old_name; |
bc27027a | 4222 | |
8d3e2936 | 4223 | if (source == target) |
bc27027a MS |
4224 | return 0; |
4225 | ||
4226 | error = may_delete(old_dir, old_dentry, is_dir); | |
4227 | if (error) | |
4228 | return error; | |
4229 | ||
da1ce067 | 4230 | if (!target) { |
bc27027a | 4231 | error = may_create(new_dir, new_dentry); |
da1ce067 MS |
4232 | } else { |
4233 | new_is_dir = d_is_dir(new_dentry); | |
4234 | ||
4235 | if (!(flags & RENAME_EXCHANGE)) | |
4236 | error = may_delete(new_dir, new_dentry, is_dir); | |
4237 | else | |
4238 | error = may_delete(new_dir, new_dentry, new_is_dir); | |
4239 | } | |
bc27027a MS |
4240 | if (error) |
4241 | return error; | |
4242 | ||
2773bf00 | 4243 | if (!old_dir->i_op->rename) |
bc27027a | 4244 | return -EPERM; |
1da177e4 LT |
4245 | |
4246 | /* | |
4247 | * If we are going to change the parent - check write permissions, | |
4248 | * we'll need to flip '..'. | |
4249 | */ | |
da1ce067 MS |
4250 | if (new_dir != old_dir) { |
4251 | if (is_dir) { | |
4252 | error = inode_permission(source, MAY_WRITE); | |
4253 | if (error) | |
4254 | return error; | |
4255 | } | |
4256 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { | |
4257 | error = inode_permission(target, MAY_WRITE); | |
4258 | if (error) | |
4259 | return error; | |
4260 | } | |
1da177e4 LT |
4261 | } |
4262 | ||
0b3974eb MS |
4263 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, |
4264 | flags); | |
1da177e4 LT |
4265 | if (error) |
4266 | return error; | |
4267 | ||
49d31c2f | 4268 | take_dentry_name_snapshot(&old_name, old_dentry); |
1d2ef590 | 4269 | dget(new_dentry); |
da1ce067 | 4270 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
bc27027a MS |
4271 | lock_two_nondirectories(source, target); |
4272 | else if (target) | |
5955102c | 4273 | inode_lock(target); |
9055cba7 SW |
4274 | |
4275 | error = -EBUSY; | |
7af1364f | 4276 | if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) |
9055cba7 SW |
4277 | goto out; |
4278 | ||
da1ce067 | 4279 | if (max_links && new_dir != old_dir) { |
bc27027a | 4280 | error = -EMLINK; |
da1ce067 | 4281 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) |
bc27027a | 4282 | goto out; |
da1ce067 MS |
4283 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && |
4284 | old_dir->i_nlink >= max_links) | |
4285 | goto out; | |
4286 | } | |
da1ce067 | 4287 | if (!is_dir) { |
bc27027a | 4288 | error = try_break_deleg(source, delegated_inode); |
8e6d782c BF |
4289 | if (error) |
4290 | goto out; | |
da1ce067 MS |
4291 | } |
4292 | if (target && !new_is_dir) { | |
4293 | error = try_break_deleg(target, delegated_inode); | |
4294 | if (error) | |
4295 | goto out; | |
8e6d782c | 4296 | } |
2773bf00 | 4297 | error = old_dir->i_op->rename(old_dir, old_dentry, |
18fc84da | 4298 | new_dir, new_dentry, flags); |
51892bbb SW |
4299 | if (error) |
4300 | goto out; | |
4301 | ||
da1ce067 | 4302 | if (!(flags & RENAME_EXCHANGE) && target) { |
8767712f AV |
4303 | if (is_dir) { |
4304 | shrink_dcache_parent(new_dentry); | |
bc27027a | 4305 | target->i_flags |= S_DEAD; |
8767712f | 4306 | } |
51892bbb | 4307 | dont_mount(new_dentry); |
8ed936b5 | 4308 | detach_mounts(new_dentry); |
bc27027a | 4309 | } |
da1ce067 MS |
4310 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { |
4311 | if (!(flags & RENAME_EXCHANGE)) | |
4312 | d_move(old_dentry, new_dentry); | |
4313 | else | |
4314 | d_exchange(old_dentry, new_dentry); | |
4315 | } | |
51892bbb | 4316 | out: |
da1ce067 | 4317 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
bc27027a MS |
4318 | unlock_two_nondirectories(source, target); |
4319 | else if (target) | |
5955102c | 4320 | inode_unlock(target); |
1da177e4 | 4321 | dput(new_dentry); |
da1ce067 | 4322 | if (!error) { |
f4ec3a3d | 4323 | fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, |
da1ce067 MS |
4324 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); |
4325 | if (flags & RENAME_EXCHANGE) { | |
f4ec3a3d | 4326 | fsnotify_move(new_dir, old_dir, &old_dentry->d_name, |
da1ce067 MS |
4327 | new_is_dir, NULL, new_dentry); |
4328 | } | |
4329 | } | |
49d31c2f | 4330 | release_dentry_name_snapshot(&old_name); |
0eeca283 | 4331 | |
1da177e4 LT |
4332 | return error; |
4333 | } | |
4d359507 | 4334 | EXPORT_SYMBOL(vfs_rename); |
1da177e4 | 4335 | |
ee81feb6 DB |
4336 | static int do_renameat2(int olddfd, const char __user *oldname, int newdfd, |
4337 | const char __user *newname, unsigned int flags) | |
1da177e4 | 4338 | { |
2ad94ae6 AV |
4339 | struct dentry *old_dentry, *new_dentry; |
4340 | struct dentry *trap; | |
f5beed75 AV |
4341 | struct path old_path, new_path; |
4342 | struct qstr old_last, new_last; | |
4343 | int old_type, new_type; | |
8e6d782c | 4344 | struct inode *delegated_inode = NULL; |
91a27b2a JL |
4345 | struct filename *from; |
4346 | struct filename *to; | |
f5beed75 | 4347 | unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; |
c6a94284 | 4348 | bool should_retry = false; |
2ad94ae6 | 4349 | int error; |
520c8b16 | 4350 | |
0d7a8555 | 4351 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
da1ce067 MS |
4352 | return -EINVAL; |
4353 | ||
0d7a8555 MS |
4354 | if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && |
4355 | (flags & RENAME_EXCHANGE)) | |
520c8b16 MS |
4356 | return -EINVAL; |
4357 | ||
0d7a8555 MS |
4358 | if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD)) |
4359 | return -EPERM; | |
4360 | ||
f5beed75 AV |
4361 | if (flags & RENAME_EXCHANGE) |
4362 | target_flags = 0; | |
4363 | ||
c6a94284 | 4364 | retry: |
c1d4dd27 AV |
4365 | from = filename_parentat(olddfd, getname(oldname), lookup_flags, |
4366 | &old_path, &old_last, &old_type); | |
91a27b2a JL |
4367 | if (IS_ERR(from)) { |
4368 | error = PTR_ERR(from); | |
1da177e4 | 4369 | goto exit; |
91a27b2a | 4370 | } |
1da177e4 | 4371 | |
c1d4dd27 AV |
4372 | to = filename_parentat(newdfd, getname(newname), lookup_flags, |
4373 | &new_path, &new_last, &new_type); | |
91a27b2a JL |
4374 | if (IS_ERR(to)) { |
4375 | error = PTR_ERR(to); | |
1da177e4 | 4376 | goto exit1; |
91a27b2a | 4377 | } |
1da177e4 LT |
4378 | |
4379 | error = -EXDEV; | |
f5beed75 | 4380 | if (old_path.mnt != new_path.mnt) |
1da177e4 LT |
4381 | goto exit2; |
4382 | ||
1da177e4 | 4383 | error = -EBUSY; |
f5beed75 | 4384 | if (old_type != LAST_NORM) |
1da177e4 LT |
4385 | goto exit2; |
4386 | ||
0a7c3937 MS |
4387 | if (flags & RENAME_NOREPLACE) |
4388 | error = -EEXIST; | |
f5beed75 | 4389 | if (new_type != LAST_NORM) |
1da177e4 LT |
4390 | goto exit2; |
4391 | ||
f5beed75 | 4392 | error = mnt_want_write(old_path.mnt); |
c30dabfe JK |
4393 | if (error) |
4394 | goto exit2; | |
4395 | ||
8e6d782c | 4396 | retry_deleg: |
f5beed75 | 4397 | trap = lock_rename(new_path.dentry, old_path.dentry); |
1da177e4 | 4398 | |
f5beed75 | 4399 | old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); |
1da177e4 LT |
4400 | error = PTR_ERR(old_dentry); |
4401 | if (IS_ERR(old_dentry)) | |
4402 | goto exit3; | |
4403 | /* source must exist */ | |
4404 | error = -ENOENT; | |
b18825a7 | 4405 | if (d_is_negative(old_dentry)) |
1da177e4 | 4406 | goto exit4; |
f5beed75 | 4407 | new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags); |
0a7c3937 MS |
4408 | error = PTR_ERR(new_dentry); |
4409 | if (IS_ERR(new_dentry)) | |
4410 | goto exit4; | |
4411 | error = -EEXIST; | |
4412 | if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) | |
4413 | goto exit5; | |
da1ce067 MS |
4414 | if (flags & RENAME_EXCHANGE) { |
4415 | error = -ENOENT; | |
4416 | if (d_is_negative(new_dentry)) | |
4417 | goto exit5; | |
4418 | ||
4419 | if (!d_is_dir(new_dentry)) { | |
4420 | error = -ENOTDIR; | |
f5beed75 | 4421 | if (new_last.name[new_last.len]) |
da1ce067 MS |
4422 | goto exit5; |
4423 | } | |
4424 | } | |
1da177e4 | 4425 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
44b1d530 | 4426 | if (!d_is_dir(old_dentry)) { |
1da177e4 | 4427 | error = -ENOTDIR; |
f5beed75 | 4428 | if (old_last.name[old_last.len]) |
0a7c3937 | 4429 | goto exit5; |
f5beed75 | 4430 | if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) |
0a7c3937 | 4431 | goto exit5; |
1da177e4 LT |
4432 | } |
4433 | /* source should not be ancestor of target */ | |
4434 | error = -EINVAL; | |
4435 | if (old_dentry == trap) | |
0a7c3937 | 4436 | goto exit5; |
1da177e4 | 4437 | /* target should not be an ancestor of source */ |
da1ce067 MS |
4438 | if (!(flags & RENAME_EXCHANGE)) |
4439 | error = -ENOTEMPTY; | |
1da177e4 LT |
4440 | if (new_dentry == trap) |
4441 | goto exit5; | |
4442 | ||
f5beed75 AV |
4443 | error = security_path_rename(&old_path, old_dentry, |
4444 | &new_path, new_dentry, flags); | |
be6d3e56 | 4445 | if (error) |
c30dabfe | 4446 | goto exit5; |
f5beed75 AV |
4447 | error = vfs_rename(old_path.dentry->d_inode, old_dentry, |
4448 | new_path.dentry->d_inode, new_dentry, | |
520c8b16 | 4449 | &delegated_inode, flags); |
1da177e4 LT |
4450 | exit5: |
4451 | dput(new_dentry); | |
4452 | exit4: | |
4453 | dput(old_dentry); | |
4454 | exit3: | |
f5beed75 | 4455 | unlock_rename(new_path.dentry, old_path.dentry); |
8e6d782c BF |
4456 | if (delegated_inode) { |
4457 | error = break_deleg_wait(&delegated_inode); | |
4458 | if (!error) | |
4459 | goto retry_deleg; | |
4460 | } | |
f5beed75 | 4461 | mnt_drop_write(old_path.mnt); |
1da177e4 | 4462 | exit2: |
c6a94284 JL |
4463 | if (retry_estale(error, lookup_flags)) |
4464 | should_retry = true; | |
f5beed75 | 4465 | path_put(&new_path); |
2ad94ae6 | 4466 | putname(to); |
1da177e4 | 4467 | exit1: |
f5beed75 | 4468 | path_put(&old_path); |
1da177e4 | 4469 | putname(from); |
c6a94284 JL |
4470 | if (should_retry) { |
4471 | should_retry = false; | |
4472 | lookup_flags |= LOOKUP_REVAL; | |
4473 | goto retry; | |
4474 | } | |
2ad94ae6 | 4475 | exit: |
1da177e4 LT |
4476 | return error; |
4477 | } | |
4478 | ||
ee81feb6 DB |
4479 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, |
4480 | int, newdfd, const char __user *, newname, unsigned int, flags) | |
4481 | { | |
4482 | return do_renameat2(olddfd, oldname, newdfd, newname, flags); | |
4483 | } | |
4484 | ||
520c8b16 MS |
4485 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
4486 | int, newdfd, const char __user *, newname) | |
4487 | { | |
ee81feb6 | 4488 | return do_renameat2(olddfd, oldname, newdfd, newname, 0); |
520c8b16 MS |
4489 | } |
4490 | ||
a26eab24 | 4491 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4492 | { |
ee81feb6 | 4493 | return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
5590ff0d UD |
4494 | } |
4495 | ||
787fb6bc MS |
4496 | int vfs_whiteout(struct inode *dir, struct dentry *dentry) |
4497 | { | |
4498 | int error = may_create(dir, dentry); | |
4499 | if (error) | |
4500 | return error; | |
4501 | ||
4502 | if (!dir->i_op->mknod) | |
4503 | return -EPERM; | |
4504 | ||
4505 | return dir->i_op->mknod(dir, dentry, | |
4506 | S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); | |
4507 | } | |
4508 | EXPORT_SYMBOL(vfs_whiteout); | |
4509 | ||
5d826c84 | 4510 | int readlink_copy(char __user *buffer, int buflen, const char *link) |
1da177e4 | 4511 | { |
5d826c84 | 4512 | int len = PTR_ERR(link); |
1da177e4 LT |
4513 | if (IS_ERR(link)) |
4514 | goto out; | |
4515 | ||
4516 | len = strlen(link); | |
4517 | if (len > (unsigned) buflen) | |
4518 | len = buflen; | |
4519 | if (copy_to_user(buffer, link, len)) | |
4520 | len = -EFAULT; | |
4521 | out: | |
4522 | return len; | |
4523 | } | |
4524 | ||
fd4a0edf MS |
4525 | /** |
4526 | * vfs_readlink - copy symlink body into userspace buffer | |
4527 | * @dentry: dentry on which to get symbolic link | |
4528 | * @buffer: user memory pointer | |
4529 | * @buflen: size of buffer | |
4530 | * | |
4531 | * Does not touch atime. That's up to the caller if necessary | |
4532 | * | |
4533 | * Does not call security hook. | |
4534 | */ | |
4535 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) | |
4536 | { | |
4537 | struct inode *inode = d_inode(dentry); | |
f2df5da6 AV |
4538 | DEFINE_DELAYED_CALL(done); |
4539 | const char *link; | |
4540 | int res; | |
fd4a0edf | 4541 | |
76fca90e MS |
4542 | if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { |
4543 | if (unlikely(inode->i_op->readlink)) | |
4544 | return inode->i_op->readlink(dentry, buffer, buflen); | |
4545 | ||
4546 | if (!d_is_symlink(dentry)) | |
4547 | return -EINVAL; | |
4548 | ||
4549 | spin_lock(&inode->i_lock); | |
4550 | inode->i_opflags |= IOP_DEFAULT_READLINK; | |
4551 | spin_unlock(&inode->i_lock); | |
4552 | } | |
fd4a0edf | 4553 | |
4c4f7c19 | 4554 | link = READ_ONCE(inode->i_link); |
f2df5da6 AV |
4555 | if (!link) { |
4556 | link = inode->i_op->get_link(dentry, inode, &done); | |
4557 | if (IS_ERR(link)) | |
4558 | return PTR_ERR(link); | |
4559 | } | |
4560 | res = readlink_copy(buffer, buflen, link); | |
4561 | do_delayed_call(&done); | |
4562 | return res; | |
fd4a0edf MS |
4563 | } |
4564 | EXPORT_SYMBOL(vfs_readlink); | |
1da177e4 | 4565 | |
d60874cd MS |
4566 | /** |
4567 | * vfs_get_link - get symlink body | |
4568 | * @dentry: dentry on which to get symbolic link | |
4569 | * @done: caller needs to free returned data with this | |
4570 | * | |
4571 | * Calls security hook and i_op->get_link() on the supplied inode. | |
4572 | * | |
4573 | * It does not touch atime. That's up to the caller if necessary. | |
4574 | * | |
4575 | * Does not work on "special" symlinks like /proc/$$/fd/N | |
4576 | */ | |
4577 | const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) | |
4578 | { | |
4579 | const char *res = ERR_PTR(-EINVAL); | |
4580 | struct inode *inode = d_inode(dentry); | |
4581 | ||
4582 | if (d_is_symlink(dentry)) { | |
4583 | res = ERR_PTR(security_inode_readlink(dentry)); | |
4584 | if (!res) | |
4585 | res = inode->i_op->get_link(dentry, inode, done); | |
4586 | } | |
4587 | return res; | |
4588 | } | |
4589 | EXPORT_SYMBOL(vfs_get_link); | |
4590 | ||
1da177e4 | 4591 | /* get the link contents into pagecache */ |
6b255391 | 4592 | const char *page_get_link(struct dentry *dentry, struct inode *inode, |
fceef393 | 4593 | struct delayed_call *callback) |
1da177e4 | 4594 | { |
ebd09abb DG |
4595 | char *kaddr; |
4596 | struct page *page; | |
6b255391 AV |
4597 | struct address_space *mapping = inode->i_mapping; |
4598 | ||
d3883d4f AV |
4599 | if (!dentry) { |
4600 | page = find_get_page(mapping, 0); | |
4601 | if (!page) | |
4602 | return ERR_PTR(-ECHILD); | |
4603 | if (!PageUptodate(page)) { | |
4604 | put_page(page); | |
4605 | return ERR_PTR(-ECHILD); | |
4606 | } | |
4607 | } else { | |
4608 | page = read_mapping_page(mapping, 0, NULL); | |
4609 | if (IS_ERR(page)) | |
4610 | return (char*)page; | |
4611 | } | |
fceef393 | 4612 | set_delayed_call(callback, page_put_link, page); |
21fc61c7 AV |
4613 | BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); |
4614 | kaddr = page_address(page); | |
6b255391 | 4615 | nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); |
ebd09abb | 4616 | return kaddr; |
1da177e4 LT |
4617 | } |
4618 | ||
6b255391 | 4619 | EXPORT_SYMBOL(page_get_link); |
1da177e4 | 4620 | |
fceef393 | 4621 | void page_put_link(void *arg) |
1da177e4 | 4622 | { |
fceef393 | 4623 | put_page(arg); |
1da177e4 | 4624 | } |
4d359507 | 4625 | EXPORT_SYMBOL(page_put_link); |
1da177e4 | 4626 | |
aa80deab AV |
4627 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
4628 | { | |
fceef393 | 4629 | DEFINE_DELAYED_CALL(done); |
6b255391 AV |
4630 | int res = readlink_copy(buffer, buflen, |
4631 | page_get_link(dentry, d_inode(dentry), | |
fceef393 AV |
4632 | &done)); |
4633 | do_delayed_call(&done); | |
aa80deab AV |
4634 | return res; |
4635 | } | |
4636 | EXPORT_SYMBOL(page_readlink); | |
4637 | ||
54566b2c NP |
4638 | /* |
4639 | * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS | |
4640 | */ | |
4641 | int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) | |
1da177e4 LT |
4642 | { |
4643 | struct address_space *mapping = inode->i_mapping; | |
0adb25d2 | 4644 | struct page *page; |
afddba49 | 4645 | void *fsdata; |
beb497ab | 4646 | int err; |
c718a975 | 4647 | unsigned int flags = 0; |
54566b2c NP |
4648 | if (nofs) |
4649 | flags |= AOP_FLAG_NOFS; | |
1da177e4 | 4650 | |
7e53cac4 | 4651 | retry: |
afddba49 | 4652 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
54566b2c | 4653 | flags, &page, &fsdata); |
1da177e4 | 4654 | if (err) |
afddba49 NP |
4655 | goto fail; |
4656 | ||
21fc61c7 | 4657 | memcpy(page_address(page), symname, len-1); |
afddba49 NP |
4658 | |
4659 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, | |
4660 | page, fsdata); | |
1da177e4 LT |
4661 | if (err < 0) |
4662 | goto fail; | |
afddba49 NP |
4663 | if (err < len-1) |
4664 | goto retry; | |
4665 | ||
1da177e4 LT |
4666 | mark_inode_dirty(inode); |
4667 | return 0; | |
1da177e4 LT |
4668 | fail: |
4669 | return err; | |
4670 | } | |
4d359507 | 4671 | EXPORT_SYMBOL(__page_symlink); |
1da177e4 | 4672 | |
0adb25d2 KK |
4673 | int page_symlink(struct inode *inode, const char *symname, int len) |
4674 | { | |
4675 | return __page_symlink(inode, symname, len, | |
c62d2555 | 4676 | !mapping_gfp_constraint(inode->i_mapping, __GFP_FS)); |
0adb25d2 | 4677 | } |
4d359507 | 4678 | EXPORT_SYMBOL(page_symlink); |
0adb25d2 | 4679 | |
92e1d5be | 4680 | const struct inode_operations page_symlink_inode_operations = { |
6b255391 | 4681 | .get_link = page_get_link, |
1da177e4 | 4682 | }; |
1da177e4 | 4683 | EXPORT_SYMBOL(page_symlink_inode_operations); |