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