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