Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp107360pxb; Mon, 13 Sep 2021 14:17:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw5+hNIzL4+4QHyDaIgbe/CddIqaM/A3jOupYR092ajFcw1YRm5tMerezRLV5xzgZslOI/d X-Received: by 2002:a17:906:7d83:: with SMTP id v3mr14845295ejo.216.1631567872599; Mon, 13 Sep 2021 14:17:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631567872; cv=none; d=google.com; s=arc-20160816; b=pu0d86qxM9AN6EjHfkoEvU4Hpl2IvxHvaq5s5B6LCd87Lj78Jck1iTmoQeY5xhRByc LmA6qUuMzbvwr1weLuhSk+VdsJ3CQ6+io/DQMe2kQskHARfH4snFbe2Dq9TY//mCBW5B csXknfIWLJl0kKzpQAOdZFq2fBGAaSEzimddi7HL9BpgAaf/R6THWlFwL78o/uQUP1lg zXJExoh5fT2dUfm1c1GLCYhv1kt473LTKmz7VjtvcNT2qVC2cv4Wz91S+d/0EPpatPMo e/xniWnusGpCw2KZJ170TiAl3f+iPYVAdPvBnY+GcdV6E39p79OkDvHQaA67iZZGzHib Wa1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Oux2mrJc86bcloaZyN/5shitq0BQfpEJ4krn7+ZgydY=; b=nsn39YCSx1RCvapkakdaF/DBWVQE60PzRjLmJ8F0aq0R1ePoaeSyZSWZXQbH4DhYc1 VMF7qq6gDirAtVk7mLQ/iGCMi92SX1sOc/xblB2KGzK6UxY3QSwt33vuVa4AUtH+qC0n 9QqJpupSq7vJuaMVY7NqKaTU9p5pUO3RvStW2jEEfLkRs0UEvzYspKM4E/FeDvnUSPx9 KudRB75NW2l0CRoC69iF2JpI2LyD2A6aRgM2Ea6TQYu7rIZYq/Q9E/uH913N7pwdcxEF /WyPAdwYp2WAjwg6U5uLGzlQf/cJ8/B40YuClbJ5Q22kAfJ/Y06pDsy/Ud5TO/pj1KCk 6YjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="rZ/eeIAo"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id by9si8477753edb.468.2021.09.13.14.17.25; Mon, 13 Sep 2021 14:17:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="rZ/eeIAo"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245126AbhIMNz4 (ORCPT + 99 others); Mon, 13 Sep 2021 09:55:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:38570 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243171AbhIMNwp (ORCPT ); Mon, 13 Sep 2021 09:52:45 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 121806187F; Mon, 13 Sep 2021 13:34:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631540068; bh=7S0OkuFqMMm+SM26KoagE0507dIUIa/dKStSrGR9e08=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rZ/eeIAo9ZlppLUUoQe8CtJkWguqWtftcmUoSRSlgkaK09BEMgrTh1oSRQNj5x4kQ +cnNWyqpKKWxzpK/NoANC8ZOnfXS409XXP5p3HKDWuvHsqTmq7nZPfyJ1D3IRlso+u J2slhX0fIHdEmXDIcMoYaeibsN5qOit28wTzmg10= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Desmond Cheong Zhi Xi , Jeff Layton , Sasha Levin , syzbot+e6d5398a02c516ce5e70@syzkaller.appspotmail.com Subject: [PATCH 5.13 038/300] fcntl: fix potential deadlocks for &fown_struct.lock Date: Mon, 13 Sep 2021 15:11:39 +0200 Message-Id: <20210913131110.613196066@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131109.253835823@linuxfoundation.org> References: <20210913131109.253835823@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Desmond Cheong Zhi Xi [ Upstream commit f671a691e299f58835d4660d642582bf0e8f6fda ] 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); 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 Signed-off-by: Jeff Layton Signed-off-by: Sasha Levin --- fs/fcntl.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/fcntl.c b/fs/fcntl.c index dfc72f15be7f..cf9e81dfa615 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -150,7 +150,8 @@ void f_delown(struct file *filp) pid_t f_getown(struct file *filp) { pid_t pid = 0; - read_lock(&filp->f_owner.lock); + + read_lock_irq(&filp->f_owner.lock); rcu_read_lock(); if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type)) { pid = pid_vnr(filp->f_owner.pid); @@ -158,7 +159,7 @@ pid_t f_getown(struct file *filp) pid = -pid; } rcu_read_unlock(); - read_unlock(&filp->f_owner.lock); + read_unlock_irq(&filp->f_owner.lock); return pid; } @@ -208,7 +209,7 @@ static int f_getown_ex(struct file *filp, unsigned long arg) struct f_owner_ex owner = {}; int ret = 0; - read_lock(&filp->f_owner.lock); + read_lock_irq(&filp->f_owner.lock); rcu_read_lock(); if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type)) owner.pid = pid_vnr(filp->f_owner.pid); @@ -231,7 +232,7 @@ static int f_getown_ex(struct file *filp, unsigned long arg) ret = -EINVAL; break; } - read_unlock(&filp->f_owner.lock); + read_unlock_irq(&filp->f_owner.lock); if (!ret) { ret = copy_to_user(owner_p, &owner, sizeof(owner)); @@ -249,10 +250,10 @@ static int f_getowner_uids(struct file *filp, unsigned long arg) uid_t src[2]; int err; - read_lock(&filp->f_owner.lock); + read_lock_irq(&filp->f_owner.lock); src[0] = from_kuid(user_ns, filp->f_owner.uid); src[1] = from_kuid(user_ns, filp->f_owner.euid); - read_unlock(&filp->f_owner.lock); + read_unlock_irq(&filp->f_owner.lock); err = put_user(src[0], &dst[0]); err |= put_user(src[1], &dst[1]); -- 2.30.2