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