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