1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * linux/fs/fcntl.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1991, 1992 Linus Torvalds
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/syscalls.h>
9*4882a593Smuzhiyun #include <linux/init.h>
10*4882a593Smuzhiyun #include <linux/mm.h>
11*4882a593Smuzhiyun #include <linux/sched/task.h>
12*4882a593Smuzhiyun #include <linux/fs.h>
13*4882a593Smuzhiyun #include <linux/file.h>
14*4882a593Smuzhiyun #include <linux/fdtable.h>
15*4882a593Smuzhiyun #include <linux/capability.h>
16*4882a593Smuzhiyun #include <linux/dnotify.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/pipe_fs_i.h>
20*4882a593Smuzhiyun #include <linux/security.h>
21*4882a593Smuzhiyun #include <linux/ptrace.h>
22*4882a593Smuzhiyun #include <linux/signal.h>
23*4882a593Smuzhiyun #include <linux/rcupdate.h>
24*4882a593Smuzhiyun #include <linux/pid_namespace.h>
25*4882a593Smuzhiyun #include <linux/user_namespace.h>
26*4882a593Smuzhiyun #include <linux/memfd.h>
27*4882a593Smuzhiyun #include <linux/compat.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <linux/poll.h>
30*4882a593Smuzhiyun #include <asm/siginfo.h>
31*4882a593Smuzhiyun #include <linux/uaccess.h>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
34*4882a593Smuzhiyun
setfl(int fd,struct file * filp,unsigned long arg)35*4882a593Smuzhiyun static int setfl(int fd, struct file * filp, unsigned long arg)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun struct inode * inode = file_inode(filp);
38*4882a593Smuzhiyun int error = 0;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun * O_APPEND cannot be cleared if the file is marked as append-only
42*4882a593Smuzhiyun * and the file is open for write.
43*4882a593Smuzhiyun */
44*4882a593Smuzhiyun if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
45*4882a593Smuzhiyun return -EPERM;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /* O_NOATIME can only be set by the owner or superuser */
48*4882a593Smuzhiyun if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
49*4882a593Smuzhiyun if (!inode_owner_or_capable(inode))
50*4882a593Smuzhiyun return -EPERM;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* required for strict SunOS emulation */
53*4882a593Smuzhiyun if (O_NONBLOCK != O_NDELAY)
54*4882a593Smuzhiyun if (arg & O_NDELAY)
55*4882a593Smuzhiyun arg |= O_NONBLOCK;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /* Pipe packetized mode is controlled by O_DIRECT flag */
58*4882a593Smuzhiyun if (!S_ISFIFO(inode->i_mode) && (arg & O_DIRECT)) {
59*4882a593Smuzhiyun if (!filp->f_mapping || !filp->f_mapping->a_ops ||
60*4882a593Smuzhiyun !filp->f_mapping->a_ops->direct_IO)
61*4882a593Smuzhiyun return -EINVAL;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun if (filp->f_op->check_flags)
65*4882a593Smuzhiyun error = filp->f_op->check_flags(arg);
66*4882a593Smuzhiyun if (error)
67*4882a593Smuzhiyun return error;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun * ->fasync() is responsible for setting the FASYNC bit.
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) {
73*4882a593Smuzhiyun error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
74*4882a593Smuzhiyun if (error < 0)
75*4882a593Smuzhiyun goto out;
76*4882a593Smuzhiyun if (error > 0)
77*4882a593Smuzhiyun error = 0;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun spin_lock(&filp->f_lock);
80*4882a593Smuzhiyun filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
81*4882a593Smuzhiyun spin_unlock(&filp->f_lock);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun out:
84*4882a593Smuzhiyun return error;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
f_modown(struct file * filp,struct pid * pid,enum pid_type type,int force)87*4882a593Smuzhiyun static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
88*4882a593Smuzhiyun int force)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun write_lock_irq(&filp->f_owner.lock);
91*4882a593Smuzhiyun if (force || !filp->f_owner.pid) {
92*4882a593Smuzhiyun put_pid(filp->f_owner.pid);
93*4882a593Smuzhiyun filp->f_owner.pid = get_pid(pid);
94*4882a593Smuzhiyun filp->f_owner.pid_type = type;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun if (pid) {
97*4882a593Smuzhiyun const struct cred *cred = current_cred();
98*4882a593Smuzhiyun filp->f_owner.uid = cred->uid;
99*4882a593Smuzhiyun filp->f_owner.euid = cred->euid;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun write_unlock_irq(&filp->f_owner.lock);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
__f_setown(struct file * filp,struct pid * pid,enum pid_type type,int force)105*4882a593Smuzhiyun void __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
106*4882a593Smuzhiyun int force)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun security_file_set_fowner(filp);
109*4882a593Smuzhiyun f_modown(filp, pid, type, force);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun EXPORT_SYMBOL(__f_setown);
112*4882a593Smuzhiyun
f_setown(struct file * filp,unsigned long arg,int force)113*4882a593Smuzhiyun int f_setown(struct file *filp, unsigned long arg, int force)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun enum pid_type type;
116*4882a593Smuzhiyun struct pid *pid = NULL;
117*4882a593Smuzhiyun int who = arg, ret = 0;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun type = PIDTYPE_TGID;
120*4882a593Smuzhiyun if (who < 0) {
121*4882a593Smuzhiyun /* avoid overflow below */
122*4882a593Smuzhiyun if (who == INT_MIN)
123*4882a593Smuzhiyun return -EINVAL;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun type = PIDTYPE_PGID;
126*4882a593Smuzhiyun who = -who;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun rcu_read_lock();
130*4882a593Smuzhiyun if (who) {
131*4882a593Smuzhiyun pid = find_vpid(who);
132*4882a593Smuzhiyun if (!pid)
133*4882a593Smuzhiyun ret = -ESRCH;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun if (!ret)
137*4882a593Smuzhiyun __f_setown(filp, pid, type, force);
138*4882a593Smuzhiyun rcu_read_unlock();
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun return ret;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun EXPORT_SYMBOL(f_setown);
143*4882a593Smuzhiyun
f_delown(struct file * filp)144*4882a593Smuzhiyun void f_delown(struct file *filp)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun f_modown(filp, NULL, PIDTYPE_TGID, 1);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
f_getown(struct file * filp)149*4882a593Smuzhiyun pid_t f_getown(struct file *filp)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun pid_t pid = 0;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun read_lock_irq(&filp->f_owner.lock);
154*4882a593Smuzhiyun rcu_read_lock();
155*4882a593Smuzhiyun if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type)) {
156*4882a593Smuzhiyun pid = pid_vnr(filp->f_owner.pid);
157*4882a593Smuzhiyun if (filp->f_owner.pid_type == PIDTYPE_PGID)
158*4882a593Smuzhiyun pid = -pid;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun rcu_read_unlock();
161*4882a593Smuzhiyun read_unlock_irq(&filp->f_owner.lock);
162*4882a593Smuzhiyun return pid;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
f_setown_ex(struct file * filp,unsigned long arg)165*4882a593Smuzhiyun static int f_setown_ex(struct file *filp, unsigned long arg)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun struct f_owner_ex __user *owner_p = (void __user *)arg;
168*4882a593Smuzhiyun struct f_owner_ex owner;
169*4882a593Smuzhiyun struct pid *pid;
170*4882a593Smuzhiyun int type;
171*4882a593Smuzhiyun int ret;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun ret = copy_from_user(&owner, owner_p, sizeof(owner));
174*4882a593Smuzhiyun if (ret)
175*4882a593Smuzhiyun return -EFAULT;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun switch (owner.type) {
178*4882a593Smuzhiyun case F_OWNER_TID:
179*4882a593Smuzhiyun type = PIDTYPE_PID;
180*4882a593Smuzhiyun break;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun case F_OWNER_PID:
183*4882a593Smuzhiyun type = PIDTYPE_TGID;
184*4882a593Smuzhiyun break;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun case F_OWNER_PGRP:
187*4882a593Smuzhiyun type = PIDTYPE_PGID;
188*4882a593Smuzhiyun break;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun default:
191*4882a593Smuzhiyun return -EINVAL;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun rcu_read_lock();
195*4882a593Smuzhiyun pid = find_vpid(owner.pid);
196*4882a593Smuzhiyun if (owner.pid && !pid)
197*4882a593Smuzhiyun ret = -ESRCH;
198*4882a593Smuzhiyun else
199*4882a593Smuzhiyun __f_setown(filp, pid, type, 1);
200*4882a593Smuzhiyun rcu_read_unlock();
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun return ret;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
f_getown_ex(struct file * filp,unsigned long arg)205*4882a593Smuzhiyun static int f_getown_ex(struct file *filp, unsigned long arg)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun struct f_owner_ex __user *owner_p = (void __user *)arg;
208*4882a593Smuzhiyun struct f_owner_ex owner = {};
209*4882a593Smuzhiyun int ret = 0;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun read_lock_irq(&filp->f_owner.lock);
212*4882a593Smuzhiyun rcu_read_lock();
213*4882a593Smuzhiyun if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type))
214*4882a593Smuzhiyun owner.pid = pid_vnr(filp->f_owner.pid);
215*4882a593Smuzhiyun rcu_read_unlock();
216*4882a593Smuzhiyun switch (filp->f_owner.pid_type) {
217*4882a593Smuzhiyun case PIDTYPE_PID:
218*4882a593Smuzhiyun owner.type = F_OWNER_TID;
219*4882a593Smuzhiyun break;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun case PIDTYPE_TGID:
222*4882a593Smuzhiyun owner.type = F_OWNER_PID;
223*4882a593Smuzhiyun break;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun case PIDTYPE_PGID:
226*4882a593Smuzhiyun owner.type = F_OWNER_PGRP;
227*4882a593Smuzhiyun break;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun default:
230*4882a593Smuzhiyun WARN_ON(1);
231*4882a593Smuzhiyun ret = -EINVAL;
232*4882a593Smuzhiyun break;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun read_unlock_irq(&filp->f_owner.lock);
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun if (!ret) {
237*4882a593Smuzhiyun ret = copy_to_user(owner_p, &owner, sizeof(owner));
238*4882a593Smuzhiyun if (ret)
239*4882a593Smuzhiyun ret = -EFAULT;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun return ret;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun #ifdef CONFIG_CHECKPOINT_RESTORE
f_getowner_uids(struct file * filp,unsigned long arg)245*4882a593Smuzhiyun static int f_getowner_uids(struct file *filp, unsigned long arg)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun struct user_namespace *user_ns = current_user_ns();
248*4882a593Smuzhiyun uid_t __user *dst = (void __user *)arg;
249*4882a593Smuzhiyun uid_t src[2];
250*4882a593Smuzhiyun int err;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun read_lock_irq(&filp->f_owner.lock);
253*4882a593Smuzhiyun src[0] = from_kuid(user_ns, filp->f_owner.uid);
254*4882a593Smuzhiyun src[1] = from_kuid(user_ns, filp->f_owner.euid);
255*4882a593Smuzhiyun read_unlock_irq(&filp->f_owner.lock);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun err = put_user(src[0], &dst[0]);
258*4882a593Smuzhiyun err |= put_user(src[1], &dst[1]);
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun return err;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun #else
f_getowner_uids(struct file * filp,unsigned long arg)263*4882a593Smuzhiyun static int f_getowner_uids(struct file *filp, unsigned long arg)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun return -EINVAL;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun #endif
268*4882a593Smuzhiyun
rw_hint_valid(enum rw_hint hint)269*4882a593Smuzhiyun static bool rw_hint_valid(enum rw_hint hint)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun switch (hint) {
272*4882a593Smuzhiyun case RWH_WRITE_LIFE_NOT_SET:
273*4882a593Smuzhiyun case RWH_WRITE_LIFE_NONE:
274*4882a593Smuzhiyun case RWH_WRITE_LIFE_SHORT:
275*4882a593Smuzhiyun case RWH_WRITE_LIFE_MEDIUM:
276*4882a593Smuzhiyun case RWH_WRITE_LIFE_LONG:
277*4882a593Smuzhiyun case RWH_WRITE_LIFE_EXTREME:
278*4882a593Smuzhiyun return true;
279*4882a593Smuzhiyun default:
280*4882a593Smuzhiyun return false;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
fcntl_rw_hint(struct file * file,unsigned int cmd,unsigned long arg)284*4882a593Smuzhiyun static long fcntl_rw_hint(struct file *file, unsigned int cmd,
285*4882a593Smuzhiyun unsigned long arg)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun struct inode *inode = file_inode(file);
288*4882a593Smuzhiyun u64 __user *argp = (u64 __user *)arg;
289*4882a593Smuzhiyun enum rw_hint hint;
290*4882a593Smuzhiyun u64 h;
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun switch (cmd) {
293*4882a593Smuzhiyun case F_GET_FILE_RW_HINT:
294*4882a593Smuzhiyun h = file_write_hint(file);
295*4882a593Smuzhiyun if (copy_to_user(argp, &h, sizeof(*argp)))
296*4882a593Smuzhiyun return -EFAULT;
297*4882a593Smuzhiyun return 0;
298*4882a593Smuzhiyun case F_SET_FILE_RW_HINT:
299*4882a593Smuzhiyun if (copy_from_user(&h, argp, sizeof(h)))
300*4882a593Smuzhiyun return -EFAULT;
301*4882a593Smuzhiyun hint = (enum rw_hint) h;
302*4882a593Smuzhiyun if (!rw_hint_valid(hint))
303*4882a593Smuzhiyun return -EINVAL;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun spin_lock(&file->f_lock);
306*4882a593Smuzhiyun file->f_write_hint = hint;
307*4882a593Smuzhiyun spin_unlock(&file->f_lock);
308*4882a593Smuzhiyun return 0;
309*4882a593Smuzhiyun case F_GET_RW_HINT:
310*4882a593Smuzhiyun h = inode->i_write_hint;
311*4882a593Smuzhiyun if (copy_to_user(argp, &h, sizeof(*argp)))
312*4882a593Smuzhiyun return -EFAULT;
313*4882a593Smuzhiyun return 0;
314*4882a593Smuzhiyun case F_SET_RW_HINT:
315*4882a593Smuzhiyun if (copy_from_user(&h, argp, sizeof(h)))
316*4882a593Smuzhiyun return -EFAULT;
317*4882a593Smuzhiyun hint = (enum rw_hint) h;
318*4882a593Smuzhiyun if (!rw_hint_valid(hint))
319*4882a593Smuzhiyun return -EINVAL;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun inode_lock(inode);
322*4882a593Smuzhiyun inode->i_write_hint = hint;
323*4882a593Smuzhiyun inode_unlock(inode);
324*4882a593Smuzhiyun return 0;
325*4882a593Smuzhiyun default:
326*4882a593Smuzhiyun return -EINVAL;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
do_fcntl(int fd,unsigned int cmd,unsigned long arg,struct file * filp)330*4882a593Smuzhiyun static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
331*4882a593Smuzhiyun struct file *filp)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun void __user *argp = (void __user *)arg;
334*4882a593Smuzhiyun struct flock flock;
335*4882a593Smuzhiyun long err = -EINVAL;
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun switch (cmd) {
338*4882a593Smuzhiyun case F_DUPFD:
339*4882a593Smuzhiyun err = f_dupfd(arg, filp, 0);
340*4882a593Smuzhiyun break;
341*4882a593Smuzhiyun case F_DUPFD_CLOEXEC:
342*4882a593Smuzhiyun err = f_dupfd(arg, filp, O_CLOEXEC);
343*4882a593Smuzhiyun break;
344*4882a593Smuzhiyun case F_GETFD:
345*4882a593Smuzhiyun err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
346*4882a593Smuzhiyun break;
347*4882a593Smuzhiyun case F_SETFD:
348*4882a593Smuzhiyun err = 0;
349*4882a593Smuzhiyun set_close_on_exec(fd, arg & FD_CLOEXEC);
350*4882a593Smuzhiyun break;
351*4882a593Smuzhiyun case F_GETFL:
352*4882a593Smuzhiyun err = filp->f_flags;
353*4882a593Smuzhiyun break;
354*4882a593Smuzhiyun case F_SETFL:
355*4882a593Smuzhiyun err = setfl(fd, filp, arg);
356*4882a593Smuzhiyun break;
357*4882a593Smuzhiyun #if BITS_PER_LONG != 32
358*4882a593Smuzhiyun /* 32-bit arches must use fcntl64() */
359*4882a593Smuzhiyun case F_OFD_GETLK:
360*4882a593Smuzhiyun #endif
361*4882a593Smuzhiyun case F_GETLK:
362*4882a593Smuzhiyun if (copy_from_user(&flock, argp, sizeof(flock)))
363*4882a593Smuzhiyun return -EFAULT;
364*4882a593Smuzhiyun err = fcntl_getlk(filp, cmd, &flock);
365*4882a593Smuzhiyun if (!err && copy_to_user(argp, &flock, sizeof(flock)))
366*4882a593Smuzhiyun return -EFAULT;
367*4882a593Smuzhiyun break;
368*4882a593Smuzhiyun #if BITS_PER_LONG != 32
369*4882a593Smuzhiyun /* 32-bit arches must use fcntl64() */
370*4882a593Smuzhiyun case F_OFD_SETLK:
371*4882a593Smuzhiyun case F_OFD_SETLKW:
372*4882a593Smuzhiyun #endif
373*4882a593Smuzhiyun fallthrough;
374*4882a593Smuzhiyun case F_SETLK:
375*4882a593Smuzhiyun case F_SETLKW:
376*4882a593Smuzhiyun if (copy_from_user(&flock, argp, sizeof(flock)))
377*4882a593Smuzhiyun return -EFAULT;
378*4882a593Smuzhiyun err = fcntl_setlk(fd, filp, cmd, &flock);
379*4882a593Smuzhiyun break;
380*4882a593Smuzhiyun case F_GETOWN:
381*4882a593Smuzhiyun /*
382*4882a593Smuzhiyun * XXX If f_owner is a process group, the
383*4882a593Smuzhiyun * negative return value will get converted
384*4882a593Smuzhiyun * into an error. Oops. If we keep the
385*4882a593Smuzhiyun * current syscall conventions, the only way
386*4882a593Smuzhiyun * to fix this will be in libc.
387*4882a593Smuzhiyun */
388*4882a593Smuzhiyun err = f_getown(filp);
389*4882a593Smuzhiyun force_successful_syscall_return();
390*4882a593Smuzhiyun break;
391*4882a593Smuzhiyun case F_SETOWN:
392*4882a593Smuzhiyun err = f_setown(filp, arg, 1);
393*4882a593Smuzhiyun break;
394*4882a593Smuzhiyun case F_GETOWN_EX:
395*4882a593Smuzhiyun err = f_getown_ex(filp, arg);
396*4882a593Smuzhiyun break;
397*4882a593Smuzhiyun case F_SETOWN_EX:
398*4882a593Smuzhiyun err = f_setown_ex(filp, arg);
399*4882a593Smuzhiyun break;
400*4882a593Smuzhiyun case F_GETOWNER_UIDS:
401*4882a593Smuzhiyun err = f_getowner_uids(filp, arg);
402*4882a593Smuzhiyun break;
403*4882a593Smuzhiyun case F_GETSIG:
404*4882a593Smuzhiyun err = filp->f_owner.signum;
405*4882a593Smuzhiyun break;
406*4882a593Smuzhiyun case F_SETSIG:
407*4882a593Smuzhiyun /* arg == 0 restores default behaviour. */
408*4882a593Smuzhiyun if (!valid_signal(arg)) {
409*4882a593Smuzhiyun break;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun err = 0;
412*4882a593Smuzhiyun filp->f_owner.signum = arg;
413*4882a593Smuzhiyun break;
414*4882a593Smuzhiyun case F_GETLEASE:
415*4882a593Smuzhiyun err = fcntl_getlease(filp);
416*4882a593Smuzhiyun break;
417*4882a593Smuzhiyun case F_SETLEASE:
418*4882a593Smuzhiyun err = fcntl_setlease(fd, filp, arg);
419*4882a593Smuzhiyun break;
420*4882a593Smuzhiyun case F_NOTIFY:
421*4882a593Smuzhiyun err = fcntl_dirnotify(fd, filp, arg);
422*4882a593Smuzhiyun break;
423*4882a593Smuzhiyun case F_SETPIPE_SZ:
424*4882a593Smuzhiyun case F_GETPIPE_SZ:
425*4882a593Smuzhiyun err = pipe_fcntl(filp, cmd, arg);
426*4882a593Smuzhiyun break;
427*4882a593Smuzhiyun case F_ADD_SEALS:
428*4882a593Smuzhiyun case F_GET_SEALS:
429*4882a593Smuzhiyun err = memfd_fcntl(filp, cmd, arg);
430*4882a593Smuzhiyun break;
431*4882a593Smuzhiyun case F_GET_RW_HINT:
432*4882a593Smuzhiyun case F_SET_RW_HINT:
433*4882a593Smuzhiyun case F_GET_FILE_RW_HINT:
434*4882a593Smuzhiyun case F_SET_FILE_RW_HINT:
435*4882a593Smuzhiyun err = fcntl_rw_hint(filp, cmd, arg);
436*4882a593Smuzhiyun break;
437*4882a593Smuzhiyun default:
438*4882a593Smuzhiyun break;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun return err;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun
check_fcntl_cmd(unsigned cmd)443*4882a593Smuzhiyun static int check_fcntl_cmd(unsigned cmd)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun switch (cmd) {
446*4882a593Smuzhiyun case F_DUPFD:
447*4882a593Smuzhiyun case F_DUPFD_CLOEXEC:
448*4882a593Smuzhiyun case F_GETFD:
449*4882a593Smuzhiyun case F_SETFD:
450*4882a593Smuzhiyun case F_GETFL:
451*4882a593Smuzhiyun return 1;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun return 0;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
SYSCALL_DEFINE3(fcntl,unsigned int,fd,unsigned int,cmd,unsigned long,arg)456*4882a593Smuzhiyun SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun struct fd f = fdget_raw(fd);
459*4882a593Smuzhiyun long err = -EBADF;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun if (!f.file)
462*4882a593Smuzhiyun goto out;
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun if (unlikely(f.file->f_mode & FMODE_PATH)) {
465*4882a593Smuzhiyun if (!check_fcntl_cmd(cmd))
466*4882a593Smuzhiyun goto out1;
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun err = security_file_fcntl(f.file, cmd, arg);
470*4882a593Smuzhiyun if (!err)
471*4882a593Smuzhiyun err = do_fcntl(fd, cmd, arg, f.file);
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun out1:
474*4882a593Smuzhiyun fdput(f);
475*4882a593Smuzhiyun out:
476*4882a593Smuzhiyun return err;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun #if BITS_PER_LONG == 32
SYSCALL_DEFINE3(fcntl64,unsigned int,fd,unsigned int,cmd,unsigned long,arg)480*4882a593Smuzhiyun SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
481*4882a593Smuzhiyun unsigned long, arg)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun void __user *argp = (void __user *)arg;
484*4882a593Smuzhiyun struct fd f = fdget_raw(fd);
485*4882a593Smuzhiyun struct flock64 flock;
486*4882a593Smuzhiyun long err = -EBADF;
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun if (!f.file)
489*4882a593Smuzhiyun goto out;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun if (unlikely(f.file->f_mode & FMODE_PATH)) {
492*4882a593Smuzhiyun if (!check_fcntl_cmd(cmd))
493*4882a593Smuzhiyun goto out1;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun err = security_file_fcntl(f.file, cmd, arg);
497*4882a593Smuzhiyun if (err)
498*4882a593Smuzhiyun goto out1;
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun switch (cmd) {
501*4882a593Smuzhiyun case F_GETLK64:
502*4882a593Smuzhiyun case F_OFD_GETLK:
503*4882a593Smuzhiyun err = -EFAULT;
504*4882a593Smuzhiyun if (copy_from_user(&flock, argp, sizeof(flock)))
505*4882a593Smuzhiyun break;
506*4882a593Smuzhiyun err = fcntl_getlk64(f.file, cmd, &flock);
507*4882a593Smuzhiyun if (!err && copy_to_user(argp, &flock, sizeof(flock)))
508*4882a593Smuzhiyun err = -EFAULT;
509*4882a593Smuzhiyun break;
510*4882a593Smuzhiyun case F_SETLK64:
511*4882a593Smuzhiyun case F_SETLKW64:
512*4882a593Smuzhiyun case F_OFD_SETLK:
513*4882a593Smuzhiyun case F_OFD_SETLKW:
514*4882a593Smuzhiyun err = -EFAULT;
515*4882a593Smuzhiyun if (copy_from_user(&flock, argp, sizeof(flock)))
516*4882a593Smuzhiyun break;
517*4882a593Smuzhiyun err = fcntl_setlk64(fd, f.file, cmd, &flock);
518*4882a593Smuzhiyun break;
519*4882a593Smuzhiyun default:
520*4882a593Smuzhiyun err = do_fcntl(fd, cmd, arg, f.file);
521*4882a593Smuzhiyun break;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun out1:
524*4882a593Smuzhiyun fdput(f);
525*4882a593Smuzhiyun out:
526*4882a593Smuzhiyun return err;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun #endif
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
531*4882a593Smuzhiyun /* careful - don't use anywhere else */
532*4882a593Smuzhiyun #define copy_flock_fields(dst, src) \
533*4882a593Smuzhiyun (dst)->l_type = (src)->l_type; \
534*4882a593Smuzhiyun (dst)->l_whence = (src)->l_whence; \
535*4882a593Smuzhiyun (dst)->l_start = (src)->l_start; \
536*4882a593Smuzhiyun (dst)->l_len = (src)->l_len; \
537*4882a593Smuzhiyun (dst)->l_pid = (src)->l_pid;
538*4882a593Smuzhiyun
get_compat_flock(struct flock * kfl,const struct compat_flock __user * ufl)539*4882a593Smuzhiyun static int get_compat_flock(struct flock *kfl, const struct compat_flock __user *ufl)
540*4882a593Smuzhiyun {
541*4882a593Smuzhiyun struct compat_flock fl;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun if (copy_from_user(&fl, ufl, sizeof(struct compat_flock)))
544*4882a593Smuzhiyun return -EFAULT;
545*4882a593Smuzhiyun copy_flock_fields(kfl, &fl);
546*4882a593Smuzhiyun return 0;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun
get_compat_flock64(struct flock * kfl,const struct compat_flock64 __user * ufl)549*4882a593Smuzhiyun static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun struct compat_flock64 fl;
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64)))
554*4882a593Smuzhiyun return -EFAULT;
555*4882a593Smuzhiyun copy_flock_fields(kfl, &fl);
556*4882a593Smuzhiyun return 0;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
put_compat_flock(const struct flock * kfl,struct compat_flock __user * ufl)559*4882a593Smuzhiyun static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun struct compat_flock fl;
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun memset(&fl, 0, sizeof(struct compat_flock));
564*4882a593Smuzhiyun copy_flock_fields(&fl, kfl);
565*4882a593Smuzhiyun if (copy_to_user(ufl, &fl, sizeof(struct compat_flock)))
566*4882a593Smuzhiyun return -EFAULT;
567*4882a593Smuzhiyun return 0;
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun
put_compat_flock64(const struct flock * kfl,struct compat_flock64 __user * ufl)570*4882a593Smuzhiyun static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun struct compat_flock64 fl;
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(kfl->l_start) > sizeof(ufl->l_start));
575*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(kfl->l_len) > sizeof(ufl->l_len));
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun memset(&fl, 0, sizeof(struct compat_flock64));
578*4882a593Smuzhiyun copy_flock_fields(&fl, kfl);
579*4882a593Smuzhiyun if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64)))
580*4882a593Smuzhiyun return -EFAULT;
581*4882a593Smuzhiyun return 0;
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun #undef copy_flock_fields
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun static unsigned int
convert_fcntl_cmd(unsigned int cmd)586*4882a593Smuzhiyun convert_fcntl_cmd(unsigned int cmd)
587*4882a593Smuzhiyun {
588*4882a593Smuzhiyun switch (cmd) {
589*4882a593Smuzhiyun case F_GETLK64:
590*4882a593Smuzhiyun return F_GETLK;
591*4882a593Smuzhiyun case F_SETLK64:
592*4882a593Smuzhiyun return F_SETLK;
593*4882a593Smuzhiyun case F_SETLKW64:
594*4882a593Smuzhiyun return F_SETLKW;
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun return cmd;
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun /*
601*4882a593Smuzhiyun * GETLK was successful and we need to return the data, but it needs to fit in
602*4882a593Smuzhiyun * the compat structure.
603*4882a593Smuzhiyun * l_start shouldn't be too big, unless the original start + end is greater than
604*4882a593Smuzhiyun * COMPAT_OFF_T_MAX, in which case the app was asking for trouble, so we return
605*4882a593Smuzhiyun * -EOVERFLOW in that case. l_len could be too big, in which case we just
606*4882a593Smuzhiyun * truncate it, and only allow the app to see that part of the conflicting lock
607*4882a593Smuzhiyun * that might make sense to it anyway
608*4882a593Smuzhiyun */
fixup_compat_flock(struct flock * flock)609*4882a593Smuzhiyun static int fixup_compat_flock(struct flock *flock)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun if (flock->l_start > COMPAT_OFF_T_MAX)
612*4882a593Smuzhiyun return -EOVERFLOW;
613*4882a593Smuzhiyun if (flock->l_len > COMPAT_OFF_T_MAX)
614*4882a593Smuzhiyun flock->l_len = COMPAT_OFF_T_MAX;
615*4882a593Smuzhiyun return 0;
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun
do_compat_fcntl64(unsigned int fd,unsigned int cmd,compat_ulong_t arg)618*4882a593Smuzhiyun static long do_compat_fcntl64(unsigned int fd, unsigned int cmd,
619*4882a593Smuzhiyun compat_ulong_t arg)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun struct fd f = fdget_raw(fd);
622*4882a593Smuzhiyun struct flock flock;
623*4882a593Smuzhiyun long err = -EBADF;
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun if (!f.file)
626*4882a593Smuzhiyun return err;
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun if (unlikely(f.file->f_mode & FMODE_PATH)) {
629*4882a593Smuzhiyun if (!check_fcntl_cmd(cmd))
630*4882a593Smuzhiyun goto out_put;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun err = security_file_fcntl(f.file, cmd, arg);
634*4882a593Smuzhiyun if (err)
635*4882a593Smuzhiyun goto out_put;
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun switch (cmd) {
638*4882a593Smuzhiyun case F_GETLK:
639*4882a593Smuzhiyun err = get_compat_flock(&flock, compat_ptr(arg));
640*4882a593Smuzhiyun if (err)
641*4882a593Smuzhiyun break;
642*4882a593Smuzhiyun err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
643*4882a593Smuzhiyun if (err)
644*4882a593Smuzhiyun break;
645*4882a593Smuzhiyun err = fixup_compat_flock(&flock);
646*4882a593Smuzhiyun if (!err)
647*4882a593Smuzhiyun err = put_compat_flock(&flock, compat_ptr(arg));
648*4882a593Smuzhiyun break;
649*4882a593Smuzhiyun case F_GETLK64:
650*4882a593Smuzhiyun case F_OFD_GETLK:
651*4882a593Smuzhiyun err = get_compat_flock64(&flock, compat_ptr(arg));
652*4882a593Smuzhiyun if (err)
653*4882a593Smuzhiyun break;
654*4882a593Smuzhiyun err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock);
655*4882a593Smuzhiyun if (!err)
656*4882a593Smuzhiyun err = put_compat_flock64(&flock, compat_ptr(arg));
657*4882a593Smuzhiyun break;
658*4882a593Smuzhiyun case F_SETLK:
659*4882a593Smuzhiyun case F_SETLKW:
660*4882a593Smuzhiyun err = get_compat_flock(&flock, compat_ptr(arg));
661*4882a593Smuzhiyun if (err)
662*4882a593Smuzhiyun break;
663*4882a593Smuzhiyun err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
664*4882a593Smuzhiyun break;
665*4882a593Smuzhiyun case F_SETLK64:
666*4882a593Smuzhiyun case F_SETLKW64:
667*4882a593Smuzhiyun case F_OFD_SETLK:
668*4882a593Smuzhiyun case F_OFD_SETLKW:
669*4882a593Smuzhiyun err = get_compat_flock64(&flock, compat_ptr(arg));
670*4882a593Smuzhiyun if (err)
671*4882a593Smuzhiyun break;
672*4882a593Smuzhiyun err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock);
673*4882a593Smuzhiyun break;
674*4882a593Smuzhiyun default:
675*4882a593Smuzhiyun err = do_fcntl(fd, cmd, arg, f.file);
676*4882a593Smuzhiyun break;
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun out_put:
679*4882a593Smuzhiyun fdput(f);
680*4882a593Smuzhiyun return err;
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun
COMPAT_SYSCALL_DEFINE3(fcntl64,unsigned int,fd,unsigned int,cmd,compat_ulong_t,arg)683*4882a593Smuzhiyun COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
684*4882a593Smuzhiyun compat_ulong_t, arg)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun return do_compat_fcntl64(fd, cmd, arg);
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun
COMPAT_SYSCALL_DEFINE3(fcntl,unsigned int,fd,unsigned int,cmd,compat_ulong_t,arg)689*4882a593Smuzhiyun COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
690*4882a593Smuzhiyun compat_ulong_t, arg)
691*4882a593Smuzhiyun {
692*4882a593Smuzhiyun switch (cmd) {
693*4882a593Smuzhiyun case F_GETLK64:
694*4882a593Smuzhiyun case F_SETLK64:
695*4882a593Smuzhiyun case F_SETLKW64:
696*4882a593Smuzhiyun case F_OFD_GETLK:
697*4882a593Smuzhiyun case F_OFD_SETLK:
698*4882a593Smuzhiyun case F_OFD_SETLKW:
699*4882a593Smuzhiyun return -EINVAL;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun return do_compat_fcntl64(fd, cmd, arg);
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun #endif
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun /* Table to convert sigio signal codes into poll band bitmaps */
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun static const __poll_t band_table[NSIGPOLL] = {
708*4882a593Smuzhiyun EPOLLIN | EPOLLRDNORM, /* POLL_IN */
709*4882a593Smuzhiyun EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND, /* POLL_OUT */
710*4882a593Smuzhiyun EPOLLIN | EPOLLRDNORM | EPOLLMSG, /* POLL_MSG */
711*4882a593Smuzhiyun EPOLLERR, /* POLL_ERR */
712*4882a593Smuzhiyun EPOLLPRI | EPOLLRDBAND, /* POLL_PRI */
713*4882a593Smuzhiyun EPOLLHUP | EPOLLERR /* POLL_HUP */
714*4882a593Smuzhiyun };
715*4882a593Smuzhiyun
sigio_perm(struct task_struct * p,struct fown_struct * fown,int sig)716*4882a593Smuzhiyun static inline int sigio_perm(struct task_struct *p,
717*4882a593Smuzhiyun struct fown_struct *fown, int sig)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun const struct cred *cred;
720*4882a593Smuzhiyun int ret;
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun rcu_read_lock();
723*4882a593Smuzhiyun cred = __task_cred(p);
724*4882a593Smuzhiyun ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
725*4882a593Smuzhiyun uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
726*4882a593Smuzhiyun uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
727*4882a593Smuzhiyun !security_file_send_sigiotask(p, fown, sig));
728*4882a593Smuzhiyun rcu_read_unlock();
729*4882a593Smuzhiyun return ret;
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun
send_sigio_to_task(struct task_struct * p,struct fown_struct * fown,int fd,int reason,enum pid_type type)732*4882a593Smuzhiyun static void send_sigio_to_task(struct task_struct *p,
733*4882a593Smuzhiyun struct fown_struct *fown,
734*4882a593Smuzhiyun int fd, int reason, enum pid_type type)
735*4882a593Smuzhiyun {
736*4882a593Smuzhiyun /*
737*4882a593Smuzhiyun * F_SETSIG can change ->signum lockless in parallel, make
738*4882a593Smuzhiyun * sure we read it once and use the same value throughout.
739*4882a593Smuzhiyun */
740*4882a593Smuzhiyun int signum = READ_ONCE(fown->signum);
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun if (!sigio_perm(p, fown, signum))
743*4882a593Smuzhiyun return;
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun switch (signum) {
746*4882a593Smuzhiyun default: {
747*4882a593Smuzhiyun kernel_siginfo_t si;
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun /* Queue a rt signal with the appropriate fd as its
750*4882a593Smuzhiyun value. We use SI_SIGIO as the source, not
751*4882a593Smuzhiyun SI_KERNEL, since kernel signals always get
752*4882a593Smuzhiyun delivered even if we can't queue. Failure to
753*4882a593Smuzhiyun queue in this case _should_ be reported; we fall
754*4882a593Smuzhiyun back to SIGIO in that case. --sct */
755*4882a593Smuzhiyun clear_siginfo(&si);
756*4882a593Smuzhiyun si.si_signo = signum;
757*4882a593Smuzhiyun si.si_errno = 0;
758*4882a593Smuzhiyun si.si_code = reason;
759*4882a593Smuzhiyun /*
760*4882a593Smuzhiyun * Posix definies POLL_IN and friends to be signal
761*4882a593Smuzhiyun * specific si_codes for SIG_POLL. Linux extended
762*4882a593Smuzhiyun * these si_codes to other signals in a way that is
763*4882a593Smuzhiyun * ambiguous if other signals also have signal
764*4882a593Smuzhiyun * specific si_codes. In that case use SI_SIGIO instead
765*4882a593Smuzhiyun * to remove the ambiguity.
766*4882a593Smuzhiyun */
767*4882a593Smuzhiyun if ((signum != SIGPOLL) && sig_specific_sicodes(signum))
768*4882a593Smuzhiyun si.si_code = SI_SIGIO;
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun /* Make sure we are called with one of the POLL_*
771*4882a593Smuzhiyun reasons, otherwise we could leak kernel stack into
772*4882a593Smuzhiyun userspace. */
773*4882a593Smuzhiyun BUG_ON((reason < POLL_IN) || ((reason - POLL_IN) >= NSIGPOLL));
774*4882a593Smuzhiyun if (reason - POLL_IN >= NSIGPOLL)
775*4882a593Smuzhiyun si.si_band = ~0L;
776*4882a593Smuzhiyun else
777*4882a593Smuzhiyun si.si_band = mangle_poll(band_table[reason - POLL_IN]);
778*4882a593Smuzhiyun si.si_fd = fd;
779*4882a593Smuzhiyun if (!do_send_sig_info(signum, &si, p, type))
780*4882a593Smuzhiyun break;
781*4882a593Smuzhiyun }
782*4882a593Smuzhiyun fallthrough; /* fall back on the old plain SIGIO signal */
783*4882a593Smuzhiyun case 0:
784*4882a593Smuzhiyun do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, type);
785*4882a593Smuzhiyun }
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun
send_sigio(struct fown_struct * fown,int fd,int band)788*4882a593Smuzhiyun void send_sigio(struct fown_struct *fown, int fd, int band)
789*4882a593Smuzhiyun {
790*4882a593Smuzhiyun struct task_struct *p;
791*4882a593Smuzhiyun enum pid_type type;
792*4882a593Smuzhiyun unsigned long flags;
793*4882a593Smuzhiyun struct pid *pid;
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun read_lock_irqsave(&fown->lock, flags);
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun type = fown->pid_type;
798*4882a593Smuzhiyun pid = fown->pid;
799*4882a593Smuzhiyun if (!pid)
800*4882a593Smuzhiyun goto out_unlock_fown;
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun if (type <= PIDTYPE_TGID) {
803*4882a593Smuzhiyun rcu_read_lock();
804*4882a593Smuzhiyun p = pid_task(pid, PIDTYPE_PID);
805*4882a593Smuzhiyun if (p)
806*4882a593Smuzhiyun send_sigio_to_task(p, fown, fd, band, type);
807*4882a593Smuzhiyun rcu_read_unlock();
808*4882a593Smuzhiyun } else {
809*4882a593Smuzhiyun read_lock(&tasklist_lock);
810*4882a593Smuzhiyun do_each_pid_task(pid, type, p) {
811*4882a593Smuzhiyun send_sigio_to_task(p, fown, fd, band, type);
812*4882a593Smuzhiyun } while_each_pid_task(pid, type, p);
813*4882a593Smuzhiyun read_unlock(&tasklist_lock);
814*4882a593Smuzhiyun }
815*4882a593Smuzhiyun out_unlock_fown:
816*4882a593Smuzhiyun read_unlock_irqrestore(&fown->lock, flags);
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun
send_sigurg_to_task(struct task_struct * p,struct fown_struct * fown,enum pid_type type)819*4882a593Smuzhiyun static void send_sigurg_to_task(struct task_struct *p,
820*4882a593Smuzhiyun struct fown_struct *fown, enum pid_type type)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun if (sigio_perm(p, fown, SIGURG))
823*4882a593Smuzhiyun do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, type);
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun
send_sigurg(struct fown_struct * fown)826*4882a593Smuzhiyun int send_sigurg(struct fown_struct *fown)
827*4882a593Smuzhiyun {
828*4882a593Smuzhiyun struct task_struct *p;
829*4882a593Smuzhiyun enum pid_type type;
830*4882a593Smuzhiyun struct pid *pid;
831*4882a593Smuzhiyun unsigned long flags;
832*4882a593Smuzhiyun int ret = 0;
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun read_lock_irqsave(&fown->lock, flags);
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun type = fown->pid_type;
837*4882a593Smuzhiyun pid = fown->pid;
838*4882a593Smuzhiyun if (!pid)
839*4882a593Smuzhiyun goto out_unlock_fown;
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun ret = 1;
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun if (type <= PIDTYPE_TGID) {
844*4882a593Smuzhiyun rcu_read_lock();
845*4882a593Smuzhiyun p = pid_task(pid, PIDTYPE_PID);
846*4882a593Smuzhiyun if (p)
847*4882a593Smuzhiyun send_sigurg_to_task(p, fown, type);
848*4882a593Smuzhiyun rcu_read_unlock();
849*4882a593Smuzhiyun } else {
850*4882a593Smuzhiyun read_lock(&tasklist_lock);
851*4882a593Smuzhiyun do_each_pid_task(pid, type, p) {
852*4882a593Smuzhiyun send_sigurg_to_task(p, fown, type);
853*4882a593Smuzhiyun } while_each_pid_task(pid, type, p);
854*4882a593Smuzhiyun read_unlock(&tasklist_lock);
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun out_unlock_fown:
857*4882a593Smuzhiyun read_unlock_irqrestore(&fown->lock, flags);
858*4882a593Smuzhiyun return ret;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun static DEFINE_SPINLOCK(fasync_lock);
862*4882a593Smuzhiyun static struct kmem_cache *fasync_cache __read_mostly;
863*4882a593Smuzhiyun
fasync_free_rcu(struct rcu_head * head)864*4882a593Smuzhiyun static void fasync_free_rcu(struct rcu_head *head)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun kmem_cache_free(fasync_cache,
867*4882a593Smuzhiyun container_of(head, struct fasync_struct, fa_rcu));
868*4882a593Smuzhiyun }
869*4882a593Smuzhiyun
870*4882a593Smuzhiyun /*
871*4882a593Smuzhiyun * Remove a fasync entry. If successfully removed, return
872*4882a593Smuzhiyun * positive and clear the FASYNC flag. If no entry exists,
873*4882a593Smuzhiyun * do nothing and return 0.
874*4882a593Smuzhiyun *
875*4882a593Smuzhiyun * NOTE! It is very important that the FASYNC flag always
876*4882a593Smuzhiyun * match the state "is the filp on a fasync list".
877*4882a593Smuzhiyun *
878*4882a593Smuzhiyun */
fasync_remove_entry(struct file * filp,struct fasync_struct ** fapp)879*4882a593Smuzhiyun int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun struct fasync_struct *fa, **fp;
882*4882a593Smuzhiyun int result = 0;
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun spin_lock(&filp->f_lock);
885*4882a593Smuzhiyun spin_lock(&fasync_lock);
886*4882a593Smuzhiyun for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
887*4882a593Smuzhiyun if (fa->fa_file != filp)
888*4882a593Smuzhiyun continue;
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun write_lock_irq(&fa->fa_lock);
891*4882a593Smuzhiyun fa->fa_file = NULL;
892*4882a593Smuzhiyun write_unlock_irq(&fa->fa_lock);
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun *fp = fa->fa_next;
895*4882a593Smuzhiyun call_rcu(&fa->fa_rcu, fasync_free_rcu);
896*4882a593Smuzhiyun filp->f_flags &= ~FASYNC;
897*4882a593Smuzhiyun result = 1;
898*4882a593Smuzhiyun break;
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun spin_unlock(&fasync_lock);
901*4882a593Smuzhiyun spin_unlock(&filp->f_lock);
902*4882a593Smuzhiyun return result;
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun
fasync_alloc(void)905*4882a593Smuzhiyun struct fasync_struct *fasync_alloc(void)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun /*
911*4882a593Smuzhiyun * NOTE! This can be used only for unused fasync entries:
912*4882a593Smuzhiyun * entries that actually got inserted on the fasync list
913*4882a593Smuzhiyun * need to be released by rcu - see fasync_remove_entry.
914*4882a593Smuzhiyun */
fasync_free(struct fasync_struct * new)915*4882a593Smuzhiyun void fasync_free(struct fasync_struct *new)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun kmem_cache_free(fasync_cache, new);
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun /*
921*4882a593Smuzhiyun * Insert a new entry into the fasync list. Return the pointer to the
922*4882a593Smuzhiyun * old one if we didn't use the new one.
923*4882a593Smuzhiyun *
924*4882a593Smuzhiyun * NOTE! It is very important that the FASYNC flag always
925*4882a593Smuzhiyun * match the state "is the filp on a fasync list".
926*4882a593Smuzhiyun */
fasync_insert_entry(int fd,struct file * filp,struct fasync_struct ** fapp,struct fasync_struct * new)927*4882a593Smuzhiyun struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
928*4882a593Smuzhiyun {
929*4882a593Smuzhiyun struct fasync_struct *fa, **fp;
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun spin_lock(&filp->f_lock);
932*4882a593Smuzhiyun spin_lock(&fasync_lock);
933*4882a593Smuzhiyun for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
934*4882a593Smuzhiyun if (fa->fa_file != filp)
935*4882a593Smuzhiyun continue;
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun write_lock_irq(&fa->fa_lock);
938*4882a593Smuzhiyun fa->fa_fd = fd;
939*4882a593Smuzhiyun write_unlock_irq(&fa->fa_lock);
940*4882a593Smuzhiyun goto out;
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun rwlock_init(&new->fa_lock);
944*4882a593Smuzhiyun new->magic = FASYNC_MAGIC;
945*4882a593Smuzhiyun new->fa_file = filp;
946*4882a593Smuzhiyun new->fa_fd = fd;
947*4882a593Smuzhiyun new->fa_next = *fapp;
948*4882a593Smuzhiyun rcu_assign_pointer(*fapp, new);
949*4882a593Smuzhiyun filp->f_flags |= FASYNC;
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun out:
952*4882a593Smuzhiyun spin_unlock(&fasync_lock);
953*4882a593Smuzhiyun spin_unlock(&filp->f_lock);
954*4882a593Smuzhiyun return fa;
955*4882a593Smuzhiyun }
956*4882a593Smuzhiyun
957*4882a593Smuzhiyun /*
958*4882a593Smuzhiyun * Add a fasync entry. Return negative on error, positive if
959*4882a593Smuzhiyun * added, and zero if did nothing but change an existing one.
960*4882a593Smuzhiyun */
fasync_add_entry(int fd,struct file * filp,struct fasync_struct ** fapp)961*4882a593Smuzhiyun static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
962*4882a593Smuzhiyun {
963*4882a593Smuzhiyun struct fasync_struct *new;
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun new = fasync_alloc();
966*4882a593Smuzhiyun if (!new)
967*4882a593Smuzhiyun return -ENOMEM;
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun /*
970*4882a593Smuzhiyun * fasync_insert_entry() returns the old (update) entry if
971*4882a593Smuzhiyun * it existed.
972*4882a593Smuzhiyun *
973*4882a593Smuzhiyun * So free the (unused) new entry and return 0 to let the
974*4882a593Smuzhiyun * caller know that we didn't add any new fasync entries.
975*4882a593Smuzhiyun */
976*4882a593Smuzhiyun if (fasync_insert_entry(fd, filp, fapp, new)) {
977*4882a593Smuzhiyun fasync_free(new);
978*4882a593Smuzhiyun return 0;
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun return 1;
982*4882a593Smuzhiyun }
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun /*
985*4882a593Smuzhiyun * fasync_helper() is used by almost all character device drivers
986*4882a593Smuzhiyun * to set up the fasync queue, and for regular files by the file
987*4882a593Smuzhiyun * lease code. It returns negative on error, 0 if it did no changes
988*4882a593Smuzhiyun * and positive if it added/deleted the entry.
989*4882a593Smuzhiyun */
fasync_helper(int fd,struct file * filp,int on,struct fasync_struct ** fapp)990*4882a593Smuzhiyun int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
991*4882a593Smuzhiyun {
992*4882a593Smuzhiyun if (!on)
993*4882a593Smuzhiyun return fasync_remove_entry(filp, fapp);
994*4882a593Smuzhiyun return fasync_add_entry(fd, filp, fapp);
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun EXPORT_SYMBOL(fasync_helper);
998*4882a593Smuzhiyun
999*4882a593Smuzhiyun /*
1000*4882a593Smuzhiyun * rcu_read_lock() is held
1001*4882a593Smuzhiyun */
kill_fasync_rcu(struct fasync_struct * fa,int sig,int band)1002*4882a593Smuzhiyun static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
1003*4882a593Smuzhiyun {
1004*4882a593Smuzhiyun while (fa) {
1005*4882a593Smuzhiyun struct fown_struct *fown;
1006*4882a593Smuzhiyun unsigned long flags;
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun if (fa->magic != FASYNC_MAGIC) {
1009*4882a593Smuzhiyun printk(KERN_ERR "kill_fasync: bad magic number in "
1010*4882a593Smuzhiyun "fasync_struct!\n");
1011*4882a593Smuzhiyun return;
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun read_lock_irqsave(&fa->fa_lock, flags);
1014*4882a593Smuzhiyun if (fa->fa_file) {
1015*4882a593Smuzhiyun fown = &fa->fa_file->f_owner;
1016*4882a593Smuzhiyun /* Don't send SIGURG to processes which have not set a
1017*4882a593Smuzhiyun queued signum: SIGURG has its own default signalling
1018*4882a593Smuzhiyun mechanism. */
1019*4882a593Smuzhiyun if (!(sig == SIGURG && fown->signum == 0))
1020*4882a593Smuzhiyun send_sigio(fown, fa->fa_fd, band);
1021*4882a593Smuzhiyun }
1022*4882a593Smuzhiyun read_unlock_irqrestore(&fa->fa_lock, flags);
1023*4882a593Smuzhiyun fa = rcu_dereference(fa->fa_next);
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun }
1026*4882a593Smuzhiyun
kill_fasync(struct fasync_struct ** fp,int sig,int band)1027*4882a593Smuzhiyun void kill_fasync(struct fasync_struct **fp, int sig, int band)
1028*4882a593Smuzhiyun {
1029*4882a593Smuzhiyun /* First a quick test without locking: usually
1030*4882a593Smuzhiyun * the list is empty.
1031*4882a593Smuzhiyun */
1032*4882a593Smuzhiyun if (*fp) {
1033*4882a593Smuzhiyun rcu_read_lock();
1034*4882a593Smuzhiyun kill_fasync_rcu(rcu_dereference(*fp), sig, band);
1035*4882a593Smuzhiyun rcu_read_unlock();
1036*4882a593Smuzhiyun }
1037*4882a593Smuzhiyun }
1038*4882a593Smuzhiyun EXPORT_SYMBOL(kill_fasync);
1039*4882a593Smuzhiyun
fcntl_init(void)1040*4882a593Smuzhiyun static int __init fcntl_init(void)
1041*4882a593Smuzhiyun {
1042*4882a593Smuzhiyun /*
1043*4882a593Smuzhiyun * Please add new bits here to ensure allocation uniqueness.
1044*4882a593Smuzhiyun * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
1045*4882a593Smuzhiyun * is defined as O_NONBLOCK on some platforms and not on others.
1046*4882a593Smuzhiyun */
1047*4882a593Smuzhiyun BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
1048*4882a593Smuzhiyun HWEIGHT32(
1049*4882a593Smuzhiyun (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
1050*4882a593Smuzhiyun __FMODE_EXEC | __FMODE_NONOTIFY));
1051*4882a593Smuzhiyun
1052*4882a593Smuzhiyun fasync_cache = kmem_cache_create("fasync_cache",
1053*4882a593Smuzhiyun sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
1054*4882a593Smuzhiyun return 0;
1055*4882a593Smuzhiyun }
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun module_init(fcntl_init)
1058