1
2
3
4
5
6
7#include <linux/syscalls.h>
8#include <linux/init.h>
9#include <linux/mm.h>
10#include <linux/fs.h>
11#include <linux/file.h>
12#include <linux/fdtable.h>
13#include <linux/capability.h>
14#include <linux/dnotify.h>
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/pipe_fs_i.h>
18#include <linux/security.h>
19#include <linux/ptrace.h>
20#include <linux/signal.h>
21#include <linux/rcupdate.h>
22#include <linux/pid_namespace.h>
23#include <linux/user_namespace.h>
24#include <linux/shmem_fs.h>
25
26#include <asm/poll.h>
27#include <asm/siginfo.h>
28#include <asm/uaccess.h>
29
30#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
31
32static int setfl(int fd, struct file * filp, unsigned long arg)
33{
34 struct inode * inode = file_inode(filp);
35 int error = 0;
36
37
38
39
40
41 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
42 return -EPERM;
43
44
45 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
46 if (!inode_owner_or_capable(inode))
47 return -EPERM;
48
49
50 if (O_NONBLOCK != O_NDELAY)
51 if (arg & O_NDELAY)
52 arg |= O_NONBLOCK;
53
54 if (arg & O_DIRECT) {
55 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
56 !filp->f_mapping->a_ops->direct_IO)
57 return -EINVAL;
58 }
59
60 if (filp->f_op && filp->f_op->check_flags)
61 error = filp->f_op->check_flags(arg);
62 if (error)
63 return error;
64
65
66
67
68 if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op &&
69 filp->f_op->fasync) {
70 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
71 if (error < 0)
72 goto out;
73 if (error > 0)
74 error = 0;
75 }
76 spin_lock(&filp->f_lock);
77 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
78 spin_unlock(&filp->f_lock);
79
80 out:
81 return error;
82}
83
84static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
85 int force)
86{
87 write_lock_irq(&filp->f_owner.lock);
88 if (force || !filp->f_owner.pid) {
89 put_pid(filp->f_owner.pid);
90 filp->f_owner.pid = get_pid(pid);
91 filp->f_owner.pid_type = type;
92
93 if (pid) {
94 const struct cred *cred = current_cred();
95 filp->f_owner.uid = cred->uid;
96 filp->f_owner.euid = cred->euid;
97 }
98 }
99 write_unlock_irq(&filp->f_owner.lock);
100}
101
102int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
103 int force)
104{
105 int err;
106
107 err = security_file_set_fowner(filp);
108 if (err)
109 return err;
110
111 f_modown(filp, pid, type, force);
112 return 0;
113}
114EXPORT_SYMBOL(__f_setown);
115
116int f_setown(struct file *filp, unsigned long arg, int force)
117{
118 enum pid_type type;
119 struct pid *pid;
120 int who = arg;
121 int result;
122 type = PIDTYPE_PID;
123 if (who < 0) {
124 type = PIDTYPE_PGID;
125 who = -who;
126 }
127 rcu_read_lock();
128 pid = find_vpid(who);
129 result = __f_setown(filp, pid, type, force);
130 rcu_read_unlock();
131 return result;
132}
133EXPORT_SYMBOL(f_setown);
134
135void f_delown(struct file *filp)
136{
137 f_modown(filp, NULL, PIDTYPE_PID, 1);
138}
139
140pid_t f_getown(struct file *filp)
141{
142 pid_t pid;
143 read_lock(&filp->f_owner.lock);
144 pid = pid_vnr(filp->f_owner.pid);
145 if (filp->f_owner.pid_type == PIDTYPE_PGID)
146 pid = -pid;
147 read_unlock(&filp->f_owner.lock);
148 return pid;
149}
150
151static int f_setown_ex(struct file *filp, unsigned long arg)
152{
153 struct f_owner_ex __user *owner_p = (void __user *)arg;
154 struct f_owner_ex owner;
155 struct pid *pid;
156 int type;
157 int ret;
158
159 ret = copy_from_user(&owner, owner_p, sizeof(owner));
160 if (ret)
161 return -EFAULT;
162
163 switch (owner.type) {
164 case F_OWNER_TID:
165 type = PIDTYPE_MAX;
166 break;
167
168 case F_OWNER_PID:
169 type = PIDTYPE_PID;
170 break;
171
172 case F_OWNER_PGRP:
173 type = PIDTYPE_PGID;
174 break;
175
176 default:
177 return -EINVAL;
178 }
179
180 rcu_read_lock();
181 pid = find_vpid(owner.pid);
182 if (owner.pid && !pid)
183 ret = -ESRCH;
184 else
185 ret = __f_setown(filp, pid, type, 1);
186 rcu_read_unlock();
187
188 return ret;
189}
190
191static int f_getown_ex(struct file *filp, unsigned long arg)
192{
193 struct f_owner_ex __user *owner_p = (void __user *)arg;
194 struct f_owner_ex owner;
195 int ret = 0;
196
197 read_lock(&filp->f_owner.lock);
198 owner.pid = pid_vnr(filp->f_owner.pid);
199 switch (filp->f_owner.pid_type) {
200 case PIDTYPE_MAX:
201 owner.type = F_OWNER_TID;
202 break;
203
204 case PIDTYPE_PID:
205 owner.type = F_OWNER_PID;
206 break;
207
208 case PIDTYPE_PGID:
209 owner.type = F_OWNER_PGRP;
210 break;
211
212 default:
213 WARN_ON(1);
214 ret = -EINVAL;
215 break;
216 }
217 read_unlock(&filp->f_owner.lock);
218
219 if (!ret) {
220 ret = copy_to_user(owner_p, &owner, sizeof(owner));
221 if (ret)
222 ret = -EFAULT;
223 }
224 return ret;
225}
226
227#ifdef CONFIG_CHECKPOINT_RESTORE
228static int f_getowner_uids(struct file *filp, unsigned long arg)
229{
230 struct user_namespace *user_ns = current_user_ns();
231 uid_t __user *dst = (void __user *)arg;
232 uid_t src[2];
233 int err;
234
235 read_lock(&filp->f_owner.lock);
236 src[0] = from_kuid(user_ns, filp->f_owner.uid);
237 src[1] = from_kuid(user_ns, filp->f_owner.euid);
238 read_unlock(&filp->f_owner.lock);
239
240 err = put_user(src[0], &dst[0]);
241 err |= put_user(src[1], &dst[1]);
242
243 return err;
244}
245#else
246static int f_getowner_uids(struct file *filp, unsigned long arg)
247{
248 return -EINVAL;
249}
250#endif
251
252static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
253 struct file *filp)
254{
255 void __user *argp = (void __user *)arg;
256 struct flock flock;
257 long err = -EINVAL;
258
259 switch (cmd) {
260 case F_DUPFD:
261 err = f_dupfd(arg, filp, 0);
262 break;
263 case F_DUPFD_CLOEXEC:
264 err = f_dupfd(arg, filp, O_CLOEXEC);
265 break;
266 case F_GETFD:
267 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
268 break;
269 case F_SETFD:
270 err = 0;
271 set_close_on_exec(fd, arg & FD_CLOEXEC);
272 break;
273 case F_GETFL:
274 err = filp->f_flags;
275 break;
276 case F_SETFL:
277 err = setfl(fd, filp, arg);
278 break;
279#if BITS_PER_LONG != 32
280
281 case F_OFD_GETLK:
282#endif
283 case F_GETLK:
284 if (copy_from_user(&flock, argp, sizeof(flock)))
285 return -EFAULT;
286 err = fcntl_getlk(filp, cmd, &flock);
287 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
288 return -EFAULT;
289 break;
290#if BITS_PER_LONG != 32
291
292 case F_OFD_SETLK:
293 case F_OFD_SETLKW:
294#endif
295
296 case F_SETLK:
297 case F_SETLKW:
298 if (copy_from_user(&flock, argp, sizeof(flock)))
299 return -EFAULT;
300 err = fcntl_setlk(fd, filp, cmd, &flock);
301 break;
302 case F_GETOWN:
303
304
305
306
307
308
309
310 err = f_getown(filp);
311 force_successful_syscall_return();
312 break;
313 case F_SETOWN:
314 err = f_setown(filp, arg, 1);
315 break;
316 case F_GETOWN_EX:
317 err = f_getown_ex(filp, arg);
318 break;
319 case F_SETOWN_EX:
320 err = f_setown_ex(filp, arg);
321 break;
322 case F_GETOWNER_UIDS:
323 err = f_getowner_uids(filp, arg);
324 break;
325 case F_GETSIG:
326 err = filp->f_owner.signum;
327 break;
328 case F_SETSIG:
329
330 if (!valid_signal(arg)) {
331 break;
332 }
333 err = 0;
334 filp->f_owner.signum = arg;
335 break;
336 case F_GETLEASE:
337 err = fcntl_getlease(filp);
338 break;
339 case F_SETLEASE:
340 err = fcntl_setlease(fd, filp, arg);
341 break;
342 case F_NOTIFY:
343 err = fcntl_dirnotify(fd, filp, arg);
344 break;
345 case F_SETPIPE_SZ:
346 case F_GETPIPE_SZ:
347 err = pipe_fcntl(filp, cmd, arg);
348 break;
349 case F_ADD_SEALS:
350 case F_GET_SEALS:
351 err = shmem_fcntl(filp, cmd, arg);
352 break;
353 default:
354 break;
355 }
356 return err;
357}
358
359static int check_fcntl_cmd(unsigned cmd)
360{
361 switch (cmd) {
362 case F_DUPFD:
363 case F_DUPFD_CLOEXEC:
364 case F_GETFD:
365 case F_SETFD:
366 case F_GETFL:
367 return 1;
368 }
369 return 0;
370}
371
372SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
373{
374 struct fd f = fdget_raw(fd);
375 long err = -EBADF;
376
377 if (!f.file)
378 goto out;
379
380 if (unlikely(f.file->f_mode & FMODE_PATH)) {
381 if (!check_fcntl_cmd(cmd))
382 goto out1;
383 }
384
385 err = security_file_fcntl(f.file, cmd, arg);
386 if (!err)
387 err = do_fcntl(fd, cmd, arg, f.file);
388
389out1:
390 fdput(f);
391out:
392 return err;
393}
394
395#if BITS_PER_LONG == 32
396SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
397 unsigned long, arg)
398{
399 void __user *argp = (void __user *)arg;
400 struct fd f = fdget_raw(fd);
401 struct flock64 flock;
402 long err = -EBADF;
403
404 if (!f.file)
405 goto out;
406
407 if (unlikely(f.file->f_mode & FMODE_PATH)) {
408 if (!check_fcntl_cmd(cmd))
409 goto out1;
410 }
411
412 err = security_file_fcntl(f.file, cmd, arg);
413 if (err)
414 goto out1;
415
416 switch (cmd) {
417 case F_GETLK64:
418 case F_OFD_GETLK:
419 err = -EFAULT;
420 if (copy_from_user(&flock, argp, sizeof(flock)))
421 break;
422 err = fcntl_getlk64(f.file, cmd, &flock);
423 if (!err && copy_to_user(argp, &flock, sizeof(flock)))
424 err = -EFAULT;
425 break;
426 case F_SETLK64:
427 case F_SETLKW64:
428 case F_OFD_SETLK:
429 case F_OFD_SETLKW:
430 err = -EFAULT;
431 if (copy_from_user(&flock, argp, sizeof(flock)))
432 break;
433 err = fcntl_setlk64(fd, f.file, cmd, &flock);
434 break;
435 default:
436 err = do_fcntl(fd, cmd, arg, f.file);
437 break;
438 }
439out1:
440 fdput(f);
441out:
442 return err;
443}
444#endif
445
446
447
448static const long band_table[NSIGPOLL] = {
449 POLLIN | POLLRDNORM,
450 POLLOUT | POLLWRNORM | POLLWRBAND,
451 POLLIN | POLLRDNORM | POLLMSG,
452 POLLERR,
453 POLLPRI | POLLRDBAND,
454 POLLHUP | POLLERR
455};
456
457static inline int sigio_perm(struct task_struct *p,
458 struct fown_struct *fown, int sig)
459{
460 const struct cred *cred;
461 int ret;
462
463 rcu_read_lock();
464 cred = __task_cred(p);
465 ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
466 uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
467 uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
468 !security_file_send_sigiotask(p, fown, sig));
469 rcu_read_unlock();
470 return ret;
471}
472
473static void send_sigio_to_task(struct task_struct *p,
474 struct fown_struct *fown,
475 int fd, int reason, int group)
476{
477
478
479
480
481 int signum = ACCESS_ONCE(fown->signum);
482
483 if (!sigio_perm(p, fown, signum))
484 return;
485
486 switch (signum) {
487 siginfo_t si;
488 default:
489
490
491
492
493
494
495 si.si_signo = signum;
496 si.si_errno = 0;
497 si.si_code = reason;
498
499
500
501 BUG_ON((reason & __SI_MASK) != __SI_POLL);
502 if (reason - POLL_IN >= NSIGPOLL)
503 si.si_band = ~0L;
504 else
505 si.si_band = band_table[reason - POLL_IN];
506 si.si_fd = fd;
507 if (!do_send_sig_info(signum, &si, p, group))
508 break;
509
510 case 0:
511 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
512 }
513}
514
515void send_sigio(struct fown_struct *fown, int fd, int band)
516{
517 struct task_struct *p;
518 enum pid_type type;
519 struct pid *pid;
520 int group = 1;
521
522 read_lock(&fown->lock);
523
524 type = fown->pid_type;
525 if (type == PIDTYPE_MAX) {
526 group = 0;
527 type = PIDTYPE_PID;
528 }
529
530 pid = fown->pid;
531 if (!pid)
532 goto out_unlock_fown;
533
534 qread_lock(&tasklist_lock);
535 do_each_pid_task(pid, type, p) {
536 send_sigio_to_task(p, fown, fd, band, group);
537 } while_each_pid_task(pid, type, p);
538 qread_unlock(&tasklist_lock);
539 out_unlock_fown:
540 read_unlock(&fown->lock);
541}
542
543static void send_sigurg_to_task(struct task_struct *p,
544 struct fown_struct *fown, int group)
545{
546 if (sigio_perm(p, fown, SIGURG))
547 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
548}
549
550int send_sigurg(struct fown_struct *fown)
551{
552 struct task_struct *p;
553 enum pid_type type;
554 struct pid *pid;
555 int group = 1;
556 int ret = 0;
557
558 read_lock(&fown->lock);
559
560 type = fown->pid_type;
561 if (type == PIDTYPE_MAX) {
562 group = 0;
563 type = PIDTYPE_PID;
564 }
565
566 pid = fown->pid;
567 if (!pid)
568 goto out_unlock_fown;
569
570 ret = 1;
571
572 qread_lock(&tasklist_lock);
573 do_each_pid_task(pid, type, p) {
574 send_sigurg_to_task(p, fown, group);
575 } while_each_pid_task(pid, type, p);
576 qread_unlock(&tasklist_lock);
577 out_unlock_fown:
578 read_unlock(&fown->lock);
579 return ret;
580}
581
582static DEFINE_SPINLOCK(fasync_lock);
583static struct kmem_cache *fasync_cache __read_mostly;
584
585static void fasync_free_rcu(struct rcu_head *head)
586{
587 kmem_cache_free(fasync_cache,
588 container_of(head, struct fasync_struct, fa_rcu));
589}
590
591
592
593
594
595
596
597
598
599
600int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
601{
602 struct fasync_struct *fa, **fp;
603 int result = 0;
604
605 spin_lock(&filp->f_lock);
606 spin_lock(&fasync_lock);
607 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
608 if (fa->fa_file != filp)
609 continue;
610
611 spin_lock_irq(&fa->fa_lock);
612 fa->fa_file = NULL;
613 spin_unlock_irq(&fa->fa_lock);
614
615 *fp = fa->fa_next;
616 call_rcu(&fa->fa_rcu, fasync_free_rcu);
617 filp->f_flags &= ~FASYNC;
618 result = 1;
619 break;
620 }
621 spin_unlock(&fasync_lock);
622 spin_unlock(&filp->f_lock);
623 return result;
624}
625
626struct fasync_struct *fasync_alloc(void)
627{
628 return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
629}
630
631
632
633
634
635
636void fasync_free(struct fasync_struct *new)
637{
638 kmem_cache_free(fasync_cache, new);
639}
640
641
642
643
644
645
646
647
648struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
649{
650 struct fasync_struct *fa, **fp;
651
652 spin_lock(&filp->f_lock);
653 spin_lock(&fasync_lock);
654 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
655 if (fa->fa_file != filp)
656 continue;
657
658 spin_lock_irq(&fa->fa_lock);
659 fa->fa_fd = fd;
660 spin_unlock_irq(&fa->fa_lock);
661 goto out;
662 }
663
664 spin_lock_init(&new->fa_lock);
665 new->magic = FASYNC_MAGIC;
666 new->fa_file = filp;
667 new->fa_fd = fd;
668 new->fa_next = *fapp;
669 rcu_assign_pointer(*fapp, new);
670 filp->f_flags |= FASYNC;
671
672out:
673 spin_unlock(&fasync_lock);
674 spin_unlock(&filp->f_lock);
675 return fa;
676}
677
678
679
680
681
682static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
683{
684 struct fasync_struct *new;
685
686 new = fasync_alloc();
687 if (!new)
688 return -ENOMEM;
689
690
691
692
693
694
695
696
697 if (fasync_insert_entry(fd, filp, fapp, new)) {
698 fasync_free(new);
699 return 0;
700 }
701
702 return 1;
703}
704
705
706
707
708
709
710
711int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
712{
713 if (!on)
714 return fasync_remove_entry(filp, fapp);
715 return fasync_add_entry(fd, filp, fapp);
716}
717
718EXPORT_SYMBOL(fasync_helper);
719
720
721
722
723static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
724{
725 while (fa) {
726 struct fown_struct *fown;
727 unsigned long flags;
728
729 if (fa->magic != FASYNC_MAGIC) {
730 printk(KERN_ERR "kill_fasync: bad magic number in "
731 "fasync_struct!\n");
732 return;
733 }
734 spin_lock_irqsave(&fa->fa_lock, flags);
735 if (fa->fa_file) {
736 fown = &fa->fa_file->f_owner;
737
738
739
740 if (!(sig == SIGURG && fown->signum == 0))
741 send_sigio(fown, fa->fa_fd, band);
742 }
743 spin_unlock_irqrestore(&fa->fa_lock, flags);
744 fa = rcu_dereference(fa->fa_next);
745 }
746}
747
748void kill_fasync(struct fasync_struct **fp, int sig, int band)
749{
750
751
752
753 if (*fp) {
754 rcu_read_lock();
755 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
756 rcu_read_unlock();
757 }
758}
759EXPORT_SYMBOL(kill_fasync);
760
761static int __init fcntl_init(void)
762{
763
764
765
766
767
768 BUILD_BUG_ON(19 - 1 != HWEIGHT32(
769 O_RDONLY | O_WRONLY | O_RDWR |
770 O_CREAT | O_EXCL | O_NOCTTY |
771 O_TRUNC | O_APPEND |
772 __O_SYNC | O_DSYNC | FASYNC |
773 O_DIRECT | O_LARGEFILE | O_DIRECTORY |
774 O_NOFOLLOW | O_NOATIME | O_CLOEXEC |
775 __FMODE_EXEC | O_PATH
776 ));
777
778 fasync_cache = kmem_cache_create("fasync_cache",
779 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
780 return 0;
781}
782
783module_init(fcntl_init)
784