Received: by 2002:a05:6520:4d:b0:139:a872:a4c9 with SMTP id i13csp2567134lkm; Mon, 20 Sep 2021 18:54:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxV0auuZg7q9ZQi4EN21oqFVFSCjSmOCmz0pwv5Ft1/HAcDBctWo3LOHCaR1x7uHniAgXA9 X-Received: by 2002:a92:bf0b:: with SMTP id z11mr18595856ilh.117.1632188879418; Mon, 20 Sep 2021 18:47:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632188879; cv=none; d=google.com; s=arc-20160816; b=QV/H0sZrRtq7Q0ur9UxLgRBMrb3neIBTModaToKbog+tF/VNQkRtwdRJtzdIf1M72z 5YJGFCz3bK4fiaDXMcbYwegd69X3OIRkerryYyrToPO7E0Cp1Jb0MZx++mD+bdwUkxMw EpG6xzR5K0LsgRE3fgP+JJRKpKIevcqCVVHkG2aTD4WlzsrY1BIC5bNfKYulaWs8uiZJ 9CzK1llBZJVR6GTbikYnc2DKcdMStZFP0hDe+fXw0+pUtO1/3zPsua3C1374PPlfL07G F5yi6kk6EkpVVwrzvLgqO8VxbfNo+x9/+vi5BZmjpjZxQqebRZKRMRHQLc3VdWQ5aKjo UKvA== 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=t3MqtjaGObGJ8ogk+3FyXb7yj0Vta0firNonk9jdhBM=; b=HFR2sV0MXpXcfHTAY3w62nfJBaVJKpYbNlOLsEoe/stQlLYXevvr43m6vjKLBtQpGQ JmRdEQd+dc4eworhKf4aa5yK0nIYmP8iuywMOJu9PAL1aYXoPcV6m79vaYXaOlylE6h6 3v1IbIYhHsgACGgrCqDP00reT5DFOHZj67SiWqOaGTFwrIaGqDeaUN6QetX2qnwI4vGQ gcZzL3wy9KB9w/u05MkpL8nE1jy3qYzETbhNoGrmSz0f3gSDVG5ix9NSxdF8V9cgyy5j FcaC4oWf8LdkBklblB8YRtH2wjI8FQPyjA2VBFpESAjOcPkox60H35lxQM97dnk5W3+w s4Bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=S94Zp+Xf; 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 n5si11795809iob.93.2021.09.20.18.47.48; Mon, 20 Sep 2021 18:47:59 -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=S94Zp+Xf; 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 S1348202AbhITRhh (ORCPT + 99 others); Mon, 20 Sep 2021 13:37:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:40198 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348089AbhITRfr (ORCPT ); Mon, 20 Sep 2021 13:35:47 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 05FF161B24; Mon, 20 Sep 2021 17:06:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632157572; bh=hrvckz5zgTtjPQZh4rLELH8lNg+zFQ0ABt3RKIeG+OM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S94Zp+XfY920NgHYHo9v57oHGrNVLlxLUe7qUjpIcTbKTZwngu7UOB9YbJ0B6y6Fr Gih9MiXbB9D+VlQoQ3y2zCDluRSq+yfpKgGrCEdy9CZRLwRtEm8GuTJCgwgHjvXbFn HqnDz/tEo0j2mKQku3GOHJtLN+kY29QtuJIFMCFI= 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 4.19 052/293] fcntl: fix potential deadlock for &fasync_struct.fa_lock Date: Mon, 20 Sep 2021 18:40:14 +0200 Message-Id: <20210920163935.041033688@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163933.258815435@linuxfoundation.org> References: <20210920163933.258815435@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 e039af1872ab..dffb5245ae72 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -993,13 +993,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 @@ -1008,7 +1009,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