Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp1385019lqb; Thu, 30 May 2024 08:40:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUDFHKmI74T4N5TkSfJI6tb3m7KGZQPVKWL0rEBjp9CGptThXeqm8/YsbdBhrOe4Nq0R+r8gfX6jpttkoQY9YbtKfhqsGIcNUQHgH41og== X-Google-Smtp-Source: AGHT+IFeDMQJDppRhjl5MtSTRBp/VEfYQyAt6AfkwpeEEwmB2oC+/3tAfBG9PcoxPj/s5V1J9xHD X-Received: by 2002:a05:6870:392b:b0:24f:e599:9182 with SMTP id 586e51a60fabf-25060bf142fmr3153658fac.26.1717083647326; Thu, 30 May 2024 08:40:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717083647; cv=pass; d=google.com; s=arc-20160816; b=0WUojS4hteVqHarAg2BIF0/6540v8PqMREbW1r8678ZMX5ph26aaQF6p7u0IWI5ucF Wibq41b6wsGXqXcQorWvWzGfsr/olyYfry1qYAbgunFstPzBQpUi/jGrgmxByBtq2oV/ tdYzP679Qo9xszu0e3xSbtGdoboK6+EdHNM3+IzpX9Z5bU+5CK/CaR100SILha8NXHD0 MAf16MEr4MHzgOlS/vWZ5xgurIRtx6d2pWYTEUkqvFjz76+NiZPdpMbo7P1NlPy4RnrJ SMrInNO/W32uOTbPXYKf9IljCbd/1MiC3CJfy2r/jibGCKT0+t6qYYOSmj00eKW8qd+F /N3w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date:dkim-signature; bh=8LXBcrfpv1f7hLYKdymWDBUZHaz6Isi/v87fgQX7qYA=; fh=2lCcX8hF6Tgcdz9dAjEAjG4IFqoEWzHVVFj2WeVRPcU=; b=Cwz5dqQZdjoa+WHRUHPESqOM6jgXcaWip/Y3rg4j6aUNWDs1x2A/ySt3XD1WNu62Oe vb41FLYyUuC9oLsPDm1sPMU9S/lMMCgZRYjxnoGEiI32exuV4Wgaq85v4X/ziSpgofX8 TnH9oUuKfPiXnDRBTRj0TMWaTdQU/tVvQO0Y0PqbXp/N/gCYNJRj2/rE51c5TnXEDqSh 7C/GLGBCuNPdJpOwjcYKZTMe0gWT+Uf79uT3+e+y/mcMWnZVDuuj2sy6YBYffk6lNVCX 2cr53V19nXaYSamXIl6xf/rdWQwYOq43moL1vMexihnuHhDPdEHbTYNSdhRLTInp/1Ag g8mw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZQgInN7Y; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-195609-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-195609-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id af79cd13be357-794abd3c071si21284085a.400.2024.05.30.08.40.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 May 2024 08:40:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-195609-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZQgInN7Y; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-195609-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-195609-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id DF2511C20336 for ; Thu, 30 May 2024 15:40:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E7AE1182D3F; Thu, 30 May 2024 15:40:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZQgInN7Y" 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 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.