Skip to content

Commit f671a69

Browse files
desmondcheongzxjtlayton
authored andcommitted
fcntl: fix potential deadlocks for &fown_struct.lock
Syzbot reports a potential deadlock in do_fcntl: ======================================================== WARNING: possible irq lock inversion dependency detected 5.12.0-syzkaller #0 Not tainted -------------------------------------------------------- syz-executor132/8391 just changed the state of lock: ffff888015967bf8 (&f->f_owner.lock){.+..}-{2:2}, at: f_getown_ex fs/fcntl.c:211 [inline] ffff888015967bf8 (&f->f_owner.lock){.+..}-{2:2}, at: do_fcntl+0x8b4/0x1200 fs/fcntl.c:395 but this lock was taken by another, HARDIRQ-safe lock in the past: (&dev->event_lock){-...}-{2:2} and interrupts could create inverse lock ordering between them. other info that might help us debug this: Chain exists of: &dev->event_lock --> &new->fa_lock --> &f->f_owner.lock Possible interrupt unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&f->f_owner.lock); local_irq_disable(); lock(&dev->event_lock); lock(&new->fa_lock); <Interrupt> lock(&dev->event_lock); *** DEADLOCK *** This happens because there is a lock hierarchy of &dev->event_lock --> &new->fa_lock --> &f->f_owner.lock from the following call chain: input_inject_event(): spin_lock_irqsave(&dev->event_lock,...); input_handle_event(): input_pass_values(): input_to_handler(): evdev_events(): evdev_pass_values(): spin_lock(&client->buffer_lock); __pass_event(): kill_fasync(): kill_fasync_rcu(): read_lock(&fa->fa_lock); send_sigio(): read_lock_irqsave(&fown->lock,...); However, since &dev->event_lock is HARDIRQ-safe, interrupts have to be disabled while grabbing &f->f_owner.lock, otherwise we invert the lock hierarchy. Hence, we replace calls to read_lock/read_unlock on &f->f_owner.lock, with read_lock_irq/read_unlock_irq. Reported-and-tested-by: syzbot+e6d5398a02c516ce5e70@syzkaller.appspotmail.com Signed-off-by: Desmond Cheong Zhi Xi <desmondcheongzx@gmail.com> Signed-off-by: Jeff Layton <jlayton@kernel.org>
1 parent 9ff50bf commit f671a69

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎fs/fcntl.c‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,16 @@ void f_delown(struct file *filp)
150150
pid_t f_getown(struct file *filp)
151151
{
152152
pid_t pid = 0;
153-
read_lock(&filp->f_owner.lock);
153+
154+
read_lock_irq(&filp->f_owner.lock);
154155
rcu_read_lock();
155156
if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type)) {
156157
pid = pid_vnr(filp->f_owner.pid);
157158
if (filp->f_owner.pid_type == PIDTYPE_PGID)
158159
pid = -pid;
159160
}
160161
rcu_read_unlock();
161-
read_unlock(&filp->f_owner.lock);
162+
read_unlock_irq(&filp->f_owner.lock);
162163
return pid;
163164
}
164165

@@ -208,7 +209,7 @@ static int f_getown_ex(struct file *filp, unsigned long arg)
208209
struct f_owner_ex owner = {};
209210
int ret = 0;
210211

211-
read_lock(&filp->f_owner.lock);
212+
read_lock_irq(&filp->f_owner.lock);
212213
rcu_read_lock();
213214
if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type))
214215
owner.pid = pid_vnr(filp->f_owner.pid);
@@ -231,7 +232,7 @@ static int f_getown_ex(struct file *filp, unsigned long arg)
231232
ret = -EINVAL;
232233
break;
233234
}
234-
read_unlock(&filp->f_owner.lock);
235+
read_unlock_irq(&filp->f_owner.lock);
235236

236237
if (!ret) {
237238
ret = copy_to_user(owner_p, &owner, sizeof(owner));
@@ -249,10 +250,10 @@ static int f_getowner_uids(struct file *filp, unsigned long arg)
249250
uid_t src[2];
250251
int err;
251252

252-
read_lock(&filp->f_owner.lock);
253+
read_lock_irq(&filp->f_owner.lock);
253254
src[0] = from_kuid(user_ns, filp->f_owner.uid);
254255
src[1] = from_kuid(user_ns, filp->f_owner.euid);
255-
read_unlock(&filp->f_owner.lock);
256+
read_unlock_irq(&filp->f_owner.lock);
256257

257258
err = put_user(src[0], &dst[0]);
258259
err |= put_user(src[1], &dst[1]);

0 commit comments

Comments
 (0)