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