blob: ee0ff60c2f76862eca294effc8c712840539c8c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040010#include <linux/fdtable.h>
Robert Love0eeca282005-07-12 17:06:03 -040011#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/tty.h>
14#include <linux/namei.h>
15#include <linux/backing-dev.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080016#include <linux/capability.h>
Andrew G. Morgan086f7312008-07-04 09:59:58 -070017#include <linux/securebits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/security.h>
19#include <linux/mount.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080020#include <linux/fcntl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
23#include <linux/fs.h>
Yoav Zachef3daed2005-06-23 00:09:58 -070024#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/pagemap.h>
26#include <linux/syscalls.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070027#include <linux/rcupdate.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000028#include <linux/audit.h>
Amit Arora97ac7352007-07-17 21:42:44 -040029#include <linux/falloc.h>
Al Viro5ad4e532009-03-29 19:50:06 -040030#include <linux/fs_struct.h>
Al Virob65a9cf2009-12-16 06:27:40 -050031#include <linux/ima.h>
Eric Paris2dfc1ca2009-12-17 20:30:52 -050032#include <linux/dnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Eric Parise81e3f42009-12-04 15:47:36 -050034#include "internal.h"
35
Daniel Rosenberge95cb222016-10-26 16:33:11 -070036int do_truncate2(struct vfsmount *mnt, struct dentry *dentry, loff_t length,
37 unsigned int time_attrs, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Amerigo Wang939a9422009-08-20 19:29:03 -070039 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct iattr newattrs;
41
42 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
43 if (length < 0)
44 return -EINVAL;
45
46 newattrs.ia_size = length;
NeilBrown4a301312006-01-08 01:02:39 -080047 newattrs.ia_valid = ATTR_SIZE | time_attrs;
Miklos Szeredicc4e69d2005-11-07 00:59:49 -080048 if (filp) {
49 newattrs.ia_file = filp;
50 newattrs.ia_valid |= ATTR_FILE;
51 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds7b82dc02007-05-08 20:10:00 -070053 /* Remove suid/sgid on truncate too */
Amerigo Wang939a9422009-08-20 19:29:03 -070054 ret = should_remove_suid(dentry);
55 if (ret)
56 newattrs.ia_valid |= ret | ATTR_FORCE;
Linus Torvalds7b82dc02007-05-08 20:10:00 -070057
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080058 mutex_lock(&dentry->d_inode->i_mutex);
Daniel Rosenberge95cb222016-10-26 16:33:11 -070059 ret = notify_change2(mnt, dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080060 mutex_unlock(&dentry->d_inode->i_mutex);
Amerigo Wang939a9422009-08-20 19:29:03 -070061 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
Daniel Rosenberge95cb222016-10-26 16:33:11 -070063int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
64 struct file *filp)
65{
66 return do_truncate2(NULL, dentry, length, time_attrs, filp);
67}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Al Viro2d8f3032008-07-22 09:59:21 -040069static long do_sys_truncate(const char __user *pathname, loff_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Al Viro2d8f3032008-07-22 09:59:21 -040071 struct path path;
72 struct inode *inode;
Daniel Rosenberg17285112016-10-26 16:27:45 -070073 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int error;
75
76 error = -EINVAL;
77 if (length < 0) /* sorry, but loff_t says... */
78 goto out;
79
Al Viro2d8f3032008-07-22 09:59:21 -040080 error = user_path(pathname, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (error)
82 goto out;
Al Viro2d8f3032008-07-22 09:59:21 -040083 inode = path.dentry->d_inode;
Daniel Rosenberg17285112016-10-26 16:27:45 -070084 mnt = path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
87 error = -EISDIR;
88 if (S_ISDIR(inode->i_mode))
89 goto dput_and_out;
90
91 error = -EINVAL;
92 if (!S_ISREG(inode->i_mode))
93 goto dput_and_out;
94
Al Viro2d8f3032008-07-22 09:59:21 -040095 error = mnt_want_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (error)
97 goto dput_and_out;
98
Daniel Rosenberg17285112016-10-26 16:27:45 -070099 error = inode_permission2(mnt, inode, MAY_WRITE);
Dave Hansen9ac9b842008-02-15 14:37:52 -0800100 if (error)
101 goto mnt_drop_write_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 error = -EPERM;
Miklos Szeredic82e42d2008-06-24 16:50:12 +0200104 if (IS_APPEND(inode))
Dave Hansen9ac9b842008-02-15 14:37:52 -0800105 goto mnt_drop_write_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 error = get_write_access(inode);
108 if (error)
Dave Hansen9ac9b842008-02-15 14:37:52 -0800109 goto mnt_drop_write_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
david m. richter97003822007-07-31 00:39:12 -0700111 /*
112 * Make sure that there are no leases. get_write_access() protects
113 * against the truncate racing with a lease-granting setlease().
114 */
Al Viro8737c932009-12-24 06:47:55 -0500115 error = break_lease(inode, O_WRONLY);
david m. richter97003822007-07-31 00:39:12 -0700116 if (error)
117 goto put_write_and_out;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 error = locks_verify_truncate(inode, NULL, length);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +0900120 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +0900121 error = security_path_truncate(&path);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500122 if (!error)
Daniel Rosenberge95cb222016-10-26 16:33:11 -0700123 error = do_truncate2(mnt, path.dentry, length, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
david m. richter97003822007-07-31 00:39:12 -0700125put_write_and_out:
126 put_write_access(inode);
Dave Hansen9ac9b842008-02-15 14:37:52 -0800127mnt_drop_write_and_out:
Al Viro2d8f3032008-07-22 09:59:21 -0400128 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129dput_and_out:
Al Viro2d8f3032008-07-22 09:59:21 -0400130 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131out:
132 return error;
133}
134
Heiko Carstens4fd8da82009-09-23 17:49:55 +0200135SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Heiko Carstens4fd8da82009-09-23 17:49:55 +0200137 return do_sys_truncate(path, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Matt Mackallb01ec0e2006-01-08 01:05:20 -0800140static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct inode * inode;
143 struct dentry *dentry;
Daniel Rosenberge95cb222016-10-26 16:33:11 -0700144 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct file * file;
146 int error;
147
148 error = -EINVAL;
149 if (length < 0)
150 goto out;
151 error = -EBADF;
152 file = fget(fd);
153 if (!file)
154 goto out;
155
156 /* explicitly opened as large or we are on 64-bit box */
157 if (file->f_flags & O_LARGEFILE)
158 small = 0;
159
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800160 dentry = file->f_path.dentry;
Daniel Rosenberge95cb222016-10-26 16:33:11 -0700161 mnt = file->f_path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 inode = dentry->d_inode;
163 error = -EINVAL;
164 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
165 goto out_putf;
166
167 error = -EINVAL;
168 /* Cannot ftruncate over 2^31 bytes without large file support */
169 if (small && length > MAX_NON_LFS)
170 goto out_putf;
171
172 error = -EPERM;
173 if (IS_APPEND(inode))
174 goto out_putf;
175
176 error = locks_verify_truncate(inode, file, length);
177 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +0900178 error = security_path_truncate(&file->f_path);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +0900179 if (!error)
Daniel Rosenberge95cb222016-10-26 16:33:11 -0700180 error = do_truncate2(mnt, dentry, length, ATTR_MTIME|ATTR_CTIME, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181out_putf:
182 fput(file);
183out:
184 return error;
185}
186
Heiko Carstensbdc480e2009-01-14 14:14:12 +0100187SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700189 long ret = do_sys_ftruncate(fd, length, 1);
Linus Torvalds385910f2006-04-18 13:22:59 -0700190 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -0700191 asmlinkage_protect(2, ret, fd, length);
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700192 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/* LFS versions of truncate are only needed on 32 bit machines */
196#if BITS_PER_LONG == 32
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100197SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 return do_sys_truncate(path, length);
200}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100201#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
202asmlinkage long SyS_truncate64(long path, loff_t length)
203{
204 return SYSC_truncate64((const char __user *) path, length);
205}
206SYSCALL_ALIAS(sys_truncate64, SyS_truncate64);
207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100209SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700211 long ret = do_sys_ftruncate(fd, length, 0);
Linus Torvalds385910f2006-04-18 13:22:59 -0700212 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -0700213 asmlinkage_protect(2, ret, fd, length);
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700214 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100216#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
217asmlinkage long SyS_ftruncate64(long fd, loff_t length)
218{
219 return SYSC_ftruncate64((unsigned int) fd, length);
220}
221SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#endif
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100223#endif /* BITS_PER_LONG == 32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400225
226int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Arora97ac7352007-07-17 21:42:44 -0400227{
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400228 struct inode *inode = file->f_path.dentry->d_inode;
229 long ret;
Amit Arora97ac7352007-07-17 21:42:44 -0400230
231 if (offset < 0 || len <= 0)
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400232 return -EINVAL;
Amit Arora97ac7352007-07-17 21:42:44 -0400233
234 /* Return error if mode is not supported */
Josef Bacik79124f12010-11-17 20:46:15 -0500235 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
236 return -EOPNOTSUPP;
237
238 /* Punch hole must have keep size set */
239 if ((mode & FALLOC_FL_PUNCH_HOLE) &&
240 !(mode & FALLOC_FL_KEEP_SIZE))
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400241 return -EOPNOTSUPP;
Amit Arora97ac7352007-07-17 21:42:44 -0400242
Amit Arora97ac7352007-07-17 21:42:44 -0400243 if (!(file->f_mode & FMODE_WRITE))
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400244 return -EBADF;
Marco Stornelli1ca551c2011-03-05 11:10:19 +0100245
246 /* It's not possible punch hole on append only file */
247 if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode))
248 return -EPERM;
249
250 if (IS_IMMUTABLE(inode))
251 return -EPERM;
252
Amit Arora97ac7352007-07-17 21:42:44 -0400253 /*
254 * Revalidate the write permissions, in case security policy has
255 * changed since the files were opened.
256 */
257 ret = security_file_permission(file, MAY_WRITE);
258 if (ret)
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400259 return ret;
Amit Arora97ac7352007-07-17 21:42:44 -0400260
Amit Arora97ac7352007-07-17 21:42:44 -0400261 if (S_ISFIFO(inode->i_mode))
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400262 return -ESPIPE;
Amit Arora97ac7352007-07-17 21:42:44 -0400263
Amit Arora97ac7352007-07-17 21:42:44 -0400264 /*
265 * Let individual file system decide if it supports preallocation
266 * for directories or not.
267 */
268 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400269 return -ENODEV;
Amit Arora97ac7352007-07-17 21:42:44 -0400270
Amit Arora97ac7352007-07-17 21:42:44 -0400271 /* Check for wrap through zero too */
272 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400273 return -EFBIG;
Amit Arora97ac7352007-07-17 21:42:44 -0400274
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100275 if (!file->f_op->fallocate)
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400276 return -EOPNOTSUPP;
Amit Arora97ac7352007-07-17 21:42:44 -0400277
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100278 return file->f_op->fallocate(file, mode, offset, len);
Amit Arora97ac7352007-07-17 21:42:44 -0400279}
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400280
281SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
282{
283 struct file *file;
284 int error = -EBADF;
285
286 file = fget(fd);
287 if (file) {
288 error = do_fallocate(file, mode, offset, len);
289 fput(file);
290 }
291
292 return error;
293}
294
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100295#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
296asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len)
297{
298 return SYSC_fallocate((int)fd, (int)mode, offset, len);
299}
300SYSCALL_ALIAS(sys_fallocate, SyS_fallocate);
301#endif
Amit Arora97ac7352007-07-17 21:42:44 -0400302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
304 * access() needs to use the real uid/gid, not the effective uid/gid.
305 * We do this by temporarily clearing all FS-related capabilities and
306 * switching the fsuid/fsgid around to the real ones.
307 */
Heiko Carstens6559eed82009-01-14 14:14:32 +0100308SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
David Howellsd84f4f92008-11-14 10:39:23 +1100310 const struct cred *old_cred;
311 struct cred *override_cred;
Al Viro2d8f3032008-07-22 09:59:21 -0400312 struct path path;
Al Viro256984a2008-07-22 08:09:30 -0400313 struct inode *inode;
Daniel Rosenberg17285112016-10-26 16:27:45 -0700314 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int res;
316
317 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
318 return -EINVAL;
319
David Howellsd84f4f92008-11-14 10:39:23 +1100320 override_cred = prepare_creds();
321 if (!override_cred)
322 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
David Howellsd84f4f92008-11-14 10:39:23 +1100324 override_cred->fsuid = override_cred->uid;
325 override_cred->fsgid = override_cred->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Andrew G. Morgan086f7312008-07-04 09:59:58 -0700327 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
David Howells1cdcbec2008-11-14 10:39:14 +1100328 /* Clear the capabilities if we switch to a non-root user */
David Howellsd84f4f92008-11-14 10:39:23 +1100329 if (override_cred->uid)
330 cap_clear(override_cred->cap_effective);
Andrew G. Morgan086f7312008-07-04 09:59:58 -0700331 else
David Howellsd84f4f92008-11-14 10:39:23 +1100332 override_cred->cap_effective =
333 override_cred->cap_permitted;
Andrew G. Morgan086f7312008-07-04 09:59:58 -0700334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
David Howellsd84f4f92008-11-14 10:39:23 +1100336 old_cred = override_creds(override_cred);
337
Al Viro2d8f3032008-07-22 09:59:21 -0400338 res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
Dave Hansen6902d922006-09-30 23:29:01 -0700339 if (res)
340 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Al Viro2d8f3032008-07-22 09:59:21 -0400342 inode = path.dentry->d_inode;
Daniel Rosenberg17285112016-10-26 16:27:45 -0700343 mnt = path.mnt;
Al Viro256984a2008-07-22 08:09:30 -0400344
345 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
Al Viro30524472008-07-22 00:02:33 -0400346 /*
347 * MAY_EXEC on regular files is denied if the fs is mounted
348 * with the "noexec" flag.
349 */
350 res = -EACCES;
Al Viro2d8f3032008-07-22 09:59:21 -0400351 if (path.mnt->mnt_flags & MNT_NOEXEC)
Al Viro30524472008-07-22 00:02:33 -0400352 goto out_path_release;
353 }
354
Daniel Rosenberg17285112016-10-26 16:27:45 -0700355 res = inode_permission2(mnt, inode, mode | MAY_ACCESS);
Dave Hansen6902d922006-09-30 23:29:01 -0700356 /* SuS v2 requires we report a read only fs too */
Al Viro256984a2008-07-22 08:09:30 -0400357 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
Dave Hansen6902d922006-09-30 23:29:01 -0700358 goto out_path_release;
Dave Hansen2f676cb2008-02-15 14:37:55 -0800359 /*
360 * This is a rare case where using __mnt_is_readonly()
361 * is OK without a mnt_want/drop_write() pair. Since
362 * no actual write to the fs is performed here, we do
363 * not need to telegraph to that to anyone.
364 *
365 * By doing this, we accept that this access is
366 * inherently racy and know that the fs may change
367 * state before we even see this result.
368 */
Al Viro2d8f3032008-07-22 09:59:21 -0400369 if (__mnt_is_readonly(path.mnt))
Dave Hansen6902d922006-09-30 23:29:01 -0700370 res = -EROFS;
371
372out_path_release:
Al Viro2d8f3032008-07-22 09:59:21 -0400373 path_put(&path);
Dave Hansen6902d922006-09-30 23:29:01 -0700374out:
David Howellsd84f4f92008-11-14 10:39:23 +1100375 revert_creds(old_cred);
376 put_cred(override_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return res;
378}
379
Heiko Carstensca013e92009-01-14 14:14:19 +0100380SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800381{
382 return sys_faccessat(AT_FDCWD, filename, mode);
383}
384
Heiko Carstens3cdad422009-01-14 14:14:22 +0100385SYSCALL_DEFINE1(chdir, const char __user *, filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Al Viro2d8f3032008-07-22 09:59:21 -0400387 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int error;
389
Al Viro2d8f3032008-07-22 09:59:21 -0400390 error = user_path_dir(filename, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (error)
392 goto out;
393
Daniel Rosenberg17285112016-10-26 16:27:45 -0700394 error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (error)
396 goto dput_and_out;
397
Al Viro2d8f3032008-07-22 09:59:21 -0400398 set_fs_pwd(current->fs, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400dput_and_out:
Al Viro2d8f3032008-07-22 09:59:21 -0400401 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402out:
403 return error;
404}
405
Heiko Carstens3cdad422009-01-14 14:14:22 +0100406SYSCALL_DEFINE1(fchdir, unsigned int, fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct inode *inode;
Daniel Rosenberg17285112016-10-26 16:27:45 -0700410 struct vfsmount *mnt;
Linus Torvalds59ae4762012-07-07 10:17:00 -0700411 int error, fput_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 error = -EBADF;
Linus Torvalds59ae4762012-07-07 10:17:00 -0700414 file = fget_raw_light(fd, &fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!file)
416 goto out;
417
Jan Blunckac748a02008-02-14 19:34:39 -0800418 inode = file->f_path.dentry->d_inode;
Daniel Rosenberg17285112016-10-26 16:27:45 -0700419 mnt = file->f_path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 error = -ENOTDIR;
422 if (!S_ISDIR(inode->i_mode))
423 goto out_putf;
424
Daniel Rosenberg17285112016-10-26 16:27:45 -0700425 error = inode_permission2(mnt, inode, MAY_EXEC | MAY_CHDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!error)
Jan Blunckac748a02008-02-14 19:34:39 -0800427 set_fs_pwd(current->fs, &file->f_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428out_putf:
Linus Torvalds59ae4762012-07-07 10:17:00 -0700429 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430out:
431 return error;
432}
433
Heiko Carstens3480b252009-01-14 14:14:16 +0100434SYSCALL_DEFINE1(chroot, const char __user *, filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Al Viro2d8f3032008-07-22 09:59:21 -0400436 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int error;
438
Al Viro2d8f3032008-07-22 09:59:21 -0400439 error = user_path_dir(filename, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (error)
441 goto out;
442
Daniel Rosenberg17285112016-10-26 16:27:45 -0700443 error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (error)
445 goto dput_and_out;
446
447 error = -EPERM;
448 if (!capable(CAP_SYS_CHROOT))
449 goto dput_and_out;
Tetsuo Handa8b8efb42009-10-04 21:49:48 +0900450 error = security_path_chroot(&path);
451 if (error)
452 goto dput_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Al Viro2d8f3032008-07-22 09:59:21 -0400454 set_fs_root(current->fs, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 error = 0;
456dput_and_out:
Al Viro2d8f3032008-07-22 09:59:21 -0400457 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458out:
459 return error;
460}
461
Al Viroe57712e2011-07-26 04:15:54 -0400462static int chmod_common(struct path *path, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Al Viroe57712e2011-07-26 04:15:54 -0400464 struct inode *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct iattr newattrs;
Al Viroe57712e2011-07-26 04:15:54 -0400466 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Al Viroe57712e2011-07-26 04:15:54 -0400468 error = mnt_want_write(path->mnt);
469 if (error)
470 return error;
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900471 mutex_lock(&inode->i_mutex);
Al Virocdcf1162011-12-08 10:51:53 -0500472 error = security_path_chmod(path, mode);
Al Viroe57712e2011-07-26 04:15:54 -0400473 if (error)
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900474 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
476 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
Daniel Rosenberge95cb222016-10-26 16:33:11 -0700477 error = notify_change2(path->mnt, path->dentry, &newattrs);
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900478out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800479 mutex_unlock(&inode->i_mutex);
Al Viroe57712e2011-07-26 04:15:54 -0400480 mnt_drop_write(path->mnt);
481 return error;
482}
483
Al Viro49f0a072011-07-26 04:22:01 -0400484SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
Al Viroe57712e2011-07-26 04:15:54 -0400485{
486 struct file * file;
487 int err = -EBADF;
488
489 file = fget(fd);
490 if (file) {
491 audit_inode(NULL, file->f_path.dentry);
492 err = chmod_common(&file->f_path, mode);
493 fput(file);
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return err;
496}
497
Al Viro49f0a072011-07-26 04:22:01 -0400498SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Al Viro2d8f3032008-07-22 09:59:21 -0400500 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Al Viro2d8f3032008-07-22 09:59:21 -0400503 error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
Al Viroe57712e2011-07-26 04:15:54 -0400504 if (!error) {
505 error = chmod_common(&path, mode);
506 path_put(&path);
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return error;
509}
510
Al Viro49f0a072011-07-26 04:22:01 -0400511SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800512{
513 return sys_fchmodat(AT_FDCWD, filename, mode);
514}
515
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900516static int chown_common(struct path *path, uid_t user, gid_t group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900518 struct inode *inode = path->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int error;
520 struct iattr newattrs;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 newattrs.ia_valid = ATTR_CTIME;
523 if (user != (uid_t) -1) {
524 newattrs.ia_valid |= ATTR_UID;
525 newattrs.ia_uid = user;
526 }
527 if (group != (gid_t) -1) {
528 newattrs.ia_valid |= ATTR_GID;
529 newattrs.ia_gid = group;
530 }
531 if (!S_ISDIR(inode->i_mode))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700532 newattrs.ia_valid |=
533 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800534 mutex_lock(&inode->i_mutex);
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900535 error = security_path_chown(path, user, group);
536 if (!error)
Daniel Rosenberge95cb222016-10-26 16:33:11 -0700537 error = notify_change2(path->mnt, path->dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800538 mutex_unlock(&inode->i_mutex);
Miklos Szeredibeb29e02008-07-01 15:01:29 +0200539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return error;
541}
542
Heiko Carstensca013e92009-01-14 14:14:19 +0100543SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Al Viro2d8f3032008-07-22 09:59:21 -0400545 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int error;
547
Al Viro2d8f3032008-07-22 09:59:21 -0400548 error = user_path(filename, &path);
Dave Hansen6902d922006-09-30 23:29:01 -0700549 if (error)
550 goto out;
Al Viro2d8f3032008-07-22 09:59:21 -0400551 error = mnt_want_write(path.mnt);
Dave Hansen2af482a2008-02-15 14:37:50 -0800552 if (error)
553 goto out_release;
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900554 error = chown_common(&path, user, group);
Al Viro2d8f3032008-07-22 09:59:21 -0400555 mnt_drop_write(path.mnt);
Dave Hansen2af482a2008-02-15 14:37:50 -0800556out_release:
Al Viro2d8f3032008-07-22 09:59:21 -0400557 path_put(&path);
Dave Hansen6902d922006-09-30 23:29:01 -0700558out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return error;
560}
561
Heiko Carstens6559eed82009-01-14 14:14:32 +0100562SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
563 gid_t, group, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800564{
Al Viro2d8f3032008-07-22 09:59:21 -0400565 struct path path;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800566 int error = -EINVAL;
Al Viro65cfc672011-03-13 15:56:26 -0400567 int lookup_flags;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800568
Al Viro65cfc672011-03-13 15:56:26 -0400569 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800570 goto out;
571
Al Viro65cfc672011-03-13 15:56:26 -0400572 lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
573 if (flag & AT_EMPTY_PATH)
574 lookup_flags |= LOOKUP_EMPTY;
575 error = user_path_at(dfd, filename, lookup_flags, &path);
Dave Hansen6902d922006-09-30 23:29:01 -0700576 if (error)
577 goto out;
Al Viro2d8f3032008-07-22 09:59:21 -0400578 error = mnt_want_write(path.mnt);
Dave Hansen2af482a2008-02-15 14:37:50 -0800579 if (error)
580 goto out_release;
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900581 error = chown_common(&path, user, group);
Al Viro2d8f3032008-07-22 09:59:21 -0400582 mnt_drop_write(path.mnt);
Dave Hansen2af482a2008-02-15 14:37:50 -0800583out_release:
Al Viro2d8f3032008-07-22 09:59:21 -0400584 path_put(&path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800585out:
586 return error;
587}
588
Heiko Carstensca013e92009-01-14 14:14:19 +0100589SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Al Viro2d8f3032008-07-22 09:59:21 -0400591 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int error;
593
Al Viro2d8f3032008-07-22 09:59:21 -0400594 error = user_lpath(filename, &path);
Dave Hansen6902d922006-09-30 23:29:01 -0700595 if (error)
596 goto out;
Al Viro2d8f3032008-07-22 09:59:21 -0400597 error = mnt_want_write(path.mnt);
Dave Hansen2af482a2008-02-15 14:37:50 -0800598 if (error)
599 goto out_release;
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900600 error = chown_common(&path, user, group);
Al Viro2d8f3032008-07-22 09:59:21 -0400601 mnt_drop_write(path.mnt);
Dave Hansen2af482a2008-02-15 14:37:50 -0800602out_release:
Al Viro2d8f3032008-07-22 09:59:21 -0400603 path_put(&path);
Dave Hansen6902d922006-09-30 23:29:01 -0700604out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return error;
606}
607
Heiko Carstensca013e92009-01-14 14:14:19 +0100608SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct file * file;
611 int error = -EBADF;
Dave Hansen6902d922006-09-30 23:29:01 -0700612 struct dentry * dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 file = fget(fd);
Dave Hansen6902d922006-09-30 23:29:01 -0700615 if (!file)
616 goto out;
617
npiggin@suse.de96029c42009-04-26 20:25:55 +1000618 error = mnt_want_write_file(file);
Dave Hansen2af482a2008-02-15 14:37:50 -0800619 if (error)
620 goto out_fput;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800621 dentry = file->f_path.dentry;
Al Viro5a190ae2007-06-07 12:19:32 -0400622 audit_inode(NULL, dentry);
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900623 error = chown_common(&file->f_path, user, group);
Al Viro2a79f172011-12-09 08:06:57 -0500624 mnt_drop_write_file(file);
Dave Hansen2af482a2008-02-15 14:37:50 -0800625out_fput:
Dave Hansen6902d922006-09-30 23:29:01 -0700626 fput(file);
627out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return error;
629}
630
Dave Hansen4a3fd212008-02-15 14:37:48 -0800631/*
632 * You have to be very careful that these write
633 * counts get cleaned up in error cases and
634 * upon __fput(). This should probably never
635 * be called outside of __dentry_open().
636 */
637static inline int __get_file_write_access(struct inode *inode,
638 struct vfsmount *mnt)
639{
640 int error;
641 error = get_write_access(inode);
642 if (error)
643 return error;
644 /*
645 * Do not take mount writer counts on
646 * special files since no writes to
647 * the mount itself will occur.
648 */
649 if (!special_file(inode->i_mode)) {
650 /*
651 * Balanced in __fput()
652 */
653 error = mnt_want_write(mnt);
654 if (error)
655 put_write_access(inode);
656 }
657 return error;
658}
659
Miklos Szeredid0611162012-05-21 17:30:15 +0200660int open_check_o_direct(struct file *f)
661{
662 /* NB: we're sure to have correct a_ops only after f_op->open */
663 if (f->f_flags & O_DIRECT) {
664 if (!f->f_mapping->a_ops ||
665 ((!f->f_mapping->a_ops->direct_IO) &&
666 (!f->f_mapping->a_ops->get_xip_mem))) {
667 return -EINVAL;
668 }
669 }
670 return 0;
671}
672
673static struct file *do_dentry_open(struct dentry *dentry, struct vfsmount *mnt,
674 struct file *f,
675 int (*open)(struct inode *, struct file *),
676 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Al Viro1abf0c72011-03-13 03:51:11 -0400678 static const struct file_operations empty_fops = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct inode *inode;
680 int error;
681
Al Viro53009902009-12-19 10:15:07 -0500682 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700683 FMODE_PREAD | FMODE_PWRITE;
Al Viro1abf0c72011-03-13 03:51:11 -0400684
685 if (unlikely(f->f_flags & O_PATH))
686 f->f_mode = FMODE_PATH;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 inode = dentry->d_inode;
689 if (f->f_mode & FMODE_WRITE) {
Dave Hansen4a3fd212008-02-15 14:37:48 -0800690 error = __get_file_write_access(inode, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (error)
692 goto cleanup_file;
Dave Hansenad775f52008-02-15 14:38:01 -0800693 if (!special_file(inode->i_mode))
694 file_take_write(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 f->f_mapping = inode->i_mapping;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800698 f->f_path.dentry = dentry;
699 f->f_path.mnt = mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 f->f_pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Al Viro1abf0c72011-03-13 03:51:11 -0400702 if (unlikely(f->f_mode & FMODE_PATH)) {
703 f->f_op = &empty_fops;
704 return f;
705 }
706
Linus Torvaldsf530fe72017-01-07 19:14:29 +0100707 if (S_ISREG(inode->i_mode))
708 f->f_mode |= FMODE_SPLICE_WRITE | FMODE_SPLICE_READ;
709
710
Al Viro1abf0c72011-03-13 03:51:11 -0400711 f->f_op = fops_get(inode->i_fop);
712
David Howells745ca242008-11-14 10:39:22 +1100713 error = security_dentry_open(f, cred);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +0900714 if (error)
715 goto cleanup_all;
716
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -0400717 error = break_lease(inode, f->f_flags);
718 if (error)
719 goto cleanup_all;
720
Trond Myklebust834f2a42005-10-18 14:20:16 -0700721 if (!open && f->f_op)
722 open = f->f_op->open;
723 if (open) {
724 error = open(inode, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (error)
726 goto cleanup_all;
727 }
Mimi Zohar890275b2010-11-02 10:13:07 -0400728 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
729 i_readcount_inc(inode);
Trond Myklebust834f2a42005-10-18 14:20:16 -0700730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
732
733 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return f;
736
737cleanup_all:
738 fops_put(f->f_op);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800739 if (f->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 put_write_access(inode);
Dave Hansenad775f52008-02-15 14:38:01 -0800741 if (!special_file(inode->i_mode)) {
742 /*
743 * We don't consider this a real
744 * mnt_want/drop_write() pair
745 * because it all happenend right
746 * here, so just reset the state.
747 */
748 file_reset_write(f);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800749 mnt_drop_write(mnt);
Dave Hansenad775f52008-02-15 14:38:01 -0800750 }
Dave Hansen4a3fd212008-02-15 14:37:48 -0800751 }
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800752 f->f_path.dentry = NULL;
753 f->f_path.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754cleanup_file:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 dput(dentry);
756 mntput(mnt);
757 return ERR_PTR(error);
758}
759
Miklos Szeredid0611162012-05-21 17:30:15 +0200760static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
761 struct file *f,
762 int (*open)(struct inode *, struct file *),
763 const struct cred *cred)
764{
765 struct file *res = do_dentry_open(dentry, mnt, f, open, cred);
766 if (!IS_ERR(res)) {
767 int error = open_check_o_direct(f);
768 if (error) {
769 fput(res);
770 res = ERR_PTR(error);
771 }
Miklos Szeredif3aa8682012-05-21 17:30:16 +0200772 } else {
773 put_filp(f);
Miklos Szeredid0611162012-05-21 17:30:15 +0200774 }
775 return res;
776}
777
Trond Myklebust834f2a42005-10-18 14:20:16 -0700778/**
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200779 * finish_open - finish opening a file
Miklos Szeredi90666b32013-09-16 14:51:55 +0200780 * @file: file pointer
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200781 * @dentry: pointer to dentry
782 * @open: open callback
Miklos Szeredi90666b32013-09-16 14:51:55 +0200783 * @opened: state of open
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200784 *
785 * This can be used to finish opening a file passed to i_op->atomic_open().
786 *
787 * If the open callback is set to NULL, then the standard f_op->open()
788 * filesystem callback is substituted.
Miklos Szeredi90666b32013-09-16 14:51:55 +0200789 *
790 * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
791 * the return value of d_splice_alias(), then the caller needs to perform dput()
792 * on it after finish_open().
793 *
794 * On successful return @file is a fully instantiated open file. After this, if
795 * an error occurs in ->atomic_open(), it needs to clean up with fput().
796 *
797 * Returns zero on success or -errno if the open failed.
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200798 */
Al Virob2ebdc52012-06-22 12:40:19 +0400799int finish_open(struct file *file, struct dentry *dentry,
800 int (*open)(struct inode *, struct file *),
801 int *opened)
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200802{
803 struct file *res;
Al Viroc3130802012-06-10 05:04:43 -0400804 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200805
Al Virob2ebdc52012-06-22 12:40:19 +0400806 mntget(file->f_path.mnt);
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200807 dget(dentry);
808
Al Virob2ebdc52012-06-22 12:40:19 +0400809 res = do_dentry_open(dentry, file->f_path.mnt, file, open, current_cred());
810 if (!IS_ERR(res)) {
Al Viro8915ef32012-06-10 05:01:45 -0400811 *opened |= FILE_OPENED;
Al Virob2ebdc52012-06-22 12:40:19 +0400812 return 0;
813 }
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200814
Al Virob2ebdc52012-06-22 12:40:19 +0400815 return PTR_ERR(res);
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200816}
817EXPORT_SYMBOL(finish_open);
818
819/**
820 * finish_no_open - finish ->atomic_open() without opening the file
821 *
Miklos Szeredi90666b32013-09-16 14:51:55 +0200822 * @file: file pointer
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200823 * @dentry: dentry or NULL (as returned from ->lookup())
824 *
825 * This can be used to set the result of a successful lookup in ->atomic_open().
Miklos Szeredi90666b32013-09-16 14:51:55 +0200826 *
827 * NB: unlike finish_open() this function does consume the dentry reference and
828 * the caller need not dput() it.
829 *
830 * Returns "1" which must be the return value of ->atomic_open() after having
831 * called this function.
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200832 */
Al Viro57fa6ca2012-06-10 06:48:09 -0400833int finish_no_open(struct file *file, struct dentry *dentry)
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200834{
Al Virob2ebdc52012-06-22 12:40:19 +0400835 file->f_path.dentry = dentry;
Al Viro57fa6ca2012-06-10 06:48:09 -0400836 return 1;
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200837}
838EXPORT_SYMBOL(finish_no_open);
839
Peter Staubach6fdcc212005-11-07 00:59:42 -0800840/*
841 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
842 * error.
843 */
David Howells745ca242008-11-14 10:39:22 +1100844struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
845 const struct cred *cred)
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700846{
847 int error;
848 struct file *f;
849
David Howellse0e81732009-09-02 09:13:40 +0100850 validate_creds(cred);
851
Tetsuo Handac212f9a2011-01-19 21:08:41 +0900852 /* We must always pass in a valid mount pointer. */
853 BUG_ON(!mnt);
Christoph Hellwig322ee5b2008-02-15 14:37:24 -0800854
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700855 error = -ENFILE;
856 f = get_empty_filp();
Peter Staubach6fdcc212005-11-07 00:59:42 -0800857 if (f == NULL) {
858 dput(dentry);
859 mntput(mnt);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700860 return ERR_PTR(error);
Peter Staubach6fdcc212005-11-07 00:59:42 -0800861 }
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700862
Al Viro482928d2009-12-19 10:10:39 -0500863 f->f_flags = flags;
864 return __dentry_open(dentry, mnt, f, NULL, cred);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700865}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866EXPORT_SYMBOL(dentry_open);
867
Matt Mackallb01ec0e2006-01-08 01:05:20 -0800868static void __put_unused_fd(struct files_struct *files, unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700870 struct fdtable *fdt = files_fdtable(files);
David Howells1dce27c2012-02-16 17:49:42 +0000871 __clear_open_fd(fd, fdt);
Eric Dumazet0c9e63f2006-03-23 03:00:12 -0800872 if (fd < files->next_fd)
873 files->next_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800876void put_unused_fd(unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct files_struct *files = current->files;
879 spin_lock(&files->file_lock);
880 __put_unused_fd(files, fd);
881 spin_unlock(&files->file_lock);
882}
883
884EXPORT_SYMBOL(put_unused_fd);
885
886/*
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800887 * Install a file pointer in the fd array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 *
889 * The VFS is full of places where we drop the files lock between
890 * setting the open_fds bitmap and installing the file in the file
891 * array. At any such point, we are vulnerable to a dup2() race
892 * installing a file in the array before us. We need to detect this and
893 * fput() the struct file we are about to overwrite in this case.
894 *
895 * It should never happen - if we allow dup2() do it, _really_ bad things
896 * will follow.
897 */
898
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800899void fd_install(unsigned int fd, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700902 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700904 fdt = files_fdtable(files);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700905 BUG_ON(fdt->fd[fd] != NULL);
906 rcu_assign_pointer(fdt->fd[fd], file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 spin_unlock(&files->file_lock);
908}
909
910EXPORT_SYMBOL(fd_install);
911
Al Viroa218d0f2011-11-21 14:59:34 -0500912static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
Al Viro47c805d2011-02-23 17:44:09 -0500913{
914 int lookup_flags = 0;
915 int acc_mode;
916
Andy Lutomirski7f52b802013-08-01 21:07:52 -0700917 if (flags & (O_CREAT | __O_TMPFILE))
Miklos Szeredicefc92d2012-08-15 13:01:24 +0200918 op->mode = (mode & S_IALLUGO) | S_IFREG;
919 else
920 op->mode = 0;
Al Viro47c805d2011-02-23 17:44:09 -0500921
922 /* Must never be set by userspace */
923 flags &= ~FMODE_NONOTIFY;
924
925 /*
926 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
927 * check for O_DSYNC if the need any syncing at all we enforce it's
928 * always set instead of having to deal with possibly weird behaviour
929 * for malicious applications setting only __O_SYNC.
930 */
931 if (flags & __O_SYNC)
932 flags |= O_DSYNC;
933
Al Virod50bcfe2013-07-13 13:26:37 +0400934 if (flags & __O_TMPFILE) {
935 if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
Al Virob0d54302013-06-07 01:20:27 -0400936 return -EINVAL;
937 acc_mode = MAY_OPEN | ACC_MODE(flags);
Al Viro815466a2013-07-20 03:11:32 +0400938 if (!(acc_mode & MAY_WRITE))
939 return -EINVAL;
Al Virob0d54302013-06-07 01:20:27 -0400940 } else if (flags & O_PATH) {
941 /*
942 * If we have O_PATH in the open flag. Then we
943 * cannot have anything other than the below set of flags
944 */
Al Viro1abf0c72011-03-13 03:51:11 -0400945 flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
946 acc_mode = 0;
947 } else {
948 acc_mode = MAY_OPEN | ACC_MODE(flags);
949 }
Al Viro47c805d2011-02-23 17:44:09 -0500950
Al Viro1abf0c72011-03-13 03:51:11 -0400951 op->open_flag = flags;
Al Viro47c805d2011-02-23 17:44:09 -0500952
953 /* O_TRUNC implies we need access checks for write permissions */
954 if (flags & O_TRUNC)
955 acc_mode |= MAY_WRITE;
956
957 /* Allow the LSM permission hook to distinguish append
958 access from general write access. */
959 if (flags & O_APPEND)
960 acc_mode |= MAY_APPEND;
961
962 op->acc_mode = acc_mode;
963
Al Viro1abf0c72011-03-13 03:51:11 -0400964 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
965
Al Viro47c805d2011-02-23 17:44:09 -0500966 if (flags & O_CREAT) {
967 op->intent |= LOOKUP_CREATE;
968 if (flags & O_EXCL)
969 op->intent |= LOOKUP_EXCL;
970 }
971
972 if (flags & O_DIRECTORY)
973 lookup_flags |= LOOKUP_DIRECTORY;
974 if (!(flags & O_NOFOLLOW))
975 lookup_flags |= LOOKUP_FOLLOW;
976 return lookup_flags;
977}
978
979/**
Jeff Layton631423f2012-10-10 16:43:10 -0400980 * file_open_name - open file and return file pointer
981 *
982 * @name: struct filename containing path to open
983 * @flags: open flags as per the open(2) second argument
984 * @mode: mode for the new file if O_CREAT is set, else ignored
985 *
986 * This is the helper to open a file from kernelspace if you really
987 * have to. But in generally you should not do this, so please move
988 * along, nothing to see here..
989 */
990struct file *file_open_name(struct filename *name, int flags, umode_t mode)
991{
992 struct open_flags op;
993 int lookup = build_open_flags(flags, mode, &op);
994 return do_filp_open(AT_FDCWD, name, &op, lookup);
995}
996
997/**
Al Viro47c805d2011-02-23 17:44:09 -0500998 * filp_open - open file and return file pointer
999 *
1000 * @filename: path to open
1001 * @flags: open flags as per the open(2) second argument
1002 * @mode: mode for the new file if O_CREAT is set, else ignored
1003 *
1004 * This is the helper to open a file from kernelspace if you really
1005 * have to. But in generally you should not do this, so please move
1006 * along, nothing to see here..
1007 */
Al Viroa218d0f2011-11-21 14:59:34 -05001008struct file *filp_open(const char *filename, int flags, umode_t mode)
Al Viro47c805d2011-02-23 17:44:09 -05001009{
Jeff Layton631423f2012-10-10 16:43:10 -04001010 struct filename name = {.name = filename};
1011 return file_open_name(&name, flags, mode);
Al Viro47c805d2011-02-23 17:44:09 -05001012}
1013EXPORT_SYMBOL(filp_open);
1014
Al Viro73d049a2011-03-11 12:08:24 -05001015struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
1016 const char *filename, int flags)
1017{
1018 struct open_flags op;
1019 int lookup = build_open_flags(flags, 0, &op);
1020 if (flags & O_CREAT)
1021 return ERR_PTR(-EINVAL);
1022 if (!filename && (flags & O_DIRECTORY))
1023 if (!dentry->d_inode->i_op->lookup)
1024 return ERR_PTR(-ENOTDIR);
1025 return do_file_open_root(dentry, mnt, filename, &op, lookup);
1026}
1027EXPORT_SYMBOL(file_open_root);
1028
Al Viroa218d0f2011-11-21 14:59:34 -05001029long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Al Viro47c805d2011-02-23 17:44:09 -05001031 struct open_flags op;
1032 int lookup = build_open_flags(flags, mode, &op);
Jeff Layton9cee5ca2012-10-10 15:25:28 -04001033 struct filename *tmp = getname(filename);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001034 int fd = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (!IS_ERR(tmp)) {
Ulrich Drepperf23513e2007-07-15 23:40:32 -07001037 fd = get_unused_fd_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (fd >= 0) {
Jeff Layton631423f2012-10-10 16:43:10 -04001039 struct file *f = do_filp_open(dfd, tmp, &op, lookup);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -07001040 if (IS_ERR(f)) {
1041 put_unused_fd(fd);
1042 fd = PTR_ERR(f);
1043 } else {
Eric Paris2a12a9d2009-12-17 21:24:21 -05001044 fsnotify_open(f);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -07001045 fd_install(fd, f);
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 putname(tmp);
1049 }
1050 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
Miklos Szeredie922efc2005-09-06 15:18:25 -07001052
Al Viroa218d0f2011-11-21 14:59:34 -05001053SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
Miklos Szeredie922efc2005-09-06 15:18:25 -07001054{
Linus Torvalds385910f2006-04-18 13:22:59 -07001055 long ret;
1056
Miklos Szeredie922efc2005-09-06 15:18:25 -07001057 if (force_o_largefile())
1058 flags |= O_LARGEFILE;
1059
Linus Torvalds385910f2006-04-18 13:22:59 -07001060 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1061 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -07001062 asmlinkage_protect(3, ret, filename, flags, mode);
Linus Torvalds385910f2006-04-18 13:22:59 -07001063 return ret;
Miklos Szeredie922efc2005-09-06 15:18:25 -07001064}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Heiko Carstens6559eed82009-01-14 14:14:32 +01001066SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
Al Viroa218d0f2011-11-21 14:59:34 -05001067 umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001068{
Linus Torvalds385910f2006-04-18 13:22:59 -07001069 long ret;
1070
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001071 if (force_o_largefile())
1072 flags |= O_LARGEFILE;
1073
Linus Torvalds385910f2006-04-18 13:22:59 -07001074 ret = do_sys_open(dfd, filename, flags, mode);
1075 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -07001076 asmlinkage_protect(4, ret, dfd, filename, flags, mode);
Linus Torvalds385910f2006-04-18 13:22:59 -07001077 return ret;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001078}
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080#ifndef __alpha__
1081
1082/*
1083 * For backward compatibility? Maybe this should be moved
1084 * into arch/i386 instead?
1085 */
Al Viroa218d0f2011-11-21 14:59:34 -05001086SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1089}
1090
1091#endif
1092
1093/*
1094 * "id" is the POSIX thread ID. We use the
1095 * files pointer for this..
1096 */
1097int filp_close(struct file *filp, fl_owner_t id)
1098{
Christoph Lameter45778ca2005-06-23 00:10:17 -07001099 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 if (!file_count(filp)) {
1102 printk(KERN_ERR "VFS: Close: file count is 0\n");
Christoph Lameter45778ca2005-06-23 00:10:17 -07001103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105
Christoph Lameter45778ca2005-06-23 00:10:17 -07001106 if (filp->f_op && filp->f_op->flush)
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -07001107 retval = filp->f_op->flush(filp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Al Viro1abf0c72011-03-13 03:51:11 -04001109 if (likely(!(filp->f_mode & FMODE_PATH))) {
1110 dnotify_flush(filp, id);
1111 locks_remove_posix(filp, id);
1112 }
Amir Samuelov6a22e462014-05-26 11:44:06 +03001113 security_file_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 fput(filp);
1115 return retval;
1116}
1117
1118EXPORT_SYMBOL(filp_close);
1119
1120/*
1121 * Careful here! We test whether the file pointer is NULL before
1122 * releasing the fd. This ensures that one clone task can't release
1123 * an fd while another clone is opening it.
1124 */
Heiko Carstensca013e92009-01-14 14:14:19 +01001125SYSCALL_DEFINE1(close, unsigned int, fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct file * filp;
1128 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001129 struct fdtable *fdt;
Ernie Petridesee731f42006-09-29 02:00:13 -07001130 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001133 fdt = files_fdtable(files);
1134 if (fd >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 goto out_unlock;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001136 filp = fdt->fd[fd];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (!filp)
1138 goto out_unlock;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -07001139 rcu_assign_pointer(fdt->fd[fd], NULL);
David Howells1dce27c2012-02-16 17:49:42 +00001140 __clear_close_on_exec(fd, fdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 __put_unused_fd(files, fd);
1142 spin_unlock(&files->file_lock);
Ernie Petridesee731f42006-09-29 02:00:13 -07001143 retval = filp_close(filp, files);
1144
1145 /* can't restart close syscall because file table entry was cleared */
1146 if (unlikely(retval == -ERESTARTSYS ||
1147 retval == -ERESTARTNOINTR ||
1148 retval == -ERESTARTNOHAND ||
1149 retval == -ERESTART_RESTARTBLOCK))
1150 retval = -EINTR;
1151
1152 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154out_unlock:
1155 spin_unlock(&files->file_lock);
1156 return -EBADF;
1157}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158EXPORT_SYMBOL(sys_close);
1159
1160/*
1161 * This routine simulates a hangup on the tty, to arrange that users
1162 * are given clean terminals at login time.
1163 */
Heiko Carstensca013e92009-01-14 14:14:19 +01001164SYSCALL_DEFINE0(vhangup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 if (capable(CAP_SYS_TTY_CONFIG)) {
Alan Cox2cb59982008-10-13 10:40:30 +01001167 tty_vhangup_self();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return 0;
1169 }
1170 return -EPERM;
1171}
1172
1173/*
1174 * Called when an inode is about to be open.
1175 * We use this to disallow opening large files on 32bit systems if
1176 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1177 * on this flag in sys_open.
1178 */
1179int generic_file_open(struct inode * inode, struct file * filp)
1180{
1181 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Alan Coxa9c62a12007-10-16 23:30:22 -07001182 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return 0;
1184}
1185
1186EXPORT_SYMBOL(generic_file_open);
1187
1188/*
1189 * This is used by subsystems that don't want seekable
Dmitry Torokhov06b1e102010-08-10 18:01:33 -07001190 * file descriptors. The function is not supposed to ever fail, the only
1191 * reason it returns an 'int' and not 'void' is so that it can be plugged
1192 * directly into file_operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
1194int nonseekable_open(struct inode *inode, struct file *filp)
1195{
1196 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1197 return 0;
1198}
1199
1200EXPORT_SYMBOL(nonseekable_open);