From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E62E5182D2D for ; Thu, 30 May 2024 15:40:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717083641; cv=none; b=AVey9OjNuh3h/d/ttt8pEMIRbbeQDz3krhxtf8gUcP1JuxK49ZXzm2mHUbrgmtHis31EjP7ep/nCyuvkPM6cWJLl9fdGqfYhTjNzlD/TaaRqFyud7rC8hR84gYE5qkB/hyzOMZrNJRTp5OkJHfLBMohlHKjH7nG1tvvmMnNSFas= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717083641; c=relaxed/simple; bh=RjrKb2LDRXMJMgv4262CU5d3QeX+oRtdcwDrIaVrcxo=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=Qbt5V/3B12P1MRtdwFBBzTCekIemetaU5IVeO/XJd7zDS+X0mRdYzImihcxOy9d4dmznccFnsV60ZEyjgRy+pCkKeMnsZnIj+ksZlHcZOIsuL7L/YB3Mih/mdskMG6tIUsxnfsprx5t//MztZq9N4E7lxEMpnnqio4Z6qzTXnXQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZQgInN7Y; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZQgInN7Y" 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) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII 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 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.