blob: fb017ee357f401e48407e39aaaf11a83de839d95 [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
707 f->f_op = fops_get(inode->i_fop);
708
David Howells745ca242008-11-14 10:39:22 +1100709 error = security_dentry_open(f, cred);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +0900710 if (error)
711 goto cleanup_all;
712
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -0400713 error = break_lease(inode, f->f_flags);
714 if (error)
715 goto cleanup_all;
716
Trond Myklebust834f2a42005-10-18 14:20:16 -0700717 if (!open && f->f_op)
718 open = f->f_op->open;
719 if (open) {
720 error = open(inode, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (error)
722 goto cleanup_all;
723 }
Mimi Zohar890275b2010-11-02 10:13:07 -0400724 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
725 i_readcount_inc(inode);
Trond Myklebust834f2a42005-10-18 14:20:16 -0700726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
728
729 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return f;
732
733cleanup_all:
734 fops_put(f->f_op);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800735 if (f->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 put_write_access(inode);
Dave Hansenad775f52008-02-15 14:38:01 -0800737 if (!special_file(inode->i_mode)) {
738 /*
739 * We don't consider this a real
740 * mnt_want/drop_write() pair
741 * because it all happenend right
742 * here, so just reset the state.
743 */
744 file_reset_write(f);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800745 mnt_drop_write(mnt);
Dave Hansenad775f52008-02-15 14:38:01 -0800746 }
Dave Hansen4a3fd212008-02-15 14:37:48 -0800747 }
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800748 f->f_path.dentry = NULL;
749 f->f_path.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750cleanup_file:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 dput(dentry);
752 mntput(mnt);
753 return ERR_PTR(error);
754}
755
Miklos Szeredid0611162012-05-21 17:30:15 +0200756static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
757 struct file *f,
758 int (*open)(struct inode *, struct file *),
759 const struct cred *cred)
760{
761 struct file *res = do_dentry_open(dentry, mnt, f, open, cred);
762 if (!IS_ERR(res)) {
763 int error = open_check_o_direct(f);
764 if (error) {
765 fput(res);
766 res = ERR_PTR(error);
767 }
Miklos Szeredif3aa8682012-05-21 17:30:16 +0200768 } else {
769 put_filp(f);
Miklos Szeredid0611162012-05-21 17:30:15 +0200770 }
771 return res;
772}
773
Trond Myklebust834f2a42005-10-18 14:20:16 -0700774/**
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200775 * finish_open - finish opening a file
Miklos Szeredi90666b32013-09-16 14:51:55 +0200776 * @file: file pointer
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200777 * @dentry: pointer to dentry
778 * @open: open callback
Miklos Szeredi90666b32013-09-16 14:51:55 +0200779 * @opened: state of open
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200780 *
781 * This can be used to finish opening a file passed to i_op->atomic_open().
782 *
783 * If the open callback is set to NULL, then the standard f_op->open()
784 * filesystem callback is substituted.
Miklos Szeredi90666b32013-09-16 14:51:55 +0200785 *
786 * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
787 * the return value of d_splice_alias(), then the caller needs to perform dput()
788 * on it after finish_open().
789 *
790 * On successful return @file is a fully instantiated open file. After this, if
791 * an error occurs in ->atomic_open(), it needs to clean up with fput().
792 *
793 * Returns zero on success or -errno if the open failed.
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200794 */
Al Virob2ebdc52012-06-22 12:40:19 +0400795int finish_open(struct file *file, struct dentry *dentry,
796 int (*open)(struct inode *, struct file *),
797 int *opened)
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200798{
799 struct file *res;
Al Viroc3130802012-06-10 05:04:43 -0400800 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200801
Al Virob2ebdc52012-06-22 12:40:19 +0400802 mntget(file->f_path.mnt);
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200803 dget(dentry);
804
Al Virob2ebdc52012-06-22 12:40:19 +0400805 res = do_dentry_open(dentry, file->f_path.mnt, file, open, current_cred());
806 if (!IS_ERR(res)) {
Al Viro8915ef32012-06-10 05:01:45 -0400807 *opened |= FILE_OPENED;
Al Virob2ebdc52012-06-22 12:40:19 +0400808 return 0;
809 }
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200810
Al Virob2ebdc52012-06-22 12:40:19 +0400811 return PTR_ERR(res);
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200812}
813EXPORT_SYMBOL(finish_open);
814
815/**
816 * finish_no_open - finish ->atomic_open() without opening the file
817 *
Miklos Szeredi90666b32013-09-16 14:51:55 +0200818 * @file: file pointer
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200819 * @dentry: dentry or NULL (as returned from ->lookup())
820 *
821 * This can be used to set the result of a successful lookup in ->atomic_open().
Miklos Szeredi90666b32013-09-16 14:51:55 +0200822 *
823 * NB: unlike finish_open() this function does consume the dentry reference and
824 * the caller need not dput() it.
825 *
826 * Returns "1" which must be the return value of ->atomic_open() after having
827 * called this function.
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200828 */
Al Viro57fa6ca2012-06-10 06:48:09 -0400829int finish_no_open(struct file *file, struct dentry *dentry)
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200830{
Al Virob2ebdc52012-06-22 12:40:19 +0400831 file->f_path.dentry = dentry;
Al Viro57fa6ca2012-06-10 06:48:09 -0400832 return 1;
Miklos Szeredi48b105f2012-06-05 15:10:17 +0200833}
834EXPORT_SYMBOL(finish_no_open);
835
Peter Staubach6fdcc212005-11-07 00:59:42 -0800836/*
837 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
838 * error.
839 */
David Howells745ca242008-11-14 10:39:22 +1100840struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
841 const struct cred *cred)
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700842{
843 int error;
844 struct file *f;
845
David Howellse0e81732009-09-02 09:13:40 +0100846 validate_creds(cred);
847
Tetsuo Handac212f9a2011-01-19 21:08:41 +0900848 /* We must always pass in a valid mount pointer. */
849 BUG_ON(!mnt);
Christoph Hellwig322ee5b2008-02-15 14:37:24 -0800850
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700851 error = -ENFILE;
852 f = get_empty_filp();
Peter Staubach6fdcc212005-11-07 00:59:42 -0800853 if (f == NULL) {
854 dput(dentry);
855 mntput(mnt);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700856 return ERR_PTR(error);
Peter Staubach6fdcc212005-11-07 00:59:42 -0800857 }
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700858
Al Viro482928d2009-12-19 10:10:39 -0500859 f->f_flags = flags;
860 return __dentry_open(dentry, mnt, f, NULL, cred);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700861}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862EXPORT_SYMBOL(dentry_open);
863
Al Viroa218d0f2011-11-21 14:59:34 -0500864static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
Al Viro47c805d2011-02-23 17:44:09 -0500865{
866 int lookup_flags = 0;
867 int acc_mode;
868
Andy Lutomirski7f52b802013-08-01 21:07:52 -0700869 if (flags & (O_CREAT | __O_TMPFILE))
Miklos Szeredicefc92d2012-08-15 13:01:24 +0200870 op->mode = (mode & S_IALLUGO) | S_IFREG;
871 else
872 op->mode = 0;
Al Viro47c805d2011-02-23 17:44:09 -0500873
874 /* Must never be set by userspace */
875 flags &= ~FMODE_NONOTIFY;
876
877 /*
878 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
879 * check for O_DSYNC if the need any syncing at all we enforce it's
880 * always set instead of having to deal with possibly weird behaviour
881 * for malicious applications setting only __O_SYNC.
882 */
883 if (flags & __O_SYNC)
884 flags |= O_DSYNC;
885
Al Virod50bcfe2013-07-13 13:26:37 +0400886 if (flags & __O_TMPFILE) {
887 if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
Al Virob0d54302013-06-07 01:20:27 -0400888 return -EINVAL;
889 acc_mode = MAY_OPEN | ACC_MODE(flags);
Al Viro815466a2013-07-20 03:11:32 +0400890 if (!(acc_mode & MAY_WRITE))
891 return -EINVAL;
Al Virob0d54302013-06-07 01:20:27 -0400892 } else if (flags & O_PATH) {
893 /*
894 * If we have O_PATH in the open flag. Then we
895 * cannot have anything other than the below set of flags
896 */
Al Viro1abf0c72011-03-13 03:51:11 -0400897 flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
898 acc_mode = 0;
899 } else {
900 acc_mode = MAY_OPEN | ACC_MODE(flags);
901 }
Al Viro47c805d2011-02-23 17:44:09 -0500902
Al Viro1abf0c72011-03-13 03:51:11 -0400903 op->open_flag = flags;
Al Viro47c805d2011-02-23 17:44:09 -0500904
905 /* O_TRUNC implies we need access checks for write permissions */
906 if (flags & O_TRUNC)
907 acc_mode |= MAY_WRITE;
908
909 /* Allow the LSM permission hook to distinguish append
910 access from general write access. */
911 if (flags & O_APPEND)
912 acc_mode |= MAY_APPEND;
913
914 op->acc_mode = acc_mode;
915
Al Viro1abf0c72011-03-13 03:51:11 -0400916 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
917
Al Viro47c805d2011-02-23 17:44:09 -0500918 if (flags & O_CREAT) {
919 op->intent |= LOOKUP_CREATE;
920 if (flags & O_EXCL)
921 op->intent |= LOOKUP_EXCL;
922 }
923
924 if (flags & O_DIRECTORY)
925 lookup_flags |= LOOKUP_DIRECTORY;
926 if (!(flags & O_NOFOLLOW))
927 lookup_flags |= LOOKUP_FOLLOW;
928 return lookup_flags;
929}
930
931/**
Jeff Layton631423f2012-10-10 16:43:10 -0400932 * file_open_name - open file and return file pointer
933 *
934 * @name: struct filename containing path to open
935 * @flags: open flags as per the open(2) second argument
936 * @mode: mode for the new file if O_CREAT is set, else ignored
937 *
938 * This is the helper to open a file from kernelspace if you really
939 * have to. But in generally you should not do this, so please move
940 * along, nothing to see here..
941 */
942struct file *file_open_name(struct filename *name, int flags, umode_t mode)
943{
944 struct open_flags op;
945 int lookup = build_open_flags(flags, mode, &op);
946 return do_filp_open(AT_FDCWD, name, &op, lookup);
947}
948
949/**
Al Viro47c805d2011-02-23 17:44:09 -0500950 * filp_open - open file and return file pointer
951 *
952 * @filename: path to open
953 * @flags: open flags as per the open(2) second argument
954 * @mode: mode for the new file if O_CREAT is set, else ignored
955 *
956 * This is the helper to open a file from kernelspace if you really
957 * have to. But in generally you should not do this, so please move
958 * along, nothing to see here..
959 */
Al Viroa218d0f2011-11-21 14:59:34 -0500960struct file *filp_open(const char *filename, int flags, umode_t mode)
Al Viro47c805d2011-02-23 17:44:09 -0500961{
Jeff Layton631423f2012-10-10 16:43:10 -0400962 struct filename name = {.name = filename};
963 return file_open_name(&name, flags, mode);
Al Viro47c805d2011-02-23 17:44:09 -0500964}
965EXPORT_SYMBOL(filp_open);
966
Al Viro73d049a2011-03-11 12:08:24 -0500967struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
968 const char *filename, int flags)
969{
970 struct open_flags op;
971 int lookup = build_open_flags(flags, 0, &op);
972 if (flags & O_CREAT)
973 return ERR_PTR(-EINVAL);
974 if (!filename && (flags & O_DIRECTORY))
975 if (!dentry->d_inode->i_op->lookup)
976 return ERR_PTR(-ENOTDIR);
977 return do_file_open_root(dentry, mnt, filename, &op, lookup);
978}
979EXPORT_SYMBOL(file_open_root);
980
Al Viroa218d0f2011-11-21 14:59:34 -0500981long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Al Viro47c805d2011-02-23 17:44:09 -0500983 struct open_flags op;
984 int lookup = build_open_flags(flags, mode, &op);
Jeff Layton9cee5ca2012-10-10 15:25:28 -0400985 struct filename *tmp = getname(filename);
Miklos Szeredie922efc2005-09-06 15:18:25 -0700986 int fd = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (!IS_ERR(tmp)) {
Ulrich Drepperf23513e2007-07-15 23:40:32 -0700989 fd = get_unused_fd_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (fd >= 0) {
Jeff Layton631423f2012-10-10 16:43:10 -0400991 struct file *f = do_filp_open(dfd, tmp, &op, lookup);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -0700992 if (IS_ERR(f)) {
993 put_unused_fd(fd);
994 fd = PTR_ERR(f);
995 } else {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500996 fsnotify_open(f);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -0700997 fd_install(fd, f);
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 putname(tmp);
1001 }
1002 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
Miklos Szeredie922efc2005-09-06 15:18:25 -07001004
Al Viroa218d0f2011-11-21 14:59:34 -05001005SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
Miklos Szeredie922efc2005-09-06 15:18:25 -07001006{
Linus Torvalds385910f2006-04-18 13:22:59 -07001007 long ret;
1008
Miklos Szeredie922efc2005-09-06 15:18:25 -07001009 if (force_o_largefile())
1010 flags |= O_LARGEFILE;
1011
Linus Torvalds385910f2006-04-18 13:22:59 -07001012 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1013 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -07001014 asmlinkage_protect(3, ret, filename, flags, mode);
Linus Torvalds385910f2006-04-18 13:22:59 -07001015 return ret;
Miklos Szeredie922efc2005-09-06 15:18:25 -07001016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Heiko Carstens6559eed82009-01-14 14:14:32 +01001018SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
Al Viroa218d0f2011-11-21 14:59:34 -05001019 umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001020{
Linus Torvalds385910f2006-04-18 13:22:59 -07001021 long ret;
1022
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001023 if (force_o_largefile())
1024 flags |= O_LARGEFILE;
1025
Linus Torvalds385910f2006-04-18 13:22:59 -07001026 ret = do_sys_open(dfd, filename, flags, mode);
1027 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -07001028 asmlinkage_protect(4, ret, dfd, filename, flags, mode);
Linus Torvalds385910f2006-04-18 13:22:59 -07001029 return ret;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001030}
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032#ifndef __alpha__
1033
1034/*
1035 * For backward compatibility? Maybe this should be moved
1036 * into arch/i386 instead?
1037 */
Al Viroa218d0f2011-11-21 14:59:34 -05001038SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1041}
1042
1043#endif
1044
1045/*
1046 * "id" is the POSIX thread ID. We use the
1047 * files pointer for this..
1048 */
1049int filp_close(struct file *filp, fl_owner_t id)
1050{
Christoph Lameter45778ca2005-06-23 00:10:17 -07001051 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 if (!file_count(filp)) {
1054 printk(KERN_ERR "VFS: Close: file count is 0\n");
Christoph Lameter45778ca2005-06-23 00:10:17 -07001055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
Christoph Lameter45778ca2005-06-23 00:10:17 -07001058 if (filp->f_op && filp->f_op->flush)
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -07001059 retval = filp->f_op->flush(filp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Al Viro1abf0c72011-03-13 03:51:11 -04001061 if (likely(!(filp->f_mode & FMODE_PATH))) {
1062 dnotify_flush(filp, id);
1063 locks_remove_posix(filp, id);
1064 }
Amir Samuelov6a22e462014-05-26 11:44:06 +03001065 security_file_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 fput(filp);
1067 return retval;
1068}
1069
1070EXPORT_SYMBOL(filp_close);
1071
1072/*
1073 * Careful here! We test whether the file pointer is NULL before
1074 * releasing the fd. This ensures that one clone task can't release
1075 * an fd while another clone is opening it.
1076 */
Heiko Carstensca013e92009-01-14 14:14:19 +01001077SYSCALL_DEFINE1(close, unsigned int, fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Al Viroa9999e92012-08-19 12:04:24 -04001079 int retval = __close_fd(current->files, fd);
Ernie Petridesee731f42006-09-29 02:00:13 -07001080
1081 /* can't restart close syscall because file table entry was cleared */
1082 if (unlikely(retval == -ERESTARTSYS ||
1083 retval == -ERESTARTNOINTR ||
1084 retval == -ERESTARTNOHAND ||
1085 retval == -ERESTART_RESTARTBLOCK))
1086 retval = -EINTR;
1087
1088 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090EXPORT_SYMBOL(sys_close);
1091
1092/*
1093 * This routine simulates a hangup on the tty, to arrange that users
1094 * are given clean terminals at login time.
1095 */
Heiko Carstensca013e92009-01-14 14:14:19 +01001096SYSCALL_DEFINE0(vhangup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 if (capable(CAP_SYS_TTY_CONFIG)) {
Alan Cox2cb59982008-10-13 10:40:30 +01001099 tty_vhangup_self();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return 0;
1101 }
1102 return -EPERM;
1103}
1104
1105/*
1106 * Called when an inode is about to be open.
1107 * We use this to disallow opening large files on 32bit systems if
1108 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1109 * on this flag in sys_open.
1110 */
1111int generic_file_open(struct inode * inode, struct file * filp)
1112{
1113 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Alan Coxa9c62a12007-10-16 23:30:22 -07001114 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return 0;
1116}
1117
1118EXPORT_SYMBOL(generic_file_open);
1119
1120/*
1121 * This is used by subsystems that don't want seekable
Dmitry Torokhov06b1e102010-08-10 18:01:33 -07001122 * file descriptors. The function is not supposed to ever fail, the only
1123 * reason it returns an 'int' and not 'void' is so that it can be plugged
1124 * directly into file_operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
1126int nonseekable_open(struct inode *inode, struct file *filp)
1127{
1128 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1129 return 0;
1130}
1131
1132EXPORT_SYMBOL(nonseekable_open);