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