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