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 21F4CC25B75 for ; Fri, 31 May 2024 16:22:24 +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:Message-ID:Date:Subject:CC :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=ztdzPrcxfbbZT2Kmp67cVySzxmWVAJAtrvQhB0GnNlA=; b=gXs9S0mkhkTDUi nz7vlR/nfK2FPHGlN09LsLDLJe5HhFIROaOWbF0tyATT1/EOskaKOS6RfnqkYhBaiZchJ0aGwGKbV r/5d+8Mr2PbzCasn2H/1aMYvqjJoWQZZGAx65d3TVWY+Uu3tUKp5tIXjlmcGhpGUj2+QCv+JLSdzO YHgqgThsVI6sN7hXPK2W6Yi5YaLMQ7jsm3wmLuHY3XQ7MqVD10vrdq1cH87d7/TRWVdQiF1pjAd3o 5Pvkhc5fJYMdhlDzm9ROThjxr+r/6zKBkiButzaIrwmyD0KgVnOH2jamyMVA8GcpRujet1y8yZplv T5/KXS6/wS31Ipb0PITw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sD51E-0000000Ap6R-02Ey; Fri, 31 May 2024 16:22:16 +0000 Received: from smtp-fw-52005.amazon.com ([52.119.213.156]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sD51A-0000000Ap5c-1vQU for linux-arm-kernel@lists.infradead.org; Fri, 31 May 2024 16:22:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1717172533; x=1748708533; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=Kkjdm7vLzgsvxwTIl6a+cQaY/194/UYr9RfyTUcw400=; b=Vxu7i7Wywy1A5JJkDa4ZrTYJTGgJ8K3fEeEleot5qo2TXG+1jeNoqKnW Tmqr3nuYfj1+05JXtaqCiZfHulybV4lzqxTlIro3L+LrRI17jtVPw1vBP d8VlmAhladJ6t1gRzf/leI4hx+AKxF8sKaSbCyvNLTYfBIDZx77W0InMX s=; X-IronPort-AV: E=Sophos;i="6.08,204,1712620800"; d="scan'208";a="657753334" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 16:22:04 +0000 Received: from EX19MTAEUB001.ant.amazon.com [10.0.10.100:27507] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.8.93:2525] with esmtp (Farcaster) id 61aea3b8-f33e-485c-8066-25d0973a7946; Fri, 31 May 2024 16:22:02 +0000 (UTC) X-Farcaster-Flow-ID: 61aea3b8-f33e-485c-8066-25d0973a7946 Received: from EX19D002EUA001.ant.amazon.com (10.252.50.66) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Fri, 31 May 2024 16:22:02 +0000 Received: from EX19MTAUWA001.ant.amazon.com (10.250.64.204) by EX19D002EUA001.ant.amazon.com (10.252.50.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Fri, 31 May 2024 16:22:01 +0000 Received: from dev-dsk-hagarhem-1b-b868d8d5.eu-west-1.amazon.com (10.253.65.58) by mail-relay.amazon.com (10.250.64.204) with Microsoft SMTP Server id 15.2.1258.28 via Frontend Transport; Fri, 31 May 2024 16:22:01 +0000 Received: by dev-dsk-hagarhem-1b-b868d8d5.eu-west-1.amazon.com (Postfix, from userid 23002382) id EA07320BED; Fri, 31 May 2024 16:22:00 +0000 (UTC) From: Hagar Hemdan To: CC: Maximilian Heyne , Norbert Manthey , Hagar Hemdan , Marc Zyngier , "Thomas Gleixner" , Eric Auger , , Subject: [PATCH v4] irqchip/gic-v3-its: Fix potential race condition in its_vlpi_prop_update() Date: Fri, 31 May 2024 16:21:44 +0000 Message-ID: <20240531162144.28650-1-hagarhem@amazon.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240531_092212_697449_28625D57 X-CRM114-Status: GOOD ( 17.29 ) 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 its_vlpi_prop_update() calls lpi_write_config() which obtains the mapping information for a VLPI without lock held. So it could race with its_vlpi_unmap(). Since all 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 hoist the locking into its_irq_set_vcpu_affinity(). This bug was discovered using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc. Fixes: 015ec0386ab6 ("irqchip/gic-v3-its: Add VLPI configuration handling") Suggested-by: Marc Zyngier Signed-off-by: Hagar Hemdan --- v4: modified the commit msg. Only compile-tested, no access to HW. --- drivers/irqchip/irq-gic-v3-its.c | 65 +++++++++++++------------------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c index 40ebf1726393..f9e824ad1523 100644 --- a/drivers/irqchip/irq-gic-v3-its.c +++ b/drivers/irqchip/irq-gic-v3-its.c @@ -1846,28 +1846,22 @@ static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) { struct its_device *its_dev = irq_data_get_irq_chip_data(d); u32 event = its_get_event_id(d); - int ret = 0; 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,46 +1893,32 @@ 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; + return 0; } static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info) { struct its_device *its_dev = irq_data_get_irq_chip_data(d); 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; + return 0; } static int its_vlpi_unmap(struct irq_data *d) { struct its_device *its_dev = irq_data_get_irq_chip_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,9 +1942,7 @@ 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; + return 0; } static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) @@ -1987,29 +1965,40 @@ 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 = { -- 2.40.1 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) (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 4A1ED158D78 for ; Fri, 31 May 2024 16:22:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.156 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717172527; cv=none; b=cQrzwZrpMWbtpPKHYvGr0+Y1RHaOl3L0b7g13/YTsYUHoh/BVJLRtMPHCXJ6Jwjl24efyO9tm5eaB8iCa1g+vKXBZ68mvd6A+17Qqzivj/SaRBBHTt4QGgM+4zG9rg5c/BqkEItWioRyFz+FxGu8OxNnEmjxQsDfeQ+UIRGkXFI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717172527; c=relaxed/simple; bh=mkb/nVcUv3NFAWt/4Kf8HdacRVGvAVCG1SNRZH1Zy8E=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=ZkPH+AVTfQ1r6OldoEDODdL5nxPQoZXfXr4FeuKyyKV2XyrejjMHuJOdARSabYRs8D6sh/VjbhD6zuclF4C5Tcb2Sw+RGGZN/sL+JsSsX/Q+zSbVyGhdUQomEsd3HeT2TWd0odZaPyWhGZtRSXlTnMtYc+u+sIba345jezT1bCU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=R3yoT+pG; arc=none smtp.client-ip=52.119.213.156 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="R3yoT+pG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1717172527; x=1748708527; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=Kkjdm7vLzgsvxwTIl6a+cQaY/194/UYr9RfyTUcw400=; b=R3yoT+pGSPNzWSY1vjdBBl53E0gvZbg+mXEUSHk0WUUEPVF67rc00LqG S7MFf987nH1K/u5ZOns6T9Ve1fEyWWQCcYR97JIlt8yuQjOXBPQFUsE6h Aripyz56m2lkpD7zYM8An15I7TdJji1WpSAXZ5/zKGF6uk5snXyQkVcsB 0=; X-IronPort-AV: E=Sophos;i="6.08,204,1712620800"; d="scan'208";a="657753334" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 16:22:04 +0000 Received: from EX19MTAEUB001.ant.amazon.com [10.0.10.100:27507] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.8.93:2525] with esmtp (Farcaster) id 61aea3b8-f33e-485c-8066-25d0973a7946; Fri, 31 May 2024 16:22:02 +0000 (UTC) X-Farcaster-Flow-ID: 61aea3b8-f33e-485c-8066-25d0973a7946 Received: from EX19D002EUA001.ant.amazon.com (10.252.50.66) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Fri, 31 May 2024 16:22:02 +0000 Received: from EX19MTAUWA001.ant.amazon.com (10.250.64.204) by EX19D002EUA001.ant.amazon.com (10.252.50.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Fri, 31 May 2024 16:22:01 +0000 Received: from dev-dsk-hagarhem-1b-b868d8d5.eu-west-1.amazon.com (10.253.65.58) by mail-relay.amazon.com (10.250.64.204) with Microsoft SMTP Server id 15.2.1258.28 via Frontend Transport; Fri, 31 May 2024 16:22:01 +0000 Received: by dev-dsk-hagarhem-1b-b868d8d5.eu-west-1.amazon.com (Postfix, from userid 23002382) id EA07320BED; Fri, 31 May 2024 16:22:00 +0000 (UTC) From: Hagar Hemdan To: CC: Maximilian Heyne , Norbert Manthey , Hagar Hemdan , Marc Zyngier , "Thomas Gleixner" , Eric Auger , , Subject: [PATCH v4] irqchip/gic-v3-its: Fix potential race condition in its_vlpi_prop_update() Date: Fri, 31 May 2024 16:21:44 +0000 Message-ID: <20240531162144.28650-1-hagarhem@amazon.com> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain its_vlpi_prop_update() calls lpi_write_config() which obtains the mapping information for a VLPI without lock held. So it could race with its_vlpi_unmap(). Since all 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 hoist the locking into its_irq_set_vcpu_affinity(). This bug was discovered using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc. Fixes: 015ec0386ab6 ("irqchip/gic-v3-its: Add VLPI configuration handling") Suggested-by: Marc Zyngier Signed-off-by: Hagar Hemdan --- v4: modified the commit msg. Only compile-tested, no access to HW. --- drivers/irqchip/irq-gic-v3-its.c | 65 +++++++++++++------------------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c index 40ebf1726393..f9e824ad1523 100644 --- a/drivers/irqchip/irq-gic-v3-its.c +++ b/drivers/irqchip/irq-gic-v3-its.c @@ -1846,28 +1846,22 @@ static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) { struct its_device *its_dev = irq_data_get_irq_chip_data(d); u32 event = its_get_event_id(d); - int ret = 0; 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,46 +1893,32 @@ 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; + return 0; } static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info) { struct its_device *its_dev = irq_data_get_irq_chip_data(d); 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; + return 0; } static int its_vlpi_unmap(struct irq_data *d) { struct its_device *its_dev = irq_data_get_irq_chip_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,9 +1942,7 @@ 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; + return 0; } static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) @@ -1987,29 +1965,40 @@ 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 = { -- 2.40.1