From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6510FC25B74 for ; Thu, 30 May 2024 15:41:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Subject:Cc:To:From:Message-ID:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=V0tMFP7MkWaFECYEfTW2UYbxiCf3WWlDzVxYQTI3hLU=; b=HxM73WyzygmiSa a5qmlNyINYpp6RtZUDkV4UDurO/WNIEBncdL9F6kZuGeF7K20eU53iR2VbzbI3PLAsT9p+8TTlAnQ QpFsijSbOovAMHzaZvFi0xYNvOd3oVaxJZaDPSO2FNedk2LcjJSJxDeTTkxq5kO4NzJs3JnuDIfD1 XA5OtAMQIcg/2H8fFY3lH67pkROu4SMsaYZMvkKS8DkcdtX7cbbzZnTmoffeFFnIX0tLVnb9gUkKe JHNfE3M2ny+6kDNavxCLPixk6Gwu5GgYqlb8kcWB7vVmAStn17DxQbdOLTF/IjEfnQ5A/wXADqs5X 16aERUZW+z0/rgapkdjQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sChtY-00000007kG0-2Y4J; Thu, 30 May 2024 15:40:48 +0000 Received: from sin.source.kernel.org ([145.40.73.55]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sChtV-00000007kFW-0BKP for linux-arm-kernel@lists.infradead.org; Thu, 30 May 2024 15:40:46 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 1FA62CE1A99; Thu, 30 May 2024 15:40:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 763B1C2BBFC; Thu, 30 May 2024 15:40:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717083640; bh=RjrKb2LDRXMJMgv4262CU5d3QeX+oRtdcwDrIaVrcxo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=ZQgInN7Yz59LtyGiZYk29Qv6j1Q9Pid0Y5m3R7Q3jTlFg8wQmOz6akpfib+F2kn41 v4YrQCsb/qGrevlwDfFA0Ru7mJT3n1Y2vJmQvw1KPpHqXUYTQg/KH58LjV2zBkirBF I0wWSM3iYV1hUB0fgs63nr18fnZv3ktQtCGZ8LjCAUx0nIusBaZz5fT8PGHmvmdifI kBAUZpIxiTYwyOVzlp15kK1q+5nVmZvCbiXsNrKG2I7q1Nuu9ohsxQne89HssDbJxU SJZI3WPI5yUKPOapLXwqB12MwtkIRf69PvlUO4ECbwUhc2bIRNb92BZiW8CskyHGEJ VbC2HY/59OMDg== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1sChtO-00Gw2M-2x; Thu, 30 May 2024 16:40:38 +0100 Date: Thu, 30 May 2024 16:40:37 +0100 Message-ID: <86v82vl34a.wl-maz@kernel.org> From: Marc Zyngier To: Hagar Hemdan Cc: Maximilian Heyne , Norbert Manthey , "Thomas\ Gleixner" , Eric Auger , , Subject: Re: [PATCH] irqchip/gic-v3-its: Fix potential race condition in its_vlpi_prop_update() In-Reply-To: <20240530105713.18552-1-hagarhem@amazon.com> References: <20240530105713.18552-1-hagarhem@amazon.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/29.2 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: hagarhem@amazon.com, mheyne@amazon.de, nmanthey@amazon.de, tglx@linutronix.de, eric.auger@redhat.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240530_084045_549802_06648B44 X-CRM114-Status: GOOD ( 31.65 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Hagar, On Thu, 30 May 2024 11:57:13 +0100, Hagar Hemdan wrote: > > Similar to commit 046b5054f566 ("irqchip/gic-v3-its: Lock VLPI map array > before translating it"), its_vlpi_prop_update() calls lpi_write_config() > which obtains the mapping information for a VLPI. > This should always be done with vlpi_lock for this device held. Otherwise, > its_vlpi_prop_update() could race with its_vlpi_unmap(). Thanks for reporting this. Note that this issue is not the same as the one you refer to (what you have here is a total absence of locking, while 046b5054f566 fixed a call to get_vlpi_map() outside of an existing critical section). > > This bug was discovered and resolved using Coverity Static Analysis > Security Testing (SAST) by Synopsys, Inc. Should we get a scrolling banner for this kind of advertisements? ;-) > > Fixes: 015ec0386ab6 ("irqchip/gic-v3-its: Add VLPI configuration handling") > Signed-off-by: Hagar Hemdan > --- > drivers/irqchip/irq-gic-v3-its.c | 12 +++++++++--- > 1 file changed, 9 insertions(+), 3 deletions(-) > > diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c > index 40ebf1726393..ecaad1786345 100644 > --- a/drivers/irqchip/irq-gic-v3-its.c > +++ b/drivers/irqchip/irq-gic-v3-its.c > @@ -1970,9 +1970,13 @@ static int its_vlpi_unmap(struct irq_data *d) > static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) > { > struct its_device *its_dev = irq_data_get_irq_chip_data(d); > + int ret = 0; > > - if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) > - return -EINVAL; > + raw_spin_lock(&its_dev->event_map.vlpi_lock); > + if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) { > + ret = -EINVAL; > + goto out; > + } > > if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI) > lpi_update_config(d, 0xff, info->config); > @@ -1980,7 +1984,9 @@ static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) > lpi_write_config(d, 0xff, info->config); > its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED)); > > - return 0; > +out: > + raw_spin_unlock(&its_dev->event_map.vlpi_lock); > + return ret; > } > > static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) As it turns out, all the calls from its_irq_set_vcpu_affinity() require the same lock to be held. So instead of peppering the locking all over the place, we could (should?) hoist the locking into its_irq_set_vcpu_affinity() and avoid future bugs. It also results in a negative diffstat. Something like the hack below (compile-tested only), which I'm sure the "Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc" will be able to verify... M. diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c index 40ebf1726393..abc1fb360ce4 100644 --- a/drivers/irqchip/irq-gic-v3-its.c +++ b/drivers/irqchip/irq-gic-v3-its.c @@ -1851,23 +1851,18 @@ static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) if (!info->map) return -EINVAL; - raw_spin_lock(&its_dev->event_map.vlpi_lock); - if (!its_dev->event_map.vm) { struct its_vlpi_map *maps; maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps), GFP_ATOMIC); - if (!maps) { - ret = -ENOMEM; - goto out; - } + if (!maps) + return -ENOMEM; its_dev->event_map.vm = info->map->vm; its_dev->event_map.vlpi_maps = maps; } else if (its_dev->event_map.vm != info->map->vm) { - ret = -EINVAL; - goto out; + return -EINVAL; } /* Get our private copy of the mapping information */ @@ -1899,8 +1894,6 @@ static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) its_dev->event_map.nr_vlpis++; } -out: - raw_spin_unlock(&its_dev->event_map.vlpi_lock); return ret; } @@ -1910,20 +1903,14 @@ static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info) struct its_vlpi_map *map; int ret = 0; - raw_spin_lock(&its_dev->event_map.vlpi_lock); - map = get_vlpi_map(d); - if (!its_dev->event_map.vm || !map) { - ret = -EINVAL; - goto out; - } + if (!its_dev->event_map.vm || !map) + return -EINVAL; /* Copy our mapping information to the incoming request */ *info->map = *map; -out: - raw_spin_unlock(&its_dev->event_map.vlpi_lock); return ret; } @@ -1933,12 +1920,8 @@ static int its_vlpi_unmap(struct irq_data *d) u32 event = its_get_event_id(d); int ret = 0; - raw_spin_lock(&its_dev->event_map.vlpi_lock); - - if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) { - ret = -EINVAL; - goto out; - } + if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) + return -EINVAL; /* Drop the virtual mapping */ its_send_discard(its_dev, event); @@ -1962,8 +1945,6 @@ static int its_vlpi_unmap(struct irq_data *d) kfree(its_dev->event_map.vlpi_maps); } -out: - raw_spin_unlock(&its_dev->event_map.vlpi_lock); return ret; } @@ -1987,29 +1968,41 @@ static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) { struct its_device *its_dev = irq_data_get_irq_chip_data(d); struct its_cmd_info *info = vcpu_info; + int ret; /* Need a v4 ITS */ if (!is_v4(its_dev->its)) return -EINVAL; + raw_spin_lock(&its_dev->event_map.vlpi_lock); + /* Unmap request? */ - if (!info) - return its_vlpi_unmap(d); + if (!info) { + ret = its_vlpi_unmap(d); + goto out; + } switch (info->cmd_type) { case MAP_VLPI: - return its_vlpi_map(d, info); + ret = its_vlpi_map(d, info); + break; case GET_VLPI: - return its_vlpi_get(d, info); + ret = its_vlpi_get(d, info); + break; case PROP_UPDATE_VLPI: case PROP_UPDATE_AND_INV_VLPI: - return its_vlpi_prop_update(d, info); + ret = its_vlpi_prop_update(d, info); + break; default: - return -EINVAL; + ret = -EINVAL; } + +out: + raw_spin_unlock(&its_dev->event_map.vlpi_lock); + return ret; } static struct irq_chip its_irq_chip = { -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel