4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/string.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 <asm/uaccess.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>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/fs.h>
39 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
43 struct iattr newattrs;
45 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
49 newattrs.ia_size = length;
50 newattrs.ia_valid = ATTR_SIZE | time_attrs;
52 newattrs.ia_file = filp;
53 newattrs.ia_valid |= ATTR_FILE;
56 /* Remove suid/sgid on truncate too */
57 ret = should_remove_suid(dentry);
59 newattrs.ia_valid |= ret | ATTR_FORCE;
61 mutex_lock(&dentry->d_inode->i_mutex);
62 ret = notify_change(dentry, &newattrs);
63 mutex_unlock(&dentry->d_inode->i_mutex);
66 EXPORT_SYMBOL(do_truncate);
68 static long do_sys_truncate(const char __user *pathname, loff_t length)
75 if (length < 0) /* sorry, but loff_t says... */
78 error = user_path(pathname, &path);
81 inode = path.dentry->d_inode;
83 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
85 if (S_ISDIR(inode->i_mode))
89 if (!S_ISREG(inode->i_mode))
92 error = mnt_want_write(path.mnt);
96 error = inode_permission(inode, MAY_WRITE);
98 goto mnt_drop_write_and_out;
101 if (IS_APPEND(inode))
102 goto mnt_drop_write_and_out;
104 error = get_write_access(inode);
106 goto mnt_drop_write_and_out;
109 * Make sure that there are no leases. get_write_access() protects
110 * against the truncate racing with a lease-granting setlease().
112 error = break_lease(inode, O_WRONLY);
114 goto put_write_and_out;
116 error = locks_verify_truncate(inode, NULL, length);
118 error = security_path_truncate(&path);
120 error = do_truncate(path.dentry, length, 0, NULL);
123 put_write_access(inode);
124 mnt_drop_write_and_out:
125 mnt_drop_write(path.mnt);
132 SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
134 return do_sys_truncate(path, length);
137 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
139 struct inode * inode;
140 struct dentry *dentry;
152 /* explicitly opened as large or we are on 64-bit box */
153 if (file->f_flags & O_LARGEFILE)
156 dentry = file->f_path.dentry;
157 inode = dentry->d_inode;
159 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
163 /* Cannot ftruncate over 2^31 bytes without large file support */
164 if (small && length > MAX_NON_LFS)
168 if (IS_APPEND(inode))
171 error = locks_verify_truncate(inode, file, length);
173 error = security_path_truncate(&file->f_path);
175 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
182 SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
184 long ret = do_sys_ftruncate(fd, length, 1);
185 /* avoid REGPARM breakage on x86: */
186 asmlinkage_protect(2, ret, fd, length);
190 /* LFS versions of truncate are only needed on 32 bit machines */
191 #if BITS_PER_LONG == 32
192 SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length)
194 return do_sys_truncate(path, length);
196 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
197 asmlinkage long SyS_truncate64(long path, loff_t length)
199 return SYSC_truncate64((const char __user *) path, length);
201 SYSCALL_ALIAS(sys_truncate64, SyS_truncate64);
204 SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length)
206 long ret = do_sys_ftruncate(fd, length, 0);
207 /* avoid REGPARM breakage on x86: */
208 asmlinkage_protect(2, ret, fd, length);
211 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
212 asmlinkage long SyS_ftruncate64(long fd, loff_t length)
214 return SYSC_ftruncate64((unsigned int) fd, length);
216 SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64);
218 #endif /* BITS_PER_LONG == 32 */
221 int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
223 struct inode *inode = file->f_path.dentry->d_inode;
226 if (offset < 0 || len <= 0)
229 /* Return error if mode is not supported */
230 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
233 /* Punch hole must have keep size set */
234 if ((mode & FALLOC_FL_PUNCH_HOLE) &&
235 !(mode & FALLOC_FL_KEEP_SIZE))
238 if (!(file->f_mode & FMODE_WRITE))
241 /* It's not possible punch hole on append only file */
242 if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode))
245 if (IS_IMMUTABLE(inode))
249 * Revalidate the write permissions, in case security policy has
250 * changed since the files were opened.
252 ret = security_file_permission(file, MAY_WRITE);
256 if (S_ISFIFO(inode->i_mode))
260 * Let individual file system decide if it supports preallocation
261 * for directories or not.
263 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
266 /* Check for wrap through zero too */
267 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
270 if (!file->f_op->fallocate)
273 return file->f_op->fallocate(file, mode, offset, len);
276 SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
283 error = do_fallocate(file, mode, offset, len);
290 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
291 asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len)
293 return SYSC_fallocate((int)fd, (int)mode, offset, len);
295 SYSCALL_ALIAS(sys_fallocate, SyS_fallocate);
299 * access() needs to use the real uid/gid, not the effective uid/gid.
300 * We do this by temporarily clearing all FS-related capabilities and
301 * switching the fsuid/fsgid around to the real ones.
303 SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
305 const struct cred *old_cred;
306 struct cred *override_cred;
311 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
314 override_cred = prepare_creds();
318 override_cred->fsuid = override_cred->uid;
319 override_cred->fsgid = override_cred->gid;
321 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
322 /* Clear the capabilities if we switch to a non-root user */
323 if (override_cred->uid)
324 cap_clear(override_cred->cap_effective);
326 override_cred->cap_effective =
327 override_cred->cap_permitted;
330 old_cred = override_creds(override_cred);
332 res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
336 inode = path.dentry->d_inode;
338 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
340 * MAY_EXEC on regular files is denied if the fs is mounted
341 * with the "noexec" flag.
344 if (path.mnt->mnt_flags & MNT_NOEXEC)
345 goto out_path_release;
348 res = inode_permission(inode, mode | MAY_ACCESS);
349 /* SuS v2 requires we report a read only fs too */
350 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
351 goto out_path_release;
353 * This is a rare case where using __mnt_is_readonly()
354 * is OK without a mnt_want/drop_write() pair. Since
355 * no actual write to the fs is performed here, we do
356 * not need to telegraph to that to anyone.
358 * By doing this, we accept that this access is
359 * inherently racy and know that the fs may change
360 * state before we even see this result.
362 if (__mnt_is_readonly(path.mnt))
368 revert_creds(old_cred);
369 put_cred(override_cred);
373 SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
375 return sys_faccessat(AT_FDCWD, filename, mode);
378 SYSCALL_DEFINE1(chdir, const char __user *, filename)
383 error = user_path_dir(filename, &path);
387 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
391 set_fs_pwd(current->fs, &path);
399 SYSCALL_DEFINE1(fchdir, unsigned int, fd)
410 inode = file->f_path.dentry->d_inode;
413 if (!S_ISDIR(inode->i_mode))
416 error = inode_permission(inode, MAY_EXEC | MAY_CHDIR);
418 set_fs_pwd(current->fs, &file->f_path);
425 SYSCALL_DEFINE1(chroot, const char __user *, filename)
430 error = user_path_dir(filename, &path);
434 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
439 if (!capable(CAP_SYS_CHROOT))
441 error = security_path_chroot(&path);
445 set_fs_root(current->fs, &path);
453 SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
455 struct inode * inode;
456 struct dentry * dentry;
459 struct iattr newattrs;
465 dentry = file->f_path.dentry;
466 inode = dentry->d_inode;
468 audit_inode(NULL, dentry);
470 err = mnt_want_write_file(file);
473 mutex_lock(&inode->i_mutex);
474 err = security_path_chmod(dentry, file->f_vfsmnt, mode);
477 if (mode == (mode_t) -1)
478 mode = inode->i_mode;
479 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
480 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
481 err = notify_change(dentry, &newattrs);
483 mutex_unlock(&inode->i_mutex);
484 mnt_drop_write(file->f_path.mnt);
491 SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, mode_t, mode)
496 struct iattr newattrs;
498 error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
501 inode = path.dentry->d_inode;
503 error = mnt_want_write(path.mnt);
506 mutex_lock(&inode->i_mutex);
507 error = security_path_chmod(path.dentry, path.mnt, mode);
510 if (mode == (mode_t) -1)
511 mode = inode->i_mode;
512 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
513 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
514 error = notify_change(path.dentry, &newattrs);
516 mutex_unlock(&inode->i_mutex);
517 mnt_drop_write(path.mnt);
524 SYSCALL_DEFINE2(chmod, const char __user *, filename, mode_t, mode)
526 return sys_fchmodat(AT_FDCWD, filename, mode);
529 static int chown_common(struct path *path, uid_t user, gid_t group)
531 struct inode *inode = path->dentry->d_inode;
533 struct iattr newattrs;
535 newattrs.ia_valid = ATTR_CTIME;
536 if (user != (uid_t) -1) {
537 newattrs.ia_valid |= ATTR_UID;
538 newattrs.ia_uid = user;
540 if (group != (gid_t) -1) {
541 newattrs.ia_valid |= ATTR_GID;
542 newattrs.ia_gid = group;
544 if (!S_ISDIR(inode->i_mode))
546 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
547 mutex_lock(&inode->i_mutex);
548 error = security_path_chown(path, user, group);
550 error = notify_change(path->dentry, &newattrs);
551 mutex_unlock(&inode->i_mutex);
556 SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
561 error = user_path(filename, &path);
564 error = mnt_want_write(path.mnt);
567 error = chown_common(&path, user, group);
568 mnt_drop_write(path.mnt);
575 SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
576 gid_t, group, int, flag)
582 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
585 follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
586 error = user_path_at(dfd, filename, follow, &path);
589 error = mnt_want_write(path.mnt);
592 error = chown_common(&path, user, group);
593 mnt_drop_write(path.mnt);
600 SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
605 error = user_lpath(filename, &path);
608 error = mnt_want_write(path.mnt);
611 error = chown_common(&path, user, group);
612 mnt_drop_write(path.mnt);
619 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
623 struct dentry * dentry;
629 error = mnt_want_write_file(file);
632 dentry = file->f_path.dentry;
633 audit_inode(NULL, dentry);
634 error = chown_common(&file->f_path, user, group);
635 mnt_drop_write(file->f_path.mnt);
643 * You have to be very careful that these write
644 * counts get cleaned up in error cases and
645 * upon __fput(). This should probably never
646 * be called outside of __dentry_open().
648 static inline int __get_file_write_access(struct inode *inode,
649 struct vfsmount *mnt)
652 error = get_write_access(inode);
656 * Do not take mount writer counts on
657 * special files since no writes to
658 * the mount itself will occur.
660 if (!special_file(inode->i_mode)) {
662 * Balanced in __fput()
664 error = mnt_want_write(mnt);
666 put_write_access(inode);
671 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
673 int (*open)(struct inode *, struct file *),
674 const struct cred *cred)
679 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
680 FMODE_PREAD | FMODE_PWRITE;
681 inode = dentry->d_inode;
682 if (f->f_mode & FMODE_WRITE) {
683 error = __get_file_write_access(inode, mnt);
686 if (!special_file(inode->i_mode))
690 f->f_mapping = inode->i_mapping;
691 f->f_path.dentry = dentry;
694 f->f_op = fops_get(inode->i_fop);
695 file_sb_list_add(f, inode->i_sb);
697 error = security_dentry_open(f, cred);
701 if (!open && f->f_op)
702 open = f->f_op->open;
704 error = open(inode, f);
710 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
712 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
714 /* NB: we're sure to have correct a_ops only after f_op->open */
715 if (f->f_flags & O_DIRECT) {
716 if (!f->f_mapping->a_ops ||
717 ((!f->f_mapping->a_ops->direct_IO) &&
718 (!f->f_mapping->a_ops->get_xip_mem))) {
720 f = ERR_PTR(-EINVAL);
728 if (f->f_mode & FMODE_WRITE) {
729 put_write_access(inode);
730 if (!special_file(inode->i_mode)) {
732 * We don't consider this a real
733 * mnt_want/drop_write() pair
734 * because it all happenend right
735 * here, so just reset the state.
742 f->f_path.dentry = NULL;
743 f->f_path.mnt = NULL;
748 return ERR_PTR(error);
752 * lookup_instantiate_filp - instantiates the open intent filp
753 * @nd: pointer to nameidata
754 * @dentry: pointer to dentry
755 * @open: open callback
757 * Helper for filesystems that want to use lookup open intents and pass back
758 * a fully instantiated struct file to the caller.
759 * This function is meant to be called from within a filesystem's
761 * Beware of calling it for non-regular files! Those ->open methods might block
762 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
763 * leading to a deadlock, as nobody can open that fifo anymore, because
764 * another process to open fifo will block on locked parent when doing lookup).
765 * Note that in case of error, nd->intent.open.file is destroyed, but the
766 * path information remains valid.
767 * If the open callback is set to NULL, then the standard f_op->open()
768 * filesystem callback is substituted.
770 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
771 int (*open)(struct inode *, struct file *))
773 const struct cred *cred = current_cred();
775 if (IS_ERR(nd->intent.open.file))
779 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
780 nd->intent.open.file,
783 return nd->intent.open.file;
785 release_open_intent(nd);
786 nd->intent.open.file = (struct file *)dentry;
789 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
792 * nameidata_to_filp - convert a nameidata to an open filp.
793 * @nd: pointer to nameidata
796 * Note that this function destroys the original nameidata
798 struct file *nameidata_to_filp(struct nameidata *nd)
800 const struct cred *cred = current_cred();
803 /* Pick up the filp from the open intent */
804 filp = nd->intent.open.file;
805 nd->intent.open.file = NULL;
807 /* Has the filesystem initialised the file for us? */
808 if (filp->f_path.dentry == NULL) {
810 filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
817 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
820 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
821 const struct cred *cred)
826 validate_creds(cred);
829 * We must always pass in a valid mount pointer. Historically
830 * callers got away with not passing it, but we must enforce this at
831 * the earliest possible point now to avoid strange problems deep in the
835 printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
837 return ERR_PTR(-EINVAL);
841 f = get_empty_filp();
845 return ERR_PTR(error);
849 return __dentry_open(dentry, mnt, f, NULL, cred);
851 EXPORT_SYMBOL(dentry_open);
853 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
855 struct fdtable *fdt = files_fdtable(files);
856 __FD_CLR(fd, fdt->open_fds);
857 if (fd < files->next_fd)
861 void put_unused_fd(unsigned int fd)
863 struct files_struct *files = current->files;
864 spin_lock(&files->file_lock);
865 __put_unused_fd(files, fd);
866 spin_unlock(&files->file_lock);
869 EXPORT_SYMBOL(put_unused_fd);
872 * Install a file pointer in the fd array.
874 * The VFS is full of places where we drop the files lock between
875 * setting the open_fds bitmap and installing the file in the file
876 * array. At any such point, we are vulnerable to a dup2() race
877 * installing a file in the array before us. We need to detect this and
878 * fput() the struct file we are about to overwrite in this case.
880 * It should never happen - if we allow dup2() do it, _really_ bad things
884 void fd_install(unsigned int fd, struct file *file)
886 struct files_struct *files = current->files;
888 spin_lock(&files->file_lock);
889 fdt = files_fdtable(files);
890 BUG_ON(fdt->fd[fd] != NULL);
891 rcu_assign_pointer(fdt->fd[fd], file);
892 spin_unlock(&files->file_lock);
895 EXPORT_SYMBOL(fd_install);
897 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
899 char *tmp = getname(filename);
900 int fd = PTR_ERR(tmp);
903 fd = get_unused_fd_flags(flags);
905 struct file *f = do_filp_open(dfd, tmp, flags, mode, 0);
912 trace_do_sys_open(tmp, flags, mode);
920 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
924 if (force_o_largefile())
925 flags |= O_LARGEFILE;
927 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
928 /* avoid REGPARM breakage on x86: */
929 asmlinkage_protect(3, ret, filename, flags, mode);
933 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
938 if (force_o_largefile())
939 flags |= O_LARGEFILE;
941 ret = do_sys_open(dfd, filename, flags, mode);
942 /* avoid REGPARM breakage on x86: */
943 asmlinkage_protect(4, ret, dfd, filename, flags, mode);
950 * For backward compatibility? Maybe this should be moved
951 * into arch/i386 instead?
953 SYSCALL_DEFINE2(creat, const char __user *, pathname, int, mode)
955 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
961 * "id" is the POSIX thread ID. We use the
962 * files pointer for this..
964 int filp_close(struct file *filp, fl_owner_t id)
968 if (!file_count(filp)) {
969 printk(KERN_ERR "VFS: Close: file count is 0\n");
973 if (filp->f_op && filp->f_op->flush)
974 retval = filp->f_op->flush(filp, id);
976 dnotify_flush(filp, id);
977 locks_remove_posix(filp, id);
982 EXPORT_SYMBOL(filp_close);
985 * Careful here! We test whether the file pointer is NULL before
986 * releasing the fd. This ensures that one clone task can't release
987 * an fd while another clone is opening it.
989 SYSCALL_DEFINE1(close, unsigned int, fd)
992 struct files_struct *files = current->files;
996 spin_lock(&files->file_lock);
997 fdt = files_fdtable(files);
998 if (fd >= fdt->max_fds)
1003 rcu_assign_pointer(fdt->fd[fd], NULL);
1004 FD_CLR(fd, fdt->close_on_exec);
1005 __put_unused_fd(files, fd);
1006 spin_unlock(&files->file_lock);
1007 retval = filp_close(filp, files);
1009 /* can't restart close syscall because file table entry was cleared */
1010 if (unlikely(retval == -ERESTARTSYS ||
1011 retval == -ERESTARTNOINTR ||
1012 retval == -ERESTARTNOHAND ||
1013 retval == -ERESTART_RESTARTBLOCK))
1019 spin_unlock(&files->file_lock);
1022 EXPORT_SYMBOL(sys_close);
1025 * This routine simulates a hangup on the tty, to arrange that users
1026 * are given clean terminals at login time.
1028 SYSCALL_DEFINE0(vhangup)
1030 if (capable(CAP_SYS_TTY_CONFIG)) {
1038 * Called when an inode is about to be open.
1039 * We use this to disallow opening large files on 32bit systems if
1040 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1041 * on this flag in sys_open.
1043 int generic_file_open(struct inode * inode, struct file * filp)
1045 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1050 EXPORT_SYMBOL(generic_file_open);
1053 * This is used by subsystems that don't want seekable
1054 * file descriptors. The function is not supposed to ever fail, the only
1055 * reason it returns an 'int' and not 'void' is so that it can be plugged
1056 * directly into file_operations structure.
1058 int nonseekable_open(struct inode *inode, struct file *filp)
1060 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1064 EXPORT_SYMBOL(nonseekable_open);