Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-only
2 : /*
3 : * linux/fs/open.c
4 : *
5 : * Copyright (C) 1991, 1992 Linus Torvalds
6 : */
7 :
8 : #include <linux/string.h>
9 : #include <linux/mm.h>
10 : #include <linux/file.h>
11 : #include <linux/fdtable.h>
12 : #include <linux/fsnotify.h>
13 : #include <linux/module.h>
14 : #include <linux/tty.h>
15 : #include <linux/namei.h>
16 : #include <linux/backing-dev.h>
17 : #include <linux/capability.h>
18 : #include <linux/securebits.h>
19 : #include <linux/security.h>
20 : #include <linux/mount.h>
21 : #include <linux/fcntl.h>
22 : #include <linux/slab.h>
23 : #include <linux/uaccess.h>
24 : #include <linux/fs.h>
25 : #include <linux/personality.h>
26 : #include <linux/pagemap.h>
27 : #include <linux/syscalls.h>
28 : #include <linux/rcupdate.h>
29 : #include <linux/audit.h>
30 : #include <linux/falloc.h>
31 : #include <linux/fs_struct.h>
32 : #include <linux/ima.h>
33 : #include <linux/dnotify.h>
34 : #include <linux/compat.h>
35 : #include <linux/mnt_idmapping.h>
36 : #include <linux/filelock.h>
37 :
38 : #include "internal.h"
39 :
40 4622774 : int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
41 : loff_t length, unsigned int time_attrs, struct file *filp)
42 : {
43 4622774 : int ret;
44 4622774 : struct iattr newattrs;
45 :
46 : /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
47 4622774 : if (length < 0)
48 : return -EINVAL;
49 :
50 4622774 : newattrs.ia_size = length;
51 4622774 : newattrs.ia_valid = ATTR_SIZE | time_attrs;
52 4622774 : if (filp) {
53 1886951 : newattrs.ia_file = filp;
54 1886951 : newattrs.ia_valid |= ATTR_FILE;
55 : }
56 :
57 : /* Remove suid, sgid, and file capabilities on truncate too */
58 4622774 : ret = dentry_needs_remove_privs(idmap, dentry);
59 4622713 : if (ret < 0)
60 : return ret;
61 4622713 : if (ret)
62 12 : newattrs.ia_valid |= ret | ATTR_FORCE;
63 :
64 4622713 : inode_lock(dentry->d_inode);
65 : /* Note any delegations or leases have already been broken: */
66 4622812 : ret = notify_change(idmap, dentry, &newattrs, NULL);
67 4622821 : inode_unlock(dentry->d_inode);
68 4622821 : return ret;
69 : }
70 :
71 2735835 : long vfs_truncate(const struct path *path, loff_t length)
72 : {
73 2735835 : struct mnt_idmap *idmap;
74 2735835 : struct inode *inode;
75 2735835 : long error;
76 :
77 2735835 : inode = path->dentry->d_inode;
78 :
79 : /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
80 2735835 : if (S_ISDIR(inode->i_mode))
81 : return -EISDIR;
82 2735835 : if (!S_ISREG(inode->i_mode))
83 : return -EINVAL;
84 :
85 2735835 : error = mnt_want_write(path->mnt);
86 2735837 : if (error)
87 0 : goto out;
88 :
89 2735837 : idmap = mnt_idmap(path->mnt);
90 2735837 : error = inode_permission(idmap, inode, MAY_WRITE);
91 2735823 : if (error)
92 4 : goto mnt_drop_write_and_out;
93 :
94 2735819 : error = -EPERM;
95 2735819 : if (IS_APPEND(inode))
96 4 : goto mnt_drop_write_and_out;
97 :
98 2735815 : error = get_write_access(inode);
99 2735825 : if (error)
100 0 : goto mnt_drop_write_and_out;
101 :
102 : /*
103 : * Make sure that there are no leases. get_write_access() protects
104 : * against the truncate racing with a lease-granting setlease().
105 : */
106 2735825 : error = break_lease(inode, O_WRONLY);
107 2735828 : if (error)
108 2 : goto put_write_and_out;
109 :
110 2735826 : error = security_path_truncate(path);
111 2735826 : if (!error)
112 2735826 : error = do_truncate(idmap, path->dentry, length, 0, NULL);
113 :
114 2735827 : put_write_and_out:
115 2735827 : put_write_access(inode);
116 2735835 : mnt_drop_write_and_out:
117 2735835 : mnt_drop_write(path->mnt);
118 : out:
119 : return error;
120 : }
121 : EXPORT_SYMBOL_GPL(vfs_truncate);
122 :
123 2735832 : long do_sys_truncate(const char __user *pathname, loff_t length)
124 : {
125 2735832 : unsigned int lookup_flags = LOOKUP_FOLLOW;
126 2735832 : struct path path;
127 2735832 : int error;
128 :
129 2735832 : if (length < 0) /* sorry, but loff_t says... */
130 : return -EINVAL;
131 :
132 2735832 : retry:
133 2735832 : error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
134 2735838 : if (!error) {
135 2735838 : error = vfs_truncate(&path, length);
136 2735836 : path_put(&path);
137 : }
138 5471668 : if (retry_estale(error, lookup_flags)) {
139 0 : lookup_flags |= LOOKUP_REVAL;
140 0 : goto retry;
141 : }
142 : return error;
143 : }
144 :
145 5471657 : SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
146 : {
147 2735822 : return do_sys_truncate(path, length);
148 : }
149 :
150 : #ifdef CONFIG_COMPAT
151 : COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
152 : {
153 : return do_sys_truncate(path, length);
154 : }
155 : #endif
156 :
157 1203166 : long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
158 : {
159 1203166 : struct inode *inode;
160 1203166 : struct dentry *dentry;
161 1203166 : struct fd f;
162 1203166 : int error;
163 :
164 1203166 : error = -EINVAL;
165 1203166 : if (length < 0)
166 0 : goto out;
167 1203166 : error = -EBADF;
168 1203166 : f = fdget(fd);
169 1203168 : if (!f.file)
170 0 : goto out;
171 :
172 : /* explicitly opened as large or we are on 64-bit box */
173 1203168 : if (f.file->f_flags & O_LARGEFILE)
174 1203171 : small = 0;
175 :
176 1203168 : dentry = f.file->f_path.dentry;
177 1203168 : inode = dentry->d_inode;
178 1203168 : error = -EINVAL;
179 1203168 : if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
180 0 : goto out_putf;
181 :
182 1203168 : error = -EINVAL;
183 : /* Cannot ftruncate over 2^31 bytes without large file support */
184 1203168 : if (small && length > MAX_NON_LFS)
185 0 : goto out_putf;
186 :
187 1203168 : error = -EPERM;
188 : /* Check IS_APPEND on real upper inode */
189 1203168 : if (IS_APPEND(file_inode(f.file)))
190 4 : goto out_putf;
191 1203164 : sb_start_write(inode->i_sb);
192 1203115 : error = security_file_truncate(f.file);
193 1203115 : if (!error)
194 1203115 : error = do_truncate(file_mnt_idmap(f.file), dentry, length,
195 : ATTR_MTIME | ATTR_CTIME, f.file);
196 1203237 : sb_end_write(inode->i_sb);
197 1203243 : out_putf:
198 1203243 : fdput(f);
199 1203243 : out:
200 1203243 : return error;
201 : }
202 :
203 2406389 : SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
204 : {
205 1203145 : return do_sys_ftruncate(fd, length, 1);
206 : }
207 :
208 : #ifdef CONFIG_COMPAT
209 : COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
210 : {
211 : return do_sys_ftruncate(fd, length, 1);
212 : }
213 : #endif
214 :
215 : /* LFS versions of truncate are only needed on 32 bit machines */
216 : #if BITS_PER_LONG == 32
217 : SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
218 : {
219 : return do_sys_truncate(path, length);
220 : }
221 :
222 : SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
223 : {
224 : return do_sys_ftruncate(fd, length, 0);
225 : }
226 : #endif /* BITS_PER_LONG == 32 */
227 :
228 : #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_TRUNCATE64)
229 : COMPAT_SYSCALL_DEFINE3(truncate64, const char __user *, pathname,
230 : compat_arg_u64_dual(length))
231 : {
232 : return ksys_truncate(pathname, compat_arg_u64_glue(length));
233 : }
234 : #endif
235 :
236 : #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FTRUNCATE64)
237 : COMPAT_SYSCALL_DEFINE3(ftruncate64, unsigned int, fd,
238 : compat_arg_u64_dual(length))
239 : {
240 : return ksys_ftruncate(fd, compat_arg_u64_glue(length));
241 : }
242 : #endif
243 :
244 67528710 : int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
245 : {
246 67528710 : struct inode *inode = file_inode(file);
247 67528710 : long ret;
248 :
249 67528710 : if (offset < 0 || len <= 0)
250 : return -EINVAL;
251 :
252 : /* Return error if mode is not supported */
253 18121539 : if (mode & ~FALLOC_FL_SUPPORTED_MASK)
254 : return -EOPNOTSUPP;
255 :
256 : /* Punch hole and zero range are mutually exclusive */
257 18121539 : if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
258 : (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
259 : return -EOPNOTSUPP;
260 :
261 : /* Punch hole must have keep size set */
262 18121539 : if ((mode & FALLOC_FL_PUNCH_HOLE) &&
263 : !(mode & FALLOC_FL_KEEP_SIZE))
264 : return -EOPNOTSUPP;
265 :
266 : /* Collapse range should only be used exclusively. */
267 17449242 : if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
268 1622854 : (mode & ~FALLOC_FL_COLLAPSE_RANGE))
269 : return -EINVAL;
270 :
271 : /* Insert range should only be used exclusively. */
272 16781440 : if ((mode & FALLOC_FL_INSERT_RANGE) &&
273 1526371 : (mode & ~FALLOC_FL_INSERT_RANGE))
274 : return -EINVAL;
275 :
276 : /* Unshare range should only be used with allocate mode. */
277 16113582 : if ((mode & FALLOC_FL_UNSHARE_RANGE) &&
278 66 : (mode & ~(FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_KEEP_SIZE)))
279 : return -EINVAL;
280 :
281 : /* Mapping free space should only be used by itself. */
282 16113582 : if ((mode & FALLOC_FL_MAP_FREE_SPACE) &&
283 204 : (mode & ~FALLOC_FL_MAP_FREE_SPACE))
284 : return -EINVAL;
285 :
286 16113582 : if (!(file->f_mode & FMODE_WRITE))
287 : return -EBADF;
288 :
289 : /*
290 : * We can only allow pure fallocate on append only files
291 : */
292 16113582 : if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
293 : return -EPERM;
294 :
295 16113582 : if (IS_IMMUTABLE(inode))
296 : return -EPERM;
297 :
298 : /*
299 : * We cannot allow any fallocate operation on an active swapfile
300 : */
301 16113582 : if (IS_SWAPFILE(inode))
302 : return -ETXTBSY;
303 :
304 : /*
305 : * Revalidate the write permissions, in case security policy has
306 : * changed since the files were opened.
307 : */
308 16113578 : ret = security_file_permission(file, MAY_WRITE);
309 16113578 : if (ret)
310 : return ret;
311 :
312 16113578 : if (S_ISFIFO(inode->i_mode))
313 : return -ESPIPE;
314 :
315 16113578 : if (S_ISDIR(inode->i_mode))
316 : return -EISDIR;
317 :
318 16113578 : if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
319 : return -ENODEV;
320 :
321 : /* Check for wrap through zero too */
322 16113578 : if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
323 : return -EFBIG;
324 :
325 16113574 : if (!file->f_op->fallocate)
326 : return -EOPNOTSUPP;
327 :
328 16113470 : file_start_write(file);
329 16113377 : ret = file->f_op->fallocate(file, mode, offset, len);
330 :
331 : /*
332 : * Create inotify and fanotify events.
333 : *
334 : * To keep the logic simple always create events if fallocate succeeds.
335 : * This implies that events are even created if the file size remains
336 : * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
337 : */
338 16113492 : if (ret == 0)
339 15052338 : fsnotify_modify(file);
340 :
341 16113502 : file_end_write(file);
342 16113509 : return ret;
343 : }
344 : EXPORT_SYMBOL_GPL(vfs_fallocate);
345 :
346 64725124 : int ksys_fallocate(int fd, int mode, loff_t offset, loff_t len)
347 : {
348 64725124 : struct fd f = fdget(fd);
349 64725099 : int error = -EBADF;
350 :
351 64725099 : if (f.file) {
352 64725099 : error = vfs_fallocate(f.file, mode, offset, len);
353 64725123 : fdput(f);
354 : }
355 64725118 : return error;
356 : }
357 :
358 129450218 : SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
359 : {
360 64725089 : return ksys_fallocate(fd, mode, offset, len);
361 : }
362 :
363 : #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FALLOCATE)
364 : COMPAT_SYSCALL_DEFINE6(fallocate, int, fd, int, mode, compat_arg_u64_dual(offset),
365 : compat_arg_u64_dual(len))
366 : {
367 : return ksys_fallocate(fd, mode, compat_arg_u64_glue(offset),
368 : compat_arg_u64_glue(len));
369 : }
370 : #endif
371 :
372 : /*
373 : * access() needs to use the real uid/gid, not the effective uid/gid.
374 : * We do this by temporarily clearing all FS-related capabilities and
375 : * switching the fsuid/fsgid around to the real ones.
376 : *
377 : * Creating new credentials is expensive, so we try to skip doing it,
378 : * which we can if the result would match what we already got.
379 : */
380 57413738 : static bool access_need_override_creds(int flags)
381 : {
382 57413738 : const struct cred *cred;
383 :
384 57413738 : if (flags & AT_EACCESS)
385 : return false;
386 :
387 57388077 : cred = current_cred();
388 57388077 : if (!uid_eq(cred->fsuid, cred->uid) ||
389 : !gid_eq(cred->fsgid, cred->gid))
390 : return true;
391 :
392 57389828 : if (!issecure(SECURE_NO_SETUID_FIXUP)) {
393 57391351 : kuid_t root_uid = make_kuid(cred->user_ns, 0);
394 57388228 : if (!uid_eq(cred->uid, root_uid)) {
395 68970 : if (!cap_isclear(cred->cap_effective))
396 11289 : return true;
397 : } else {
398 57319258 : if (!cap_isidentical(cred->cap_effective,
399 : cred->cap_permitted))
400 : return true;
401 : }
402 : }
403 :
404 : return false;
405 : }
406 :
407 14742 : static const struct cred *access_override_creds(void)
408 : {
409 14742 : const struct cred *old_cred;
410 14742 : struct cred *override_cred;
411 :
412 14742 : override_cred = prepare_creds();
413 14743 : if (!override_cred)
414 : return NULL;
415 :
416 : /*
417 : * XXX access_need_override_creds performs checks in hopes of skipping
418 : * this work. Make sure it stays in sync if making any changes in this
419 : * routine.
420 : */
421 :
422 14743 : override_cred->fsuid = override_cred->uid;
423 14743 : override_cred->fsgid = override_cred->gid;
424 :
425 14743 : if (!issecure(SECURE_NO_SETUID_FIXUP)) {
426 : /* Clear the capabilities if we switch to a non-root user */
427 14743 : kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
428 14743 : if (!uid_eq(override_cred->uid, root_uid))
429 12681 : cap_clear(override_cred->cap_effective);
430 : else
431 2062 : override_cred->cap_effective =
432 : override_cred->cap_permitted;
433 : }
434 :
435 : /*
436 : * The new set of credentials can *only* be used in
437 : * task-synchronous circumstances, and does not need
438 : * RCU freeing, unless somebody then takes a separate
439 : * reference to it.
440 : *
441 : * NOTE! This is _only_ true because this credential
442 : * is used purely for override_creds() that installs
443 : * it as the subjective cred. Other threads will be
444 : * accessing ->real_cred, not the subjective cred.
445 : *
446 : * If somebody _does_ make a copy of this (using the
447 : * 'get_current_cred()' function), that will clear the
448 : * non_rcu field, because now that other user may be
449 : * expecting RCU freeing. But normal thread-synchronous
450 : * cred accesses will keep things non-RCY.
451 : */
452 14743 : override_cred->non_rcu = 1;
453 :
454 14743 : old_cred = override_creds(override_cred);
455 :
456 : /* override_cred() gets its own ref */
457 14743 : put_cred(override_cred);
458 :
459 14743 : return old_cred;
460 : }
461 :
462 57414562 : static long do_faccessat(int dfd, const char __user *filename, int mode, int flags)
463 : {
464 57414562 : struct path path;
465 57414562 : struct inode *inode;
466 57414562 : int res;
467 57414562 : unsigned int lookup_flags = LOOKUP_FOLLOW;
468 57414562 : const struct cred *old_cred = NULL;
469 :
470 57414562 : if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
471 : return -EINVAL;
472 :
473 57414562 : if (flags & ~(AT_EACCESS | AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
474 : return -EINVAL;
475 :
476 57414562 : if (flags & AT_SYMLINK_NOFOLLOW)
477 16321 : lookup_flags &= ~LOOKUP_FOLLOW;
478 57414562 : if (flags & AT_EMPTY_PATH)
479 0 : lookup_flags |= LOOKUP_EMPTY;
480 :
481 57414562 : if (access_need_override_creds(flags)) {
482 14743 : old_cred = access_override_creds();
483 14743 : if (!old_cred)
484 : return -ENOMEM;
485 : }
486 :
487 57408270 : retry:
488 57408270 : res = user_path_at(dfd, filename, lookup_flags, &path);
489 57410686 : if (res)
490 26883155 : goto out;
491 :
492 30527531 : inode = d_backing_inode(path.dentry);
493 :
494 30527531 : if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
495 : /*
496 : * MAY_EXEC on regular files is denied if the fs is mounted
497 : * with the "noexec" flag.
498 : */
499 12579898 : res = -EACCES;
500 12579898 : if (path_noexec(&path))
501 0 : goto out_path_release;
502 : }
503 :
504 30527391 : res = inode_permission(mnt_idmap(path.mnt), inode, mode | MAY_ACCESS);
505 : /* SuS v2 requires we report a read only fs too */
506 30522645 : if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
507 30435372 : goto out_path_release;
508 : /*
509 : * This is a rare case where using __mnt_is_readonly()
510 : * is OK without a mnt_want/drop_write() pair. Since
511 : * no actual write to the fs is performed here, we do
512 : * not need to telegraph to that to anyone.
513 : *
514 : * By doing this, we accept that this access is
515 : * inherently racy and know that the fs may change
516 : * state before we even see this result.
517 : */
518 87273 : if (__mnt_is_readonly(path.mnt))
519 225 : res = -EROFS;
520 :
521 87045 : out_path_release:
522 30522642 : path_put(&path);
523 61055132 : if (retry_estale(res, lookup_flags)) {
524 0 : lookup_flags |= LOOKUP_REVAL;
525 0 : goto retry;
526 : }
527 30527566 : out:
528 57410721 : if (old_cred)
529 14743 : revert_creds(old_cred);
530 :
531 57411964 : return res;
532 : }
533 :
534 114448256 : SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
535 : {
536 57228573 : return do_faccessat(dfd, filename, mode, 0);
537 : }
538 :
539 384762 : SYSCALL_DEFINE4(faccessat2, int, dfd, const char __user *, filename, int, mode,
540 : int, flags)
541 : {
542 192381 : return do_faccessat(dfd, filename, mode, flags);
543 : }
544 :
545 0 : SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
546 : {
547 0 : return do_faccessat(AT_FDCWD, filename, mode, 0);
548 : }
549 :
550 1457765771 : SYSCALL_DEFINE1(chdir, const char __user *, filename)
551 : {
552 728912745 : struct path path;
553 728912745 : int error;
554 728912745 : unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
555 728906377 : retry:
556 728906377 : error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
557 729082653 : if (error)
558 81496094 : goto out;
559 :
560 647586559 : error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
561 647621252 : if (error)
562 0 : goto dput_and_out;
563 :
564 647621252 : set_fs_pwd(current->fs, &path);
565 :
566 647625927 : dput_and_out:
567 647625927 : path_put(&path);
568 1295255014 : if (retry_estale(error, lookup_flags)) {
569 0 : lookup_flags |= LOOKUP_REVAL;
570 0 : goto retry;
571 : }
572 647633875 : out:
573 729129969 : return error;
574 : }
575 :
576 7031126 : SYSCALL_DEFINE1(fchdir, unsigned int, fd)
577 : {
578 3515563 : struct fd f = fdget_raw(fd);
579 3515563 : int error;
580 :
581 3515563 : error = -EBADF;
582 3515563 : if (!f.file)
583 0 : goto out;
584 :
585 3515563 : error = -ENOTDIR;
586 3515563 : if (!d_can_lookup(f.file->f_path.dentry))
587 0 : goto out_putf;
588 :
589 3515563 : error = file_permission(f.file, MAY_EXEC | MAY_CHDIR);
590 3515563 : if (!error)
591 3515563 : set_fs_pwd(current->fs, &f.file->f_path);
592 0 : out_putf:
593 3515563 : fdput(f);
594 3515563 : out:
595 3515563 : return error;
596 : }
597 :
598 46 : SYSCALL_DEFINE1(chroot, const char __user *, filename)
599 : {
600 23 : struct path path;
601 23 : int error;
602 23 : unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
603 23 : retry:
604 23 : error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
605 23 : if (error)
606 0 : goto out;
607 :
608 23 : error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
609 23 : if (error)
610 0 : goto dput_and_out;
611 :
612 23 : error = -EPERM;
613 23 : if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
614 0 : goto dput_and_out;
615 23 : error = security_path_chroot(&path);
616 23 : if (error)
617 : goto dput_and_out;
618 :
619 23 : set_fs_root(current->fs, &path);
620 23 : error = 0;
621 23 : dput_and_out:
622 23 : path_put(&path);
623 46 : if (retry_estale(error, lookup_flags)) {
624 0 : lookup_flags |= LOOKUP_REVAL;
625 0 : goto retry;
626 : }
627 23 : out:
628 23 : return error;
629 : }
630 :
631 262224 : int chmod_common(const struct path *path, umode_t mode)
632 : {
633 262224 : struct inode *inode = path->dentry->d_inode;
634 262224 : struct inode *delegated_inode = NULL;
635 262224 : struct iattr newattrs;
636 262224 : int error;
637 :
638 262224 : error = mnt_want_write(path->mnt);
639 262218 : if (error)
640 : return error;
641 262189 : retry_deleg:
642 262189 : inode_lock(inode);
643 262201 : error = security_path_chmod(path, mode);
644 262201 : if (error)
645 : goto out_unlock;
646 262201 : newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
647 262201 : newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
648 262201 : error = notify_change(mnt_idmap(path->mnt), path->dentry,
649 : &newattrs, &delegated_inode);
650 : out_unlock:
651 262205 : inode_unlock(inode);
652 262196 : if (delegated_inode) {
653 0 : error = break_deleg_wait(&delegated_inode);
654 0 : if (!error)
655 0 : goto retry_deleg;
656 : }
657 262196 : mnt_drop_write(path->mnt);
658 262196 : return error;
659 : }
660 :
661 0 : int vfs_fchmod(struct file *file, umode_t mode)
662 : {
663 255059 : audit_file(file);
664 0 : return chmod_common(&file->f_path, mode);
665 : }
666 :
667 510112 : SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
668 : {
669 255053 : struct fd f = fdget(fd);
670 255059 : int err = -EBADF;
671 :
672 255059 : if (f.file) {
673 255059 : err = vfs_fchmod(f.file, mode);
674 255053 : fdput(f);
675 : }
676 255057 : return err;
677 : }
678 :
679 7163 : static int do_fchmodat(int dfd, const char __user *filename, umode_t mode)
680 : {
681 7163 : struct path path;
682 7163 : int error;
683 7163 : unsigned int lookup_flags = LOOKUP_FOLLOW;
684 7163 : retry:
685 7163 : error = user_path_at(dfd, filename, lookup_flags, &path);
686 7163 : if (!error) {
687 7163 : error = chmod_common(&path, mode);
688 7163 : path_put(&path);
689 14326 : if (retry_estale(error, lookup_flags)) {
690 0 : lookup_flags |= LOOKUP_REVAL;
691 0 : goto retry;
692 : }
693 : }
694 7163 : return error;
695 : }
696 :
697 14326 : SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename,
698 : umode_t, mode)
699 : {
700 7163 : return do_fchmodat(dfd, filename, mode);
701 : }
702 :
703 0 : SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
704 : {
705 0 : return do_fchmodat(AT_FDCWD, filename, mode);
706 : }
707 :
708 : /*
709 : * Check whether @kuid is valid and if so generate and set vfsuid_t in
710 : * ia_vfsuid.
711 : *
712 : * Return: true if @kuid is valid, false if not.
713 : */
714 : static inline bool setattr_vfsuid(struct iattr *attr, kuid_t kuid)
715 : {
716 6182530 : if (!uid_valid(kuid))
717 : return false;
718 6162520 : attr->ia_valid |= ATTR_UID;
719 6162520 : attr->ia_vfsuid = VFSUIDT_INIT(kuid);
720 6162520 : return true;
721 : }
722 :
723 : /*
724 : * Check whether @kgid is valid and if so generate and set vfsgid_t in
725 : * ia_vfsgid.
726 : *
727 : * Return: true if @kgid is valid, false if not.
728 : */
729 : static inline bool setattr_vfsgid(struct iattr *attr, kgid_t kgid)
730 : {
731 6162208 : if (!gid_valid(kgid))
732 : return false;
733 6162208 : attr->ia_valid |= ATTR_GID;
734 6162208 : attr->ia_vfsgid = VFSGIDT_INIT(kgid);
735 6162208 : return true;
736 : }
737 :
738 6182645 : int chown_common(const struct path *path, uid_t user, gid_t group)
739 : {
740 6182645 : struct mnt_idmap *idmap;
741 6182645 : struct user_namespace *fs_userns;
742 6182645 : struct inode *inode = path->dentry->d_inode;
743 6182645 : struct inode *delegated_inode = NULL;
744 6182645 : int error;
745 6182645 : struct iattr newattrs;
746 6182645 : kuid_t uid;
747 6182645 : kgid_t gid;
748 :
749 6182645 : uid = make_kuid(current_user_ns(), user);
750 6183031 : gid = make_kgid(current_user_ns(), group);
751 :
752 6183017 : idmap = mnt_idmap(path->mnt);
753 6183020 : fs_userns = i_user_ns(inode);
754 :
755 6182918 : retry_deleg:
756 6182918 : newattrs.ia_vfsuid = INVALID_VFSUID;
757 6182918 : newattrs.ia_vfsgid = INVALID_VFSGID;
758 6182918 : newattrs.ia_valid = ATTR_CTIME;
759 6182918 : if ((user != (uid_t)-1) && !setattr_vfsuid(&newattrs, uid))
760 : return -EINVAL;
761 6162908 : if ((group != (gid_t)-1) && !setattr_vfsgid(&newattrs, gid))
762 : return -EINVAL;
763 6162908 : inode_lock(inode);
764 6163069 : if (!S_ISDIR(inode->i_mode))
765 5420382 : newattrs.ia_valid |= ATTR_KILL_SUID | ATTR_KILL_PRIV |
766 5420398 : setattr_should_drop_sgid(idmap, inode);
767 : /* Continue to send actual fs values, not the mount values. */
768 6163053 : error = security_path_chown(
769 : path,
770 : from_vfsuid(idmap, fs_userns, newattrs.ia_vfsuid),
771 : from_vfsgid(idmap, fs_userns, newattrs.ia_vfsgid));
772 6162923 : if (!error)
773 6162923 : error = notify_change(idmap, path->dentry, &newattrs,
774 : &delegated_inode);
775 6163220 : inode_unlock(inode);
776 6163154 : if (delegated_inode) {
777 0 : error = break_deleg_wait(&delegated_inode);
778 0 : if (!error)
779 0 : goto retry_deleg;
780 : }
781 : return error;
782 : }
783 :
784 4356568 : int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
785 : int flag)
786 : {
787 4356568 : struct path path;
788 4356568 : int error = -EINVAL;
789 4356568 : int lookup_flags;
790 :
791 4356568 : if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
792 0 : goto out;
793 :
794 4356568 : lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
795 4356568 : if (flag & AT_EMPTY_PATH)
796 933 : lookup_flags |= LOOKUP_EMPTY;
797 4355635 : retry:
798 4356568 : error = user_path_at(dfd, filename, lookup_flags, &path);
799 4356606 : if (error)
800 4 : goto out;
801 4356602 : error = mnt_want_write(path.mnt);
802 4356598 : if (error)
803 0 : goto out_release;
804 4356598 : error = chown_common(&path, user, group);
805 4356569 : mnt_drop_write(path.mnt);
806 4356550 : out_release:
807 4356550 : path_put(&path);
808 8713138 : if (retry_estale(error, lookup_flags)) {
809 0 : lookup_flags |= LOOKUP_REVAL;
810 0 : goto retry;
811 : }
812 4356569 : out:
813 4356573 : return error;
814 : }
815 :
816 8713130 : SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
817 : gid_t, group, int, flag)
818 : {
819 4356556 : return do_fchownat(dfd, filename, user, group, flag);
820 : }
821 :
822 0 : SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
823 : {
824 0 : return do_fchownat(AT_FDCWD, filename, user, group, 0);
825 : }
826 :
827 0 : SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
828 : {
829 0 : return do_fchownat(AT_FDCWD, filename, user, group,
830 : AT_SYMLINK_NOFOLLOW);
831 : }
832 :
833 1826312 : int vfs_fchown(struct file *file, uid_t user, gid_t group)
834 : {
835 1826312 : int error;
836 :
837 1826312 : error = mnt_want_write_file(file);
838 1826228 : if (error)
839 : return error;
840 1826229 : audit_file(file);
841 1826229 : error = chown_common(&file->f_path, user, group);
842 1826638 : mnt_drop_write_file(file);
843 1826638 : return error;
844 : }
845 :
846 1826230 : int ksys_fchown(unsigned int fd, uid_t user, gid_t group)
847 : {
848 1826230 : struct fd f = fdget(fd);
849 1826116 : int error = -EBADF;
850 :
851 1826116 : if (f.file) {
852 1826116 : error = vfs_fchown(f.file, user, group);
853 1826633 : fdput(f);
854 : }
855 1826633 : return error;
856 : }
857 :
858 3652867 : SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
859 : {
860 1826275 : return ksys_fchown(fd, user, group);
861 : }
862 :
863 696353638 : static int do_dentry_open(struct file *f,
864 : struct inode *inode,
865 : int (*open)(struct inode *, struct file *))
866 : {
867 696353638 : static const struct file_operations empty_fops = {};
868 696353638 : int error;
869 :
870 696353638 : path_get(&f->f_path);
871 696421448 : f->f_inode = inode;
872 696421448 : f->f_mapping = inode->i_mapping;
873 696421448 : f->f_wb_err = filemap_sample_wb_err(f->f_mapping);
874 696398615 : f->f_sb_err = file_sample_sb_err(f);
875 :
876 696427074 : if (unlikely(f->f_flags & O_PATH)) {
877 18159241 : f->f_mode = FMODE_PATH | FMODE_OPENED;
878 18159241 : f->f_op = &empty_fops;
879 18159241 : return 0;
880 : }
881 :
882 678267833 : if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
883 272236747 : i_readcount_inc(inode);
884 406031086 : } else if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
885 401701864 : error = get_write_access(inode);
886 401734611 : if (unlikely(error))
887 0 : goto cleanup_file;
888 401734611 : error = __mnt_want_write(f->f_path.mnt);
889 401740879 : if (unlikely(error)) {
890 0 : put_write_access(inode);
891 0 : goto cleanup_file;
892 : }
893 401740879 : f->f_mode |= FMODE_WRITER;
894 : }
895 :
896 : /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
897 678305063 : if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
898 672778767 : f->f_mode |= FMODE_ATOMIC_POS;
899 :
900 678305063 : f->f_op = fops_get(inode->i_fop);
901 678377552 : if (WARN_ON(!f->f_op)) {
902 0 : error = -ENODEV;
903 0 : goto cleanup_all;
904 : }
905 :
906 678377552 : error = security_file_open(f);
907 678377552 : if (error)
908 : goto cleanup_all;
909 :
910 678377552 : error = break_lease(file_inode(f), f->f_flags);
911 678278227 : if (error)
912 0 : goto cleanup_all;
913 :
914 : /* normally all 3 are set; ->open() can clear them if needed */
915 678278227 : f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
916 678278227 : if (!open)
917 678230954 : open = f->f_op->open;
918 678278227 : if (open) {
919 677606495 : error = open(inode, f);
920 677597325 : if (error)
921 958598 : goto cleanup_all;
922 : }
923 677310459 : f->f_mode |= FMODE_OPENED;
924 677310459 : if ((f->f_mode & FMODE_READ) &&
925 292054156 : likely(f->f_op->read || f->f_op->read_iter))
926 292001671 : f->f_mode |= FMODE_CAN_READ;
927 677310459 : if ((f->f_mode & FMODE_WRITE) &&
928 405084018 : likely(f->f_op->write || f->f_op->write_iter))
929 405031814 : f->f_mode |= FMODE_CAN_WRITE;
930 677310459 : if ((f->f_mode & FMODE_LSEEK) && !f->f_op->llseek)
931 281 : f->f_mode &= ~FMODE_LSEEK;
932 677310459 : if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO)
933 18631078 : f->f_mode |= FMODE_CAN_ODIRECT;
934 :
935 677310459 : f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
936 677310459 : f->f_iocb_flags = iocb_flags(f);
937 :
938 677310459 : file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
939 :
940 677279822 : if ((f->f_flags & O_DIRECT) && !(f->f_mode & FMODE_CAN_ODIRECT))
941 : return -EINVAL;
942 :
943 : /*
944 : * XXX: Huge page cache doesn't support writing yet. Drop all page
945 : * cache for this file before processing writes.
946 : */
947 677279820 : if (f->f_mode & FMODE_WRITE) {
948 : /*
949 : * Paired with smp_mb() in collapse_file() to ensure nr_thps
950 : * is up to date and the update to i_writecount by
951 : * get_write_access() is visible. Ensures subsequent insertion
952 : * of THPs into the page cache will fail.
953 : */
954 405069805 : smp_mb();
955 405069805 : if (filemap_nr_thps(inode->i_mapping)) {
956 : struct address_space *mapping = inode->i_mapping;
957 :
958 : filemap_invalidate_lock(inode->i_mapping);
959 : /*
960 : * unmap_mapping_range just need to be called once
961 : * here, because the private pages is not need to be
962 : * unmapped mapping (e.g. data segment of dynamic
963 : * shared libraries here).
964 : */
965 : unmap_mapping_range(mapping, 0, 0, 0);
966 : truncate_inode_pages(mapping, 0);
967 : filemap_invalidate_unlock(inode->i_mapping);
968 : }
969 : }
970 :
971 : /*
972 : * Once we return a file with FMODE_OPENED, __fput() will call
973 : * fsnotify_close(), so we need fsnotify_open() here for symmetry.
974 : */
975 677294810 : fsnotify_open(f);
976 677294810 : return 0;
977 :
978 958598 : cleanup_all:
979 958598 : if (WARN_ON_ONCE(error > 0))
980 0 : error = -EINVAL;
981 958598 : fops_put(f->f_op);
982 958597 : put_file_access(f);
983 958597 : cleanup_file:
984 958597 : path_put(&f->f_path);
985 958598 : f->f_path.mnt = NULL;
986 958598 : f->f_path.dentry = NULL;
987 958598 : f->f_inode = NULL;
988 958598 : return error;
989 : }
990 :
991 : /**
992 : * finish_open - finish opening a file
993 : * @file: file pointer
994 : * @dentry: pointer to dentry
995 : * @open: open callback
996 : *
997 : * This can be used to finish opening a file passed to i_op->atomic_open().
998 : *
999 : * If the open callback is set to NULL, then the standard f_op->open()
1000 : * filesystem callback is substituted.
1001 : *
1002 : * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
1003 : * the return value of d_splice_alias(), then the caller needs to perform dput()
1004 : * on it after finish_open().
1005 : *
1006 : * Returns zero on success or -errno if the open failed.
1007 : */
1008 988311 : int finish_open(struct file *file, struct dentry *dentry,
1009 : int (*open)(struct inode *, struct file *))
1010 : {
1011 988311 : BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
1012 :
1013 988311 : file->f_path.dentry = dentry;
1014 988311 : return do_dentry_open(file, d_backing_inode(dentry), open);
1015 : }
1016 : EXPORT_SYMBOL(finish_open);
1017 :
1018 : /**
1019 : * finish_no_open - finish ->atomic_open() without opening the file
1020 : *
1021 : * @file: file pointer
1022 : * @dentry: dentry or NULL (as returned from ->lookup())
1023 : *
1024 : * This can be used to set the result of a successful lookup in ->atomic_open().
1025 : *
1026 : * NB: unlike finish_open() this function does consume the dentry reference and
1027 : * the caller need not dput() it.
1028 : *
1029 : * Returns "0" which must be the return value of ->atomic_open() after having
1030 : * called this function.
1031 : */
1032 9158 : int finish_no_open(struct file *file, struct dentry *dentry)
1033 : {
1034 9158 : file->f_path.dentry = dentry;
1035 9158 : return 0;
1036 : }
1037 : EXPORT_SYMBOL(finish_no_open);
1038 :
1039 38 : char *file_path(struct file *filp, char *buf, int buflen)
1040 : {
1041 38 : return d_path(&filp->f_path, buf, buflen);
1042 : }
1043 : EXPORT_SYMBOL(file_path);
1044 :
1045 : /**
1046 : * vfs_open - open the file at the given path
1047 : * @path: path to open
1048 : * @file: newly allocated file with f_flag initialized
1049 : */
1050 676990882 : int vfs_open(const struct path *path, struct file *file)
1051 : {
1052 676990882 : file->f_path = *path;
1053 676990882 : return do_dentry_open(file, d_backing_inode(path->dentry), NULL);
1054 : }
1055 :
1056 35020442 : struct file *dentry_open(const struct path *path, int flags,
1057 : const struct cred *cred)
1058 : {
1059 35020442 : int error;
1060 35020442 : struct file *f;
1061 :
1062 35020442 : validate_creds(cred);
1063 :
1064 : /* We must always pass in a valid mount pointer. */
1065 35020442 : BUG_ON(!path->mnt);
1066 :
1067 35020442 : f = alloc_empty_file(flags, cred);
1068 35020376 : if (!IS_ERR(f)) {
1069 35020376 : error = vfs_open(path, f);
1070 35019977 : if (error) {
1071 0 : fput(f);
1072 0 : f = ERR_PTR(error);
1073 : }
1074 : }
1075 35019977 : return f;
1076 : }
1077 : EXPORT_SYMBOL(dentry_open);
1078 :
1079 : /**
1080 : * dentry_create - Create and open a file
1081 : * @path: path to create
1082 : * @flags: O_ flags
1083 : * @mode: mode bits for new file
1084 : * @cred: credentials to use
1085 : *
1086 : * Caller must hold the parent directory's lock, and have prepared
1087 : * a negative dentry, placed in @path->dentry, for the new file.
1088 : *
1089 : * Caller sets @path->mnt to the vfsmount of the filesystem where
1090 : * the new file is to be created. The parent directory and the
1091 : * negative dentry must reside on the same filesystem instance.
1092 : *
1093 : * On success, returns a "struct file *". Otherwise a ERR_PTR
1094 : * is returned.
1095 : */
1096 0 : struct file *dentry_create(const struct path *path, int flags, umode_t mode,
1097 : const struct cred *cred)
1098 : {
1099 0 : struct file *f;
1100 0 : int error;
1101 :
1102 0 : validate_creds(cred);
1103 0 : f = alloc_empty_file(flags, cred);
1104 0 : if (IS_ERR(f))
1105 : return f;
1106 :
1107 0 : error = vfs_create(mnt_idmap(path->mnt),
1108 0 : d_inode(path->dentry->d_parent),
1109 : path->dentry, mode, true);
1110 0 : if (!error)
1111 0 : error = vfs_open(path, f);
1112 :
1113 0 : if (unlikely(error)) {
1114 0 : fput(f);
1115 0 : return ERR_PTR(error);
1116 : }
1117 : return f;
1118 : }
1119 : EXPORT_SYMBOL(dentry_create);
1120 :
1121 : /**
1122 : * kernel_file_open - open a file for kernel internal use
1123 : * @path: path of the file to open
1124 : * @flags: open flags
1125 : * @inode: the inode
1126 : * @cred: credentials for open
1127 : *
1128 : * Open a file for use by in-kernel consumers. The file is not accounted
1129 : * against nr_files and must not be installed into the file descriptor
1130 : * table.
1131 : *
1132 : * Return: Opened file on success, an error pointer on failure.
1133 : */
1134 0 : struct file *kernel_file_open(const struct path *path, int flags,
1135 : struct inode *inode, const struct cred *cred)
1136 : {
1137 0 : struct file *f;
1138 0 : int error;
1139 :
1140 0 : f = alloc_empty_file_noaccount(flags, cred);
1141 0 : if (IS_ERR(f))
1142 : return f;
1143 :
1144 0 : f->f_path = *path;
1145 0 : error = do_dentry_open(f, inode, NULL);
1146 0 : if (error) {
1147 0 : fput(f);
1148 0 : f = ERR_PTR(error);
1149 : }
1150 : return f;
1151 : }
1152 : EXPORT_SYMBOL_GPL(kernel_file_open);
1153 :
1154 : /**
1155 : * backing_file_open - open a backing file for kernel internal use
1156 : * @path: path of the file to open
1157 : * @flags: open flags
1158 : * @path: path of the backing file
1159 : * @cred: credentials for open
1160 : *
1161 : * Open a backing file for a stackable filesystem (e.g., overlayfs).
1162 : * @path may be on the stackable filesystem and backing inode on the
1163 : * underlying filesystem. In this case, we want to be able to return
1164 : * the @real_path of the backing inode. This is done by embedding the
1165 : * returned file into a container structure that also stores the path of
1166 : * the backing inode on the underlying filesystem, which can be
1167 : * retrieved using backing_file_real_path().
1168 : */
1169 18366152 : struct file *backing_file_open(const struct path *path, int flags,
1170 : const struct path *real_path,
1171 : const struct cred *cred)
1172 : {
1173 18366152 : struct file *f;
1174 18366152 : int error;
1175 :
1176 18366152 : f = alloc_empty_backing_file(flags, cred);
1177 18377642 : if (IS_ERR(f))
1178 : return f;
1179 :
1180 18377642 : f->f_path = *path;
1181 18377642 : path_get(real_path);
1182 18391109 : *backing_file_real_path(f) = *real_path;
1183 18387548 : error = do_dentry_open(f, d_inode(real_path->dentry), NULL);
1184 18391948 : if (error) {
1185 7 : fput(f);
1186 7 : f = ERR_PTR(error);
1187 : }
1188 :
1189 : return f;
1190 : }
1191 : EXPORT_SYMBOL_GPL(backing_file_open);
1192 :
1193 : #define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE))
1194 : #define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC)
1195 :
1196 664032868 : inline struct open_how build_open_how(int flags, umode_t mode)
1197 : {
1198 664032868 : struct open_how how = {
1199 664032868 : .flags = flags & VALID_OPEN_FLAGS,
1200 664032868 : .mode = mode & S_IALLUGO,
1201 : };
1202 :
1203 : /* O_PATH beats everything else. */
1204 664032868 : if (how.flags & O_PATH)
1205 18655739 : how.flags &= O_PATH_FLAGS;
1206 : /* Modes should only be set for create-like flags. */
1207 664032868 : if (!WILL_CREATE(how.flags))
1208 636562167 : how.mode = 0;
1209 664032868 : return how;
1210 : }
1211 :
1212 664047985 : inline int build_open_flags(const struct open_how *how, struct open_flags *op)
1213 : {
1214 664047985 : u64 flags = how->flags;
1215 664047985 : u64 strip = __FMODE_NONOTIFY | O_CLOEXEC;
1216 664047985 : int lookup_flags = 0;
1217 664047985 : int acc_mode = ACC_MODE(flags);
1218 :
1219 664047985 : BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS),
1220 : "struct open_flags doesn't yet handle flags > 32 bits");
1221 :
1222 : /*
1223 : * Strip flags that either shouldn't be set by userspace like
1224 : * FMODE_NONOTIFY or that aren't relevant in determining struct
1225 : * open_flags like O_CLOEXEC.
1226 : */
1227 664047985 : flags &= ~strip;
1228 :
1229 : /*
1230 : * Older syscalls implicitly clear all of the invalid flags or argument
1231 : * values before calling build_open_flags(), but openat2(2) checks all
1232 : * of its arguments.
1233 : */
1234 664047985 : if (flags & ~VALID_OPEN_FLAGS)
1235 : return -EINVAL;
1236 664047985 : if (how->resolve & ~VALID_RESOLVE_FLAGS)
1237 : return -EINVAL;
1238 :
1239 : /* Scoping flags are mutually exclusive. */
1240 664047985 : if ((how->resolve & RESOLVE_BENEATH) && (how->resolve & RESOLVE_IN_ROOT))
1241 : return -EINVAL;
1242 :
1243 : /* Deal with the mode. */
1244 664047985 : if (WILL_CREATE(flags)) {
1245 27496098 : if (how->mode & ~S_IALLUGO)
1246 : return -EINVAL;
1247 27496098 : op->mode = how->mode | S_IFREG;
1248 : } else {
1249 636551887 : if (how->mode != 0)
1250 : return -EINVAL;
1251 636551887 : op->mode = 0;
1252 : }
1253 :
1254 : /*
1255 : * Block bugs where O_DIRECTORY | O_CREAT created regular files.
1256 : * Note, that blocking O_DIRECTORY | O_CREAT here also protects
1257 : * O_TMPFILE below which requires O_DIRECTORY being raised.
1258 : */
1259 664047985 : if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT))
1260 : return -EINVAL;
1261 :
1262 : /* Now handle the creative implementation of O_TMPFILE. */
1263 664047985 : if (flags & __O_TMPFILE) {
1264 : /*
1265 : * In order to ensure programs get explicit errors when trying
1266 : * to use O_TMPFILE on old kernels we enforce that O_DIRECTORY
1267 : * is raised alongside __O_TMPFILE.
1268 : */
1269 959589 : if (!(flags & O_DIRECTORY))
1270 : return -EINVAL;
1271 959589 : if (!(acc_mode & MAY_WRITE))
1272 : return -EINVAL;
1273 : }
1274 664047983 : if (flags & O_PATH) {
1275 : /* O_PATH only permits certain other flags to be set. */
1276 18655378 : if (flags & ~O_PATH_FLAGS)
1277 : return -EINVAL;
1278 : acc_mode = 0;
1279 : }
1280 :
1281 : /*
1282 : * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
1283 : * check for O_DSYNC if the need any syncing at all we enforce it's
1284 : * always set instead of having to deal with possibly weird behaviour
1285 : * for malicious applications setting only __O_SYNC.
1286 : */
1287 664047983 : if (flags & __O_SYNC)
1288 13508 : flags |= O_DSYNC;
1289 :
1290 664047983 : op->open_flag = flags;
1291 :
1292 : /* O_TRUNC implies we need access checks for write permissions */
1293 664047983 : if (flags & O_TRUNC)
1294 21150926 : acc_mode |= MAY_WRITE;
1295 :
1296 : /* Allow the LSM permission hook to distinguish append
1297 : access from general write access. */
1298 664047983 : if (flags & O_APPEND)
1299 1513652 : acc_mode |= MAY_APPEND;
1300 :
1301 664047983 : op->acc_mode = acc_mode;
1302 :
1303 664047983 : op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
1304 :
1305 664047983 : if (flags & O_CREAT) {
1306 26536814 : op->intent |= LOOKUP_CREATE;
1307 26536814 : if (flags & O_EXCL) {
1308 2709462 : op->intent |= LOOKUP_EXCL;
1309 2709462 : flags |= O_NOFOLLOW;
1310 : }
1311 : }
1312 :
1313 664047983 : if (flags & O_DIRECTORY)
1314 14170373 : lookup_flags |= LOOKUP_DIRECTORY;
1315 664047983 : if (!(flags & O_NOFOLLOW))
1316 642029616 : lookup_flags |= LOOKUP_FOLLOW;
1317 :
1318 664047983 : if (how->resolve & RESOLVE_NO_XDEV)
1319 0 : lookup_flags |= LOOKUP_NO_XDEV;
1320 664047983 : if (how->resolve & RESOLVE_NO_MAGICLINKS)
1321 0 : lookup_flags |= LOOKUP_NO_MAGICLINKS;
1322 664047983 : if (how->resolve & RESOLVE_NO_SYMLINKS)
1323 0 : lookup_flags |= LOOKUP_NO_SYMLINKS;
1324 664047983 : if (how->resolve & RESOLVE_BENEATH)
1325 0 : lookup_flags |= LOOKUP_BENEATH;
1326 664047983 : if (how->resolve & RESOLVE_IN_ROOT)
1327 0 : lookup_flags |= LOOKUP_IN_ROOT;
1328 664047983 : if (how->resolve & RESOLVE_CACHED) {
1329 : /* Don't bother even trying for create/truncate/tmpfile open */
1330 0 : if (flags & (O_TRUNC | O_CREAT | O_TMPFILE))
1331 : return -EAGAIN;
1332 0 : lookup_flags |= LOOKUP_CACHED;
1333 : }
1334 :
1335 664047983 : op->lookup_flags = lookup_flags;
1336 664047983 : return 0;
1337 : }
1338 :
1339 : /**
1340 : * file_open_name - open file and return file pointer
1341 : *
1342 : * @name: struct filename containing path to open
1343 : * @flags: open flags as per the open(2) second argument
1344 : * @mode: mode for the new file if O_CREAT is set, else ignored
1345 : *
1346 : * This is the helper to open a file from kernelspace if you really
1347 : * have to. But in generally you should not do this, so please move
1348 : * along, nothing to see here..
1349 : */
1350 22566 : struct file *file_open_name(struct filename *name, int flags, umode_t mode)
1351 : {
1352 22566 : struct open_flags op;
1353 22566 : struct open_how how = build_open_how(flags, mode);
1354 22566 : int err = build_open_flags(&how, &op);
1355 22566 : if (err)
1356 0 : return ERR_PTR(err);
1357 22566 : return do_filp_open(AT_FDCWD, name, &op);
1358 : }
1359 :
1360 : /**
1361 : * filp_open - open file and return file pointer
1362 : *
1363 : * @filename: path to open
1364 : * @flags: open flags as per the open(2) second argument
1365 : * @mode: mode for the new file if O_CREAT is set, else ignored
1366 : *
1367 : * This is the helper to open a file from kernelspace if you really
1368 : * have to. But in generally you should not do this, so please move
1369 : * along, nothing to see here..
1370 : */
1371 22402 : struct file *filp_open(const char *filename, int flags, umode_t mode)
1372 : {
1373 22402 : struct filename *name = getname_kernel(filename);
1374 22402 : struct file *file = ERR_CAST(name);
1375 :
1376 22402 : if (!IS_ERR(name)) {
1377 22402 : file = file_open_name(name, flags, mode);
1378 22402 : putname(name);
1379 : }
1380 22402 : return file;
1381 : }
1382 : EXPORT_SYMBOL(filp_open);
1383 :
1384 6376 : struct file *file_open_root(const struct path *root,
1385 : const char *filename, int flags, umode_t mode)
1386 : {
1387 6376 : struct open_flags op;
1388 6376 : struct open_how how = build_open_how(flags, mode);
1389 6376 : int err = build_open_flags(&how, &op);
1390 6376 : if (err)
1391 0 : return ERR_PTR(err);
1392 6376 : return do_file_open_root(root, filename, &op);
1393 : }
1394 : EXPORT_SYMBOL(file_open_root);
1395 :
1396 663973218 : static long do_sys_openat2(int dfd, const char __user *filename,
1397 : struct open_how *how)
1398 : {
1399 663973218 : struct open_flags op;
1400 663973218 : int fd = build_open_flags(how, &op);
1401 663988894 : struct filename *tmp;
1402 :
1403 663988894 : if (fd)
1404 2 : return fd;
1405 :
1406 663988892 : tmp = getname(filename);
1407 663978687 : if (IS_ERR(tmp))
1408 998 : return PTR_ERR(tmp);
1409 :
1410 663977689 : fd = get_unused_fd_flags(how->flags);
1411 664002587 : if (fd >= 0) {
1412 664002801 : struct file *f = do_filp_open(dfd, tmp, &op);
1413 664083366 : if (IS_ERR(f)) {
1414 53007527 : put_unused_fd(fd);
1415 53005250 : fd = PTR_ERR(f);
1416 : } else {
1417 611075839 : fd_install(fd, f);
1418 : }
1419 : }
1420 664081596 : putname(tmp);
1421 664090781 : return fd;
1422 : }
1423 :
1424 664040880 : long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
1425 : {
1426 664040880 : struct open_how how = build_open_how(flags, mode);
1427 664040880 : return do_sys_openat2(dfd, filename, &how);
1428 : }
1429 :
1430 :
1431 0 : SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1432 : {
1433 0 : if (force_o_largefile())
1434 0 : flags |= O_LARGEFILE;
1435 0 : return do_sys_open(AT_FDCWD, filename, flags, mode);
1436 : }
1437 :
1438 1328147597 : SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
1439 : umode_t, mode)
1440 : {
1441 664041047 : if (force_o_largefile())
1442 664041047 : flags |= O_LARGEFILE;
1443 664041047 : return do_sys_open(dfd, filename, flags, mode);
1444 : }
1445 :
1446 0 : SYSCALL_DEFINE4(openat2, int, dfd, const char __user *, filename,
1447 : struct open_how __user *, how, size_t, usize)
1448 : {
1449 0 : int err;
1450 0 : struct open_how tmp;
1451 :
1452 0 : BUILD_BUG_ON(sizeof(struct open_how) < OPEN_HOW_SIZE_VER0);
1453 0 : BUILD_BUG_ON(sizeof(struct open_how) != OPEN_HOW_SIZE_LATEST);
1454 :
1455 0 : if (unlikely(usize < OPEN_HOW_SIZE_VER0))
1456 : return -EINVAL;
1457 :
1458 0 : err = copy_struct_from_user(&tmp, sizeof(tmp), how, usize);
1459 0 : if (err)
1460 0 : return err;
1461 :
1462 0 : audit_openat2_how(&tmp);
1463 :
1464 : /* O_LARGEFILE is only allowed for non-O_PATH. */
1465 0 : if (!(tmp.flags & O_PATH) && force_o_largefile())
1466 0 : tmp.flags |= O_LARGEFILE;
1467 :
1468 0 : return do_sys_openat2(dfd, filename, &tmp);
1469 : }
1470 :
1471 : #ifdef CONFIG_COMPAT
1472 : /*
1473 : * Exactly like sys_open(), except that it doesn't set the
1474 : * O_LARGEFILE flag.
1475 : */
1476 : COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1477 : {
1478 : return do_sys_open(AT_FDCWD, filename, flags, mode);
1479 : }
1480 :
1481 : /*
1482 : * Exactly like sys_openat(), except that it doesn't set the
1483 : * O_LARGEFILE flag.
1484 : */
1485 : COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
1486 : {
1487 : return do_sys_open(dfd, filename, flags, mode);
1488 : }
1489 : #endif
1490 :
1491 : #ifndef __alpha__
1492 :
1493 : /*
1494 : * For backward compatibility? Maybe this should be moved
1495 : * into arch/i386 instead?
1496 : */
1497 0 : SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
1498 : {
1499 0 : int flags = O_CREAT | O_WRONLY | O_TRUNC;
1500 :
1501 0 : if (force_o_largefile())
1502 0 : flags |= O_LARGEFILE;
1503 0 : return do_sys_open(AT_FDCWD, pathname, flags, mode);
1504 : }
1505 : #endif
1506 :
1507 : /*
1508 : * "id" is the POSIX thread ID. We use the
1509 : * files pointer for this..
1510 : */
1511 817753384 : int filp_close(struct file *filp, fl_owner_t id)
1512 : {
1513 817753384 : int retval = 0;
1514 :
1515 817753384 : if (CHECK_DATA_CORRUPTION(file_count(filp) == 0,
1516 : "VFS: Close: file count is 0 (f_op=%ps)",
1517 : filp->f_op)) {
1518 : return 0;
1519 : }
1520 :
1521 817753384 : if (filp->f_op->flush)
1522 100064567 : retval = filp->f_op->flush(filp, id);
1523 :
1524 817739608 : if (likely(!(filp->f_mode & FMODE_PATH))) {
1525 799571894 : dnotify_flush(filp, id);
1526 799488962 : locks_remove_posix(filp, id);
1527 : }
1528 817747333 : fput(filp);
1529 817797052 : return retval;
1530 : }
1531 :
1532 : EXPORT_SYMBOL(filp_close);
1533 :
1534 : /*
1535 : * Careful here! We test whether the file pointer is NULL before
1536 : * releasing the fd. This ensures that one clone task can't release
1537 : * an fd while another clone is opening it.
1538 : */
1539 1466608796 : SYSCALL_DEFINE1(close, unsigned int, fd)
1540 : {
1541 733313620 : int retval = close_fd(fd);
1542 :
1543 : /* can't restart close syscall because file table entry was cleared */
1544 733379013 : if (unlikely(retval == -ERESTARTSYS ||
1545 : retval == -ERESTARTNOINTR ||
1546 : retval == -ERESTARTNOHAND ||
1547 : retval == -ERESTART_RESTARTBLOCK))
1548 0 : retval = -EINTR;
1549 :
1550 733379013 : return retval;
1551 : }
1552 :
1553 : /**
1554 : * close_range() - Close all file descriptors in a given range.
1555 : *
1556 : * @fd: starting file descriptor to close
1557 : * @max_fd: last file descriptor to close
1558 : * @flags: reserved for future extensions
1559 : *
1560 : * This closes a range of file descriptors. All file descriptors
1561 : * from @fd up to and including @max_fd are closed.
1562 : * Currently, errors to close a given file descriptor are ignored.
1563 : */
1564 87800 : SYSCALL_DEFINE3(close_range, unsigned int, fd, unsigned int, max_fd,
1565 : unsigned int, flags)
1566 : {
1567 43899 : return __close_range(fd, max_fd, flags);
1568 : }
1569 :
1570 : /*
1571 : * This routine simulates a hangup on the tty, to arrange that users
1572 : * are given clean terminals at login time.
1573 : */
1574 0 : SYSCALL_DEFINE0(vhangup)
1575 : {
1576 0 : if (capable(CAP_SYS_TTY_CONFIG)) {
1577 0 : tty_vhangup_self();
1578 0 : return 0;
1579 : }
1580 : return -EPERM;
1581 : }
1582 :
1583 : /*
1584 : * Called when an inode is about to be open.
1585 : * We use this to disallow opening large files on 32bit systems if
1586 : * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1587 : * on this flag in sys_open.
1588 : */
1589 495919992 : int generic_file_open(struct inode * inode, struct file * filp)
1590 : {
1591 495919992 : if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1592 0 : return -EOVERFLOW;
1593 : return 0;
1594 : }
1595 :
1596 : EXPORT_SYMBOL(generic_file_open);
1597 :
1598 : /*
1599 : * This is used by subsystems that don't want seekable
1600 : * file descriptors. The function is not supposed to ever fail, the only
1601 : * reason it returns an 'int' and not 'void' is so that it can be plugged
1602 : * directly into file_operations structure.
1603 : */
1604 81548 : int nonseekable_open(struct inode *inode, struct file *filp)
1605 : {
1606 81548 : filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1607 81548 : return 0;
1608 : }
1609 :
1610 : EXPORT_SYMBOL(nonseekable_open);
1611 :
1612 : /*
1613 : * stream_open is used by subsystems that want stream-like file descriptors.
1614 : * Such file descriptors are not seekable and don't have notion of position
1615 : * (file.f_pos is always 0 and ppos passed to .read()/.write() is always NULL).
1616 : * Contrary to file descriptors of other regular files, .read() and .write()
1617 : * can run simultaneously.
1618 : *
1619 : * stream_open never fails and is marked to return int so that it could be
1620 : * directly used as file_operations.open .
1621 : */
1622 29682171 : int stream_open(struct inode *inode, struct file *filp)
1623 : {
1624 29682171 : filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS);
1625 29682171 : filp->f_mode |= FMODE_STREAM;
1626 29682171 : return 0;
1627 : }
1628 :
1629 : EXPORT_SYMBOL(stream_open);
|