]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/open.c
mfd: intel-lpss: Remove D3cold delay
[mirror_ubuntu-bionic-kernel.git] / fs / open.c
1 /*
2 * linux/fs/open.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7 #include <linux/string.h>
8 #include <linux/mm.h>
9 #include <linux/file.h>
10 #include <linux/fdtable.h>
11 #include <linux/fsnotify.h>
12 #include <linux/module.h>
13 #include <linux/tty.h>
14 #include <linux/namei.h>
15 #include <linux/backing-dev.h>
16 #include <linux/capability.h>
17 #include <linux/securebits.h>
18 #include <linux/security.h>
19 #include <linux/mount.h>
20 #include <linux/fcntl.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <linux/fs.h>
24 #include <linux/personality.h>
25 #include <linux/pagemap.h>
26 #include <linux/syscalls.h>
27 #include <linux/rcupdate.h>
28 #include <linux/audit.h>
29 #include <linux/falloc.h>
30 #include <linux/fs_struct.h>
31 #include <linux/ima.h>
32 #include <linux/dnotify.h>
33 #include <linux/compat.h>
34
35 #include "internal.h"
36
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/fs.h>
39
40 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
41 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(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(dentry, &newattrs, NULL);
67 inode_unlock(dentry->d_inode);
68 return ret;
69 }
70 EXPORT_SYMBOL_GPL(do_truncate);
71
72 long vfs_truncate(const struct path *path, loff_t length)
73 {
74 struct inode *inode;
75 struct dentry *upperdentry;
76 long error;
77
78 inode = path->dentry->d_inode;
79
80 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
81 if (S_ISDIR(inode->i_mode))
82 return -EISDIR;
83 if (!S_ISREG(inode->i_mode))
84 return -EINVAL;
85
86 error = mnt_want_write(path->mnt);
87 if (error)
88 goto out;
89
90 error = inode_permission(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 /*
99 * If this is an overlayfs then do as if opening the file so we get
100 * write access on the upper inode, not on the overlay inode. For
101 * non-overlay filesystems d_real() is an identity function.
102 */
103 upperdentry = d_real(path->dentry, NULL, O_WRONLY, 0);
104 error = PTR_ERR(upperdentry);
105 if (IS_ERR(upperdentry))
106 goto mnt_drop_write_and_out;
107
108 error = get_write_access(upperdentry->d_inode);
109 if (error)
110 goto mnt_drop_write_and_out;
111
112 /*
113 * Make sure that there are no leases. get_write_access() protects
114 * against the truncate racing with a lease-granting setlease().
115 */
116 error = break_lease(inode, O_WRONLY);
117 if (error)
118 goto put_write_and_out;
119
120 error = locks_verify_truncate(inode, NULL, length);
121 if (!error)
122 error = security_path_truncate(path);
123 if (!error)
124 error = do_truncate(path->dentry, length, 0, NULL);
125
126 put_write_and_out:
127 put_write_access(upperdentry->d_inode);
128 mnt_drop_write_and_out:
129 mnt_drop_write(path->mnt);
130 out:
131 return error;
132 }
133 EXPORT_SYMBOL_GPL(vfs_truncate);
134
135 static long do_sys_truncate(const char __user *pathname, loff_t length)
136 {
137 unsigned int lookup_flags = LOOKUP_FOLLOW;
138 struct path path;
139 int error;
140
141 if (length < 0) /* sorry, but loff_t says... */
142 return -EINVAL;
143
144 retry:
145 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
146 if (!error) {
147 error = vfs_truncate(&path, length);
148 path_put(&path);
149 }
150 if (retry_estale(error, lookup_flags)) {
151 lookup_flags |= LOOKUP_REVAL;
152 goto retry;
153 }
154 return error;
155 }
156
157 SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
158 {
159 return do_sys_truncate(path, length);
160 }
161
162 #ifdef CONFIG_COMPAT
163 COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
164 {
165 return do_sys_truncate(path, length);
166 }
167 #endif
168
169 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
170 {
171 struct inode *inode;
172 struct dentry *dentry;
173 struct fd f;
174 int error;
175
176 error = -EINVAL;
177 if (length < 0)
178 goto out;
179 error = -EBADF;
180 f = fdget(fd);
181 if (!f.file)
182 goto out;
183
184 /* explicitly opened as large or we are on 64-bit box */
185 if (f.file->f_flags & O_LARGEFILE)
186 small = 0;
187
188 dentry = f.file->f_path.dentry;
189 inode = dentry->d_inode;
190 error = -EINVAL;
191 if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
192 goto out_putf;
193
194 error = -EINVAL;
195 /* Cannot ftruncate over 2^31 bytes without large file support */
196 if (small && length > MAX_NON_LFS)
197 goto out_putf;
198
199 error = -EPERM;
200 /* Check IS_APPEND on real upper inode */
201 if (IS_APPEND(file_inode(f.file)))
202 goto out_putf;
203
204 sb_start_write(inode->i_sb);
205 error = locks_verify_truncate(inode, f.file, length);
206 if (!error)
207 error = security_path_truncate(&f.file->f_path);
208 if (!error)
209 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
210 sb_end_write(inode->i_sb);
211 out_putf:
212 fdput(f);
213 out:
214 return error;
215 }
216
217 SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
218 {
219 return do_sys_ftruncate(fd, length, 1);
220 }
221
222 #ifdef CONFIG_COMPAT
223 COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
224 {
225 return do_sys_ftruncate(fd, length, 1);
226 }
227 #endif
228
229 /* LFS versions of truncate are only needed on 32 bit machines */
230 #if BITS_PER_LONG == 32
231 SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
232 {
233 return do_sys_truncate(path, length);
234 }
235
236 SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
237 {
238 return do_sys_ftruncate(fd, length, 0);
239 }
240 #endif /* BITS_PER_LONG == 32 */
241
242
243 int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
244 {
245 struct inode *inode = file_inode(file);
246 long ret;
247
248 if (offset < 0 || len <= 0)
249 return -EINVAL;
250
251 /* Return error if mode is not supported */
252 if (mode & ~FALLOC_FL_SUPPORTED_MASK)
253 return -EOPNOTSUPP;
254
255 /* Punch hole and zero range are mutually exclusive */
256 if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
257 (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
258 return -EOPNOTSUPP;
259
260 /* Punch hole must have keep size set */
261 if ((mode & FALLOC_FL_PUNCH_HOLE) &&
262 !(mode & FALLOC_FL_KEEP_SIZE))
263 return -EOPNOTSUPP;
264
265 /* Collapse range should only be used exclusively. */
266 if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
267 (mode & ~FALLOC_FL_COLLAPSE_RANGE))
268 return -EINVAL;
269
270 /* Insert range should only be used exclusively. */
271 if ((mode & FALLOC_FL_INSERT_RANGE) &&
272 (mode & ~FALLOC_FL_INSERT_RANGE))
273 return -EINVAL;
274
275 /* Unshare range should only be used with allocate mode. */
276 if ((mode & FALLOC_FL_UNSHARE_RANGE) &&
277 (mode & ~(FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_KEEP_SIZE)))
278 return -EINVAL;
279
280 if (!(file->f_mode & FMODE_WRITE))
281 return -EBADF;
282
283 /*
284 * We can only allow pure fallocate on append only files
285 */
286 if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
287 return -EPERM;
288
289 if (IS_IMMUTABLE(inode))
290 return -EPERM;
291
292 /*
293 * We cannot allow any fallocate operation on an active swapfile
294 */
295 if (IS_SWAPFILE(inode))
296 return -ETXTBSY;
297
298 /*
299 * Revalidate the write permissions, in case security policy has
300 * changed since the files were opened.
301 */
302 ret = security_file_permission(file, MAY_WRITE);
303 if (ret)
304 return ret;
305
306 if (S_ISFIFO(inode->i_mode))
307 return -ESPIPE;
308
309 if (S_ISDIR(inode->i_mode))
310 return -EISDIR;
311
312 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
313 return -ENODEV;
314
315 /* Check for wrap through zero too */
316 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
317 return -EFBIG;
318
319 if (!file->f_op->fallocate)
320 return -EOPNOTSUPP;
321
322 file_start_write(file);
323 ret = file->f_op->fallocate(file, mode, offset, len);
324
325 /*
326 * Create inotify and fanotify events.
327 *
328 * To keep the logic simple always create events if fallocate succeeds.
329 * This implies that events are even created if the file size remains
330 * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
331 */
332 if (ret == 0)
333 fsnotify_modify(file);
334
335 file_end_write(file);
336 return ret;
337 }
338 EXPORT_SYMBOL_GPL(vfs_fallocate);
339
340 SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
341 {
342 struct fd f = fdget(fd);
343 int error = -EBADF;
344
345 if (f.file) {
346 error = vfs_fallocate(f.file, mode, offset, len);
347 fdput(f);
348 }
349 return error;
350 }
351
352 /*
353 * access() needs to use the real uid/gid, not the effective uid/gid.
354 * We do this by temporarily clearing all FS-related capabilities and
355 * switching the fsuid/fsgid around to the real ones.
356 */
357 SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
358 {
359 const struct cred *old_cred;
360 struct cred *override_cred;
361 struct path path;
362 struct inode *inode;
363 int res;
364 unsigned int lookup_flags = LOOKUP_FOLLOW;
365
366 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
367 return -EINVAL;
368
369 override_cred = prepare_creds();
370 if (!override_cred)
371 return -ENOMEM;
372
373 override_cred->fsuid = override_cred->uid;
374 override_cred->fsgid = override_cred->gid;
375
376 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
377 /* Clear the capabilities if we switch to a non-root user */
378 kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
379 if (!uid_eq(override_cred->uid, root_uid))
380 cap_clear(override_cred->cap_effective);
381 else
382 override_cred->cap_effective =
383 override_cred->cap_permitted;
384 }
385
386 /*
387 * The new set of credentials can *only* be used in
388 * task-synchronous circumstances, and does not need
389 * RCU freeing, unless somebody then takes a separate
390 * reference to it.
391 *
392 * NOTE! This is _only_ true because this credential
393 * is used purely for override_creds() that installs
394 * it as the subjective cred. Other threads will be
395 * accessing ->real_cred, not the subjective cred.
396 *
397 * If somebody _does_ make a copy of this (using the
398 * 'get_current_cred()' function), that will clear the
399 * non_rcu field, because now that other user may be
400 * expecting RCU freeing. But normal thread-synchronous
401 * cred accesses will keep things non-RCY.
402 */
403 override_cred->non_rcu = 1;
404
405 old_cred = override_creds(override_cred);
406 retry:
407 res = user_path_at(dfd, filename, lookup_flags, &path);
408 if (res)
409 goto out;
410
411 inode = d_backing_inode(path.dentry);
412
413 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
414 /*
415 * MAY_EXEC on regular files is denied if the fs is mounted
416 * with the "noexec" flag.
417 */
418 res = -EACCES;
419 if (path_noexec(&path))
420 goto out_path_release;
421 }
422
423 res = inode_permission(inode, mode | MAY_ACCESS);
424 /* SuS v2 requires we report a read only fs too */
425 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
426 goto out_path_release;
427 /*
428 * This is a rare case where using __mnt_is_readonly()
429 * is OK without a mnt_want/drop_write() pair. Since
430 * no actual write to the fs is performed here, we do
431 * not need to telegraph to that to anyone.
432 *
433 * By doing this, we accept that this access is
434 * inherently racy and know that the fs may change
435 * state before we even see this result.
436 */
437 if (__mnt_is_readonly(path.mnt))
438 res = -EROFS;
439
440 out_path_release:
441 path_put(&path);
442 if (retry_estale(res, lookup_flags)) {
443 lookup_flags |= LOOKUP_REVAL;
444 goto retry;
445 }
446 out:
447 revert_creds(old_cred);
448 put_cred(override_cred);
449 return res;
450 }
451
452 SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
453 {
454 return sys_faccessat(AT_FDCWD, filename, mode);
455 }
456
457 SYSCALL_DEFINE1(chdir, const char __user *, filename)
458 {
459 struct path path;
460 int error;
461 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
462 retry:
463 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
464 if (error)
465 goto out;
466
467 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
468 if (error)
469 goto dput_and_out;
470
471 set_fs_pwd(current->fs, &path);
472
473 dput_and_out:
474 path_put(&path);
475 if (retry_estale(error, lookup_flags)) {
476 lookup_flags |= LOOKUP_REVAL;
477 goto retry;
478 }
479 out:
480 return error;
481 }
482
483 SYSCALL_DEFINE1(fchdir, unsigned int, fd)
484 {
485 struct fd f = fdget_raw(fd);
486 int error;
487
488 error = -EBADF;
489 if (!f.file)
490 goto out;
491
492 error = -ENOTDIR;
493 if (!d_can_lookup(f.file->f_path.dentry))
494 goto out_putf;
495
496 error = inode_permission(file_inode(f.file), MAY_EXEC | MAY_CHDIR);
497 if (!error)
498 set_fs_pwd(current->fs, &f.file->f_path);
499 out_putf:
500 fdput(f);
501 out:
502 return error;
503 }
504
505 SYSCALL_DEFINE1(chroot, const char __user *, filename)
506 {
507 struct path path;
508 int error;
509 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
510 retry:
511 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
512 if (error)
513 goto out;
514
515 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
516 if (error)
517 goto dput_and_out;
518
519 error = -EPERM;
520 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
521 goto dput_and_out;
522 error = security_path_chroot(&path);
523 if (error)
524 goto dput_and_out;
525
526 set_fs_root(current->fs, &path);
527 error = 0;
528 dput_and_out:
529 path_put(&path);
530 if (retry_estale(error, lookup_flags)) {
531 lookup_flags |= LOOKUP_REVAL;
532 goto retry;
533 }
534 out:
535 return error;
536 }
537
538 static int chmod_common(const struct path *path, umode_t mode)
539 {
540 struct inode *inode = path->dentry->d_inode;
541 struct inode *delegated_inode = NULL;
542 struct iattr newattrs;
543 int error;
544
545 error = mnt_want_write(path->mnt);
546 if (error)
547 return error;
548 retry_deleg:
549 inode_lock(inode);
550 error = security_path_chmod(path, mode);
551 if (error)
552 goto out_unlock;
553 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
554 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
555 error = notify_change(path->dentry, &newattrs, &delegated_inode);
556 out_unlock:
557 inode_unlock(inode);
558 if (delegated_inode) {
559 error = break_deleg_wait(&delegated_inode);
560 if (!error)
561 goto retry_deleg;
562 }
563 mnt_drop_write(path->mnt);
564 return error;
565 }
566
567 SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
568 {
569 struct fd f = fdget(fd);
570 int err = -EBADF;
571
572 if (f.file) {
573 audit_file(f.file);
574 err = chmod_common(&f.file->f_path, mode);
575 fdput(f);
576 }
577 return err;
578 }
579
580 SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
581 {
582 struct path path;
583 int error;
584 unsigned int lookup_flags = LOOKUP_FOLLOW;
585 retry:
586 error = user_path_at(dfd, filename, lookup_flags, &path);
587 if (!error) {
588 error = chmod_common(&path, mode);
589 path_put(&path);
590 if (retry_estale(error, lookup_flags)) {
591 lookup_flags |= LOOKUP_REVAL;
592 goto retry;
593 }
594 }
595 return error;
596 }
597
598 SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
599 {
600 return sys_fchmodat(AT_FDCWD, filename, mode);
601 }
602
603 static int chown_common(const struct path *path, uid_t user, gid_t group)
604 {
605 struct inode *inode = path->dentry->d_inode;
606 struct inode *delegated_inode = NULL;
607 int error;
608 struct iattr newattrs;
609 kuid_t uid;
610 kgid_t gid;
611
612 uid = make_kuid(current_user_ns(), user);
613 gid = make_kgid(current_user_ns(), group);
614
615 retry_deleg:
616 newattrs.ia_valid = ATTR_CTIME;
617 if (user != (uid_t) -1) {
618 if (!uid_valid(uid))
619 return -EINVAL;
620 newattrs.ia_valid |= ATTR_UID;
621 newattrs.ia_uid = uid;
622 }
623 if (group != (gid_t) -1) {
624 if (!gid_valid(gid))
625 return -EINVAL;
626 newattrs.ia_valid |= ATTR_GID;
627 newattrs.ia_gid = gid;
628 }
629 if (!S_ISDIR(inode->i_mode))
630 newattrs.ia_valid |=
631 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
632 inode_lock(inode);
633 error = security_path_chown(path, uid, gid);
634 if (!error)
635 error = notify_change(path->dentry, &newattrs, &delegated_inode);
636 inode_unlock(inode);
637 if (delegated_inode) {
638 error = break_deleg_wait(&delegated_inode);
639 if (!error)
640 goto retry_deleg;
641 }
642 return error;
643 }
644
645 SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
646 gid_t, group, int, flag)
647 {
648 struct path path;
649 int error = -EINVAL;
650 int lookup_flags;
651
652 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
653 goto out;
654
655 lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
656 if (flag & AT_EMPTY_PATH)
657 lookup_flags |= LOOKUP_EMPTY;
658 retry:
659 error = user_path_at(dfd, filename, lookup_flags, &path);
660 if (error)
661 goto out;
662 error = mnt_want_write(path.mnt);
663 if (error)
664 goto out_release;
665 error = chown_common(&path, user, group);
666 mnt_drop_write(path.mnt);
667 out_release:
668 path_put(&path);
669 if (retry_estale(error, lookup_flags)) {
670 lookup_flags |= LOOKUP_REVAL;
671 goto retry;
672 }
673 out:
674 return error;
675 }
676
677 SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
678 {
679 return sys_fchownat(AT_FDCWD, filename, user, group, 0);
680 }
681
682 SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
683 {
684 return sys_fchownat(AT_FDCWD, filename, user, group,
685 AT_SYMLINK_NOFOLLOW);
686 }
687
688 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
689 {
690 struct fd f = fdget(fd);
691 int error = -EBADF;
692
693 if (!f.file)
694 goto out;
695
696 error = mnt_want_write_file_path(f.file);
697 if (error)
698 goto out_fput;
699 audit_file(f.file);
700 error = chown_common(&f.file->f_path, user, group);
701 mnt_drop_write_file_path(f.file);
702 out_fput:
703 fdput(f);
704 out:
705 return error;
706 }
707
708 int open_check_o_direct(struct file *f)
709 {
710 /* NB: we're sure to have correct a_ops only after f_op->open */
711 if (f->f_flags & O_DIRECT) {
712 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
713 return -EINVAL;
714 }
715 return 0;
716 }
717 EXPORT_SYMBOL_GPL(open_check_o_direct);
718
719 static int do_dentry_open(struct file *f,
720 struct inode *inode,
721 int (*open)(struct inode *, struct file *),
722 const struct cred *cred)
723 {
724 static const struct file_operations empty_fops = {};
725 int error;
726
727 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
728 FMODE_PREAD | FMODE_PWRITE;
729
730 path_get(&f->f_path);
731 f->f_inode = inode;
732 f->f_mapping = inode->i_mapping;
733
734 /* Ensure that we skip any errors that predate opening of the file */
735 f->f_wb_err = filemap_sample_wb_err(f->f_mapping);
736
737 if (unlikely(f->f_flags & O_PATH)) {
738 f->f_mode = FMODE_PATH;
739 f->f_op = &empty_fops;
740 return 0;
741 }
742
743 /* Any file opened for execve()/uselib() has to be a regular file. */
744 if (unlikely(f->f_flags & FMODE_EXEC && !S_ISREG(inode->i_mode))) {
745 error = -EACCES;
746 goto cleanup_file;
747 }
748
749 if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
750 error = get_write_access(inode);
751 if (unlikely(error))
752 goto cleanup_file;
753 error = __mnt_want_write(f->f_path.mnt);
754 if (unlikely(error)) {
755 put_write_access(inode);
756 goto cleanup_file;
757 }
758 f->f_mode |= FMODE_WRITER;
759 }
760
761 /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
762 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
763 f->f_mode |= FMODE_ATOMIC_POS;
764
765 f->f_op = fops_get(inode->i_fop);
766 if (unlikely(WARN_ON(!f->f_op))) {
767 error = -ENODEV;
768 goto cleanup_all;
769 }
770
771 error = security_file_open(f, cred);
772 if (error)
773 goto cleanup_all;
774
775 error = break_lease(locks_inode(f), f->f_flags);
776 if (error)
777 goto cleanup_all;
778
779 if (!open)
780 open = f->f_op->open;
781 if (open) {
782 error = open(inode, f);
783 if (error)
784 goto cleanup_all;
785 }
786 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
787 i_readcount_inc(inode);
788 if ((f->f_mode & FMODE_READ) &&
789 likely(f->f_op->read || f->f_op->read_iter))
790 f->f_mode |= FMODE_CAN_READ;
791 if ((f->f_mode & FMODE_WRITE) &&
792 likely(f->f_op->write || f->f_op->write_iter))
793 f->f_mode |= FMODE_CAN_WRITE;
794
795 f->f_write_hint = WRITE_LIFE_NOT_SET;
796 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
797
798 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
799
800 return 0;
801
802 cleanup_all:
803 fops_put(f->f_op);
804 if (f->f_mode & FMODE_WRITER) {
805 put_write_access(inode);
806 __mnt_drop_write(f->f_path.mnt);
807 }
808 cleanup_file:
809 path_put(&f->f_path);
810 f->f_path.mnt = NULL;
811 f->f_path.dentry = NULL;
812 f->f_inode = NULL;
813 return error;
814 }
815
816 /**
817 * finish_open - finish opening a file
818 * @file: file pointer
819 * @dentry: pointer to dentry
820 * @open: open callback
821 * @opened: state of open
822 *
823 * This can be used to finish opening a file passed to i_op->atomic_open().
824 *
825 * If the open callback is set to NULL, then the standard f_op->open()
826 * filesystem callback is substituted.
827 *
828 * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
829 * the return value of d_splice_alias(), then the caller needs to perform dput()
830 * on it after finish_open().
831 *
832 * On successful return @file is a fully instantiated open file. After this, if
833 * an error occurs in ->atomic_open(), it needs to clean up with fput().
834 *
835 * Returns zero on success or -errno if the open failed.
836 */
837 int finish_open(struct file *file, struct dentry *dentry,
838 int (*open)(struct inode *, struct file *),
839 int *opened)
840 {
841 int error;
842 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
843
844 file->f_path.dentry = dentry;
845 error = do_dentry_open(file, d_backing_inode(dentry), open,
846 current_cred());
847 if (!error)
848 *opened |= FILE_OPENED;
849
850 return error;
851 }
852 EXPORT_SYMBOL(finish_open);
853
854 /**
855 * finish_no_open - finish ->atomic_open() without opening the file
856 *
857 * @file: file pointer
858 * @dentry: dentry or NULL (as returned from ->lookup())
859 *
860 * This can be used to set the result of a successful lookup in ->atomic_open().
861 *
862 * NB: unlike finish_open() this function does consume the dentry reference and
863 * the caller need not dput() it.
864 *
865 * Returns "1" which must be the return value of ->atomic_open() after having
866 * called this function.
867 */
868 int finish_no_open(struct file *file, struct dentry *dentry)
869 {
870 file->f_path.dentry = dentry;
871 return 1;
872 }
873 EXPORT_SYMBOL(finish_no_open);
874
875 char *file_path(struct file *filp, char *buf, int buflen)
876 {
877 return d_path(&filp->f_path, buf, buflen);
878 }
879 EXPORT_SYMBOL(file_path);
880
881 /**
882 * vfs_open - open the file at the given path
883 * @path: path to open
884 * @file: newly allocated file with f_flag initialized
885 * @cred: credentials to use
886 */
887 int vfs_open(const struct path *path, struct file *file,
888 const struct cred *cred)
889 {
890 struct dentry *dentry = d_real(path->dentry, NULL, file->f_flags, 0);
891
892 if (IS_ERR(dentry))
893 return PTR_ERR(dentry);
894
895 file->f_path = *path;
896 return do_dentry_open(file, d_backing_inode(dentry), NULL, cred);
897 }
898
899 struct file *dentry_open(const struct path *path, int flags,
900 const struct cred *cred)
901 {
902 int error;
903 struct file *f;
904
905 validate_creds(cred);
906
907 /* We must always pass in a valid mount pointer. */
908 BUG_ON(!path->mnt);
909
910 f = get_empty_filp();
911 if (!IS_ERR(f)) {
912 f->f_flags = flags;
913 error = vfs_open(path, f, cred);
914 if (!error) {
915 /* from now on we need fput() to dispose of f */
916 error = open_check_o_direct(f);
917 if (error) {
918 fput(f);
919 f = ERR_PTR(error);
920 }
921 } else {
922 put_filp(f);
923 f = ERR_PTR(error);
924 }
925 }
926 return f;
927 }
928 EXPORT_SYMBOL(dentry_open);
929
930 static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
931 {
932 int lookup_flags = 0;
933 int acc_mode = ACC_MODE(flags);
934
935 /*
936 * Clear out all open flags we don't know about so that we don't report
937 * them in fcntl(F_GETFD) or similar interfaces.
938 */
939 flags &= VALID_OPEN_FLAGS;
940
941 if (flags & (O_CREAT | __O_TMPFILE))
942 op->mode = (mode & S_IALLUGO) | S_IFREG;
943 else
944 op->mode = 0;
945
946 /* Must never be set by userspace */
947 flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
948
949 /*
950 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
951 * check for O_DSYNC if the need any syncing at all we enforce it's
952 * always set instead of having to deal with possibly weird behaviour
953 * for malicious applications setting only __O_SYNC.
954 */
955 if (flags & __O_SYNC)
956 flags |= O_DSYNC;
957
958 if (flags & __O_TMPFILE) {
959 if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
960 return -EINVAL;
961 if (!(acc_mode & MAY_WRITE))
962 return -EINVAL;
963 } else if (flags & O_PATH) {
964 /*
965 * If we have O_PATH in the open flag. Then we
966 * cannot have anything other than the below set of flags
967 */
968 flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
969 acc_mode = 0;
970 }
971
972 op->open_flag = flags;
973
974 /* O_TRUNC implies we need access checks for write permissions */
975 if (flags & O_TRUNC)
976 acc_mode |= MAY_WRITE;
977
978 /* Allow the LSM permission hook to distinguish append
979 access from general write access. */
980 if (flags & O_APPEND)
981 acc_mode |= MAY_APPEND;
982
983 op->acc_mode = acc_mode;
984
985 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
986
987 if (flags & O_CREAT) {
988 op->intent |= LOOKUP_CREATE;
989 if (flags & O_EXCL)
990 op->intent |= LOOKUP_EXCL;
991 }
992
993 if (flags & O_DIRECTORY)
994 lookup_flags |= LOOKUP_DIRECTORY;
995 if (!(flags & O_NOFOLLOW))
996 lookup_flags |= LOOKUP_FOLLOW;
997 op->lookup_flags = lookup_flags;
998 return 0;
999 }
1000
1001 /**
1002 * file_open_name - open file and return file pointer
1003 *
1004 * @name: struct filename containing path to open
1005 * @flags: open flags as per the open(2) second argument
1006 * @mode: mode for the new file if O_CREAT is set, else ignored
1007 *
1008 * This is the helper to open a file from kernelspace if you really
1009 * have to. But in generally you should not do this, so please move
1010 * along, nothing to see here..
1011 */
1012 struct file *file_open_name(struct filename *name, int flags, umode_t mode)
1013 {
1014 struct open_flags op;
1015 int err = build_open_flags(flags, mode, &op);
1016 return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
1017 }
1018
1019 /**
1020 * filp_open - open file and return file pointer
1021 *
1022 * @filename: path to open
1023 * @flags: open flags as per the open(2) second argument
1024 * @mode: mode for the new file if O_CREAT is set, else ignored
1025 *
1026 * This is the helper to open a file from kernelspace if you really
1027 * have to. But in generally you should not do this, so please move
1028 * along, nothing to see here..
1029 */
1030 struct file *filp_open(const char *filename, int flags, umode_t mode)
1031 {
1032 struct filename *name = getname_kernel(filename);
1033 struct file *file = ERR_CAST(name);
1034
1035 if (!IS_ERR(name)) {
1036 file = file_open_name(name, flags, mode);
1037 putname(name);
1038 }
1039 return file;
1040 }
1041 EXPORT_SYMBOL(filp_open);
1042
1043 struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
1044 const char *filename, int flags, umode_t mode)
1045 {
1046 struct open_flags op;
1047 int err = build_open_flags(flags, mode, &op);
1048 if (err)
1049 return ERR_PTR(err);
1050 return do_file_open_root(dentry, mnt, filename, &op);
1051 }
1052 EXPORT_SYMBOL(file_open_root);
1053
1054 struct file *filp_clone_open(struct file *oldfile)
1055 {
1056 struct file *file;
1057 int retval;
1058
1059 file = get_empty_filp();
1060 if (IS_ERR(file))
1061 return file;
1062
1063 file->f_flags = oldfile->f_flags;
1064 retval = vfs_open(&oldfile->f_path, file, oldfile->f_cred);
1065 if (retval) {
1066 put_filp(file);
1067 return ERR_PTR(retval);
1068 }
1069
1070 return file;
1071 }
1072 EXPORT_SYMBOL(filp_clone_open);
1073
1074 long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
1075 {
1076 struct open_flags op;
1077 int fd = build_open_flags(flags, mode, &op);
1078 struct filename *tmp;
1079
1080 if (fd)
1081 return fd;
1082
1083 tmp = getname(filename);
1084 if (IS_ERR(tmp))
1085 return PTR_ERR(tmp);
1086
1087 fd = get_unused_fd_flags(flags);
1088 if (fd >= 0) {
1089 struct file *f = do_filp_open(dfd, tmp, &op);
1090 if (IS_ERR(f)) {
1091 put_unused_fd(fd);
1092 fd = PTR_ERR(f);
1093 } else {
1094 fsnotify_open(f);
1095 fd_install(fd, f);
1096 trace_do_sys_open(tmp->name, flags, mode);
1097 }
1098 }
1099 putname(tmp);
1100 return fd;
1101 }
1102
1103 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1104 {
1105 if (force_o_largefile())
1106 flags |= O_LARGEFILE;
1107
1108 return do_sys_open(AT_FDCWD, filename, flags, mode);
1109 }
1110
1111 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
1112 umode_t, mode)
1113 {
1114 if (force_o_largefile())
1115 flags |= O_LARGEFILE;
1116
1117 return do_sys_open(dfd, filename, flags, mode);
1118 }
1119
1120 #ifdef CONFIG_COMPAT
1121 /*
1122 * Exactly like sys_open(), except that it doesn't set the
1123 * O_LARGEFILE flag.
1124 */
1125 COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1126 {
1127 return do_sys_open(AT_FDCWD, filename, flags, mode);
1128 }
1129
1130 /*
1131 * Exactly like sys_openat(), except that it doesn't set the
1132 * O_LARGEFILE flag.
1133 */
1134 COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
1135 {
1136 return do_sys_open(dfd, filename, flags, mode);
1137 }
1138 #endif
1139
1140 #ifndef __alpha__
1141
1142 /*
1143 * For backward compatibility? Maybe this should be moved
1144 * into arch/i386 instead?
1145 */
1146 SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
1147 {
1148 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1149 }
1150
1151 #endif
1152
1153 /*
1154 * "id" is the POSIX thread ID. We use the
1155 * files pointer for this..
1156 */
1157 int filp_close(struct file *filp, fl_owner_t id)
1158 {
1159 int retval = 0;
1160
1161 if (!file_count(filp)) {
1162 printk(KERN_ERR "VFS: Close: file count is 0\n");
1163 return 0;
1164 }
1165
1166 if (filp->f_op->flush)
1167 retval = filp->f_op->flush(filp, id);
1168
1169 if (likely(!(filp->f_mode & FMODE_PATH))) {
1170 dnotify_flush(filp, id);
1171 locks_remove_posix(filp, id);
1172 }
1173 fput(filp);
1174 return retval;
1175 }
1176
1177 EXPORT_SYMBOL(filp_close);
1178
1179 /*
1180 * Careful here! We test whether the file pointer is NULL before
1181 * releasing the fd. This ensures that one clone task can't release
1182 * an fd while another clone is opening it.
1183 */
1184 SYSCALL_DEFINE1(close, unsigned int, fd)
1185 {
1186 int retval = __close_fd(current->files, fd);
1187
1188 /* can't restart close syscall because file table entry was cleared */
1189 if (unlikely(retval == -ERESTARTSYS ||
1190 retval == -ERESTARTNOINTR ||
1191 retval == -ERESTARTNOHAND ||
1192 retval == -ERESTART_RESTARTBLOCK))
1193 retval = -EINTR;
1194
1195 return retval;
1196 }
1197 EXPORT_SYMBOL(sys_close);
1198
1199 /*
1200 * This routine simulates a hangup on the tty, to arrange that users
1201 * are given clean terminals at login time.
1202 */
1203 SYSCALL_DEFINE0(vhangup)
1204 {
1205 if (capable(CAP_SYS_TTY_CONFIG)) {
1206 tty_vhangup_self();
1207 return 0;
1208 }
1209 return -EPERM;
1210 }
1211
1212 /*
1213 * Called when an inode is about to be open.
1214 * We use this to disallow opening large files on 32bit systems if
1215 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1216 * on this flag in sys_open.
1217 */
1218 int generic_file_open(struct inode * inode, struct file * filp)
1219 {
1220 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1221 return -EOVERFLOW;
1222 return 0;
1223 }
1224
1225 EXPORT_SYMBOL(generic_file_open);
1226
1227 /*
1228 * This is used by subsystems that don't want seekable
1229 * file descriptors. The function is not supposed to ever fail, the only
1230 * reason it returns an 'int' and not 'void' is so that it can be plugged
1231 * directly into file_operations structure.
1232 */
1233 int nonseekable_open(struct inode *inode, struct file *filp)
1234 {
1235 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1236 return 0;
1237 }
1238
1239 EXPORT_SYMBOL(nonseekable_open);
1240
1241 /*
1242 * stream_open is used by subsystems that want stream-like file descriptors.
1243 * Such file descriptors are not seekable and don't have notion of position
1244 * (file.f_pos is always 0). Contrary to file descriptors of other regular
1245 * files, .read() and .write() can run simultaneously.
1246 *
1247 * stream_open never fails and is marked to return int so that it could be
1248 * directly used as file_operations.open .
1249 */
1250 int stream_open(struct inode *inode, struct file *filp)
1251 {
1252 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS);
1253 filp->f_mode |= FMODE_STREAM;
1254 return 0;
1255 }
1256
1257 EXPORT_SYMBOL(stream_open);