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