Received: by 2002:a05:6a10:6d25:0:0:0:0 with SMTP id gq37csp1838713pxb; Mon, 13 Sep 2021 06:40:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdLjTJrSO1oBonHAekIqQaWsWMoF2vkt/7SNmursrsu0+wnfMaq6LScFNPUVptrMJ39hYz X-Received: by 2002:a02:6a2e:: with SMTP id l46mr9857641jac.73.1631540426586; Mon, 13 Sep 2021 06:40:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631540426; cv=none; d=google.com; s=arc-20160816; b=RibKN3/cNXpKSD3CWh9z586PMhYsceXGyhGf+cq7j1WWA4SYv3I2VMd7zrCqKnse0t AELeLbLd37WQcjeJQKpgs3Ao2/iURueK2SMCmBJT2yl5VZOEcU9YsHjA8fJTpx2Nyhgb 3rB1mnFzmaZps7debyezM/qu6VRXz5ISAkT4oGBJy9rA5eUYnz0sSR7ylQeW3WLOzc3t YSsqnHVGUGTTeJ7pvwwy/uJ7lxYGy1uaKmD/5XckNXsqs9AgzBseKoMLwwrUbXIDsqIE /VRYCo9PoD66x3gSXFXRY8s3ehMP43LOmg76+EgCu2ttthZAuRr43EnpokfeVBU+F7BP I/oQ== 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=CkQQprD1nOeJbBRixYn45cv98MEA+JGtFwuFM3Dtyn8=; b=TEm002arwRBEck9tM3h6wd7gB0BjrTC5wlFOrcFmuRqLXMDQy1np3dyhR+njUyzEd6 pbcsYRfuJnzZFZxLfFROnSxZuU7GUOOxYAnA9DGvPL1lbsJTn8J1F5hCnNFhFhsG3Ozs ryd+sEYvsBdWH3+jlK33eTakBQXbg83kL7o3Efthmv0xGppfMmuc0ehg5+UwI7zV/w3j Z1iUAQ/+OiDmsaUVyMETl+NKfG7eBc/YI1H/M90nk9rn1sskOGwUMgVgm77NhD7FjClF 5qjxHcTf8fNt/VjDenNyGz0nEl8g353b1gWoyMNAz62j64IDDNdQbhkTAR32vbyeP04c KMUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JaWB+qFI; 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 d27si6748974jaa.121.2021.09.13.06.40.14; Mon, 13 Sep 2021 06:40:26 -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=JaWB+qFI; 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 S235901AbhIMNiO (ORCPT + 99 others); Mon, 13 Sep 2021 09:38:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:58178 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242638AbhIMNcg (ORCPT ); Mon, 13 Sep 2021 09:32:36 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C3FB96137A; Mon, 13 Sep 2021 13:25:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631539544; bh=0VqOQdi3MJf18k+TNyXmI1bXyp7rMFeRpUZdG+erPOc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JaWB+qFIZQICEhl6MXcqfqvAc1fchKRVSD6lYHlupHzkqWTaapWMhZOWuD5R9dmut CB0EVyAdugfMy74VFH4/Bb+Jn4LuP84cKRbaSzt0s9ndMI6HZYd/InCLjcHOrVYSBB NK+AF2DX4+DPKrr/Bh/mNgqvhnxLMdXlvVLT+WLw= 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 Subject: [PATCH 5.10 033/236] fcntl: fix potential deadlock for &fasync_struct.fa_lock Date: Mon, 13 Sep 2021 15:12:18 +0200 Message-Id: <20210913131101.468055752@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131100.316353015@linuxfoundation.org> References: <20210913131100.316353015@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 2f488f698fda820f8e6fa0407630154eceb145d6 ] There is an existing lock hierarchy of &dev->event_lock --> &fasync_struct.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,...); &dev->event_lock is HARDIRQ-safe, so interrupts have to be disabled while grabbing &fasync_struct.fa_lock, otherwise we invert the lock hierarchy. However, since kill_fasync which calls kill_fasync_rcu is an exported symbol, it may not necessarily be called with interrupts disabled. As kill_fasync_rcu may be called with interrupts disabled (for example, in the call chain above), we replace calls to read_lock/read_unlock on &fasync_struct.fa_lock in kill_fasync_rcu with read_lock_irqsave/read_unlock_irqrestore. Signed-off-by: Desmond Cheong Zhi Xi Signed-off-by: Jeff Layton Signed-off-by: Sasha Levin --- fs/fcntl.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/fcntl.c b/fs/fcntl.c index 05b36b28f2e8..71b43538fa44 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -995,13 +995,14 @@ static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band) { while (fa) { struct fown_struct *fown; + unsigned long flags; if (fa->magic != FASYNC_MAGIC) { printk(KERN_ERR "kill_fasync: bad magic number in " "fasync_struct!\n"); return; } - read_lock(&fa->fa_lock); + read_lock_irqsave(&fa->fa_lock, flags); if (fa->fa_file) { fown = &fa->fa_file->f_owner; /* Don't send SIGURG to processes which have not set a @@ -1010,7 +1011,7 @@ static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band) if (!(sig == SIGURG && fown->signum == 0)) send_sigio(fown, fa->fa_fd, band); } - read_unlock(&fa->fa_lock); + read_unlock_irqrestore(&fa->fa_lock, flags); fa = rcu_dereference(fa->fa_next); } } -- 2.30.2