Received: by 2002:a05:6500:2018:b0:1fb:9675:f89d with SMTP id t24csp284038lqh; Fri, 31 May 2024 00:47:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWr1Hhg6jWrK+MjD7txfXGe62q79kKtYgwX6NNl4FG7ZBKXdrdnKLkgOzlj9c7oShJBm9Jxc/3jiuy4bfWA6v7lsX8X6RoSZG00n5OSyw== X-Google-Smtp-Source: AGHT+IG7tZ6h31cTtO3q8TcKv4j+lwbHJ96ypkZ+SY8K+6vppsHFE+ELGwH7l0O55VtWVRvIfAhZ X-Received: by 2002:a17:902:fb0d:b0:1ec:3ae4:cfe9 with SMTP id d9443c01a7336-1f63702909fmr8857495ad.33.1717141677346; Fri, 31 May 2024 00:47:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717141677; cv=pass; d=google.com; s=arc-20160816; b=KcDFZ/C3BE6NmXDqVimMF/Y6gP8wK3KDpdYEeT/NzmlCoH+s06AtmHsMwZIEnun7s5 9CwV2BvQ52UH9QNgL3RqrYnBsc6A+PPUc5XLVnx/sTYSk0+efTwlYlA4fv7eM2It0EVr xmVeshGI+REC5vhk9t48PCHf62DrBbb45GUS8yBnGF0No+bNRe8YhdMQotkgRsNIrRcJ A2mUse9QjtZOBdAj1zrfMhynXeXECCczfvSt6d1bIMc3PyNaM4AM62+pKqjt3X+lJ7Bn uP7SwuCGxZXZ2HcTD9y9InVgYxsQugIGTZ/0Hee+t9W5J8S92miFszqfoA5t3vXxnK/X bI0Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=/9TcEfcmlhOhbQiEqMm/468xXgoXewDL5g4J3m8ERMc=; fh=M/R+Cd5xTY5VaEyQPIjUavZyTYFiPJYv9Rbv7ueof+s=; b=1APYbQgvpSTTExllI/OPXK+cLt/3DfpSKf2oV3Yc8buUU3BQNLEpXDUGIcfYljb/eD hXU3vIYgoZiEaedooAvrwlTitWjGjb5J9v37NkHtMbNNSAo1flFqft1FygCMJB+AjTfH eiGfSe3BZxmqzZx0ZWffgYK1/9zkr4u4w37hFVJhkRcUzX7poNu3mGpUg+hWXsZhwPrL 3YuJjSbDSSpLuk96xlH3KWAzNP2/Ip0quHDFISj0CNOhmwRHf8Of+0pPUa1z4DsJ9MMu dTkZJ+s+1rnQpItcL6pCWR2+VtxL7XOaIHbLEfAVJ49gsDmcXdwF3UwSD5mqm6u73mP7 CZng==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=P327yaoV; arc=pass (i=1 spf=pass spfdomain=amazon.com dkim=pass dkdomain=amazon.com dmarc=pass fromdomain=amazon.com); spf=pass (google.com: domain of linux-kernel+bounces-196421-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196421-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d9443c01a7336-1f632426fb4si10999475ad.645.2024.05.31.00.47.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 May 2024 00:47:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-196421-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=P327yaoV; arc=pass (i=1 spf=pass spfdomain=amazon.com dkim=pass dkdomain=amazon.com dmarc=pass fromdomain=amazon.com); spf=pass (google.com: domain of linux-kernel+bounces-196421-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196421-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 919B8B25292 for ; Fri, 31 May 2024 07:43:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D1FC574074; Fri, 31 May 2024 07:43:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="P327yaoV" Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) (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 4E91225569 for ; Fri, 31 May 2024 07:43:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.154 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717141393; cv=none; b=Pfw4q+ISV1tiozUBEImUUkw697ae7D75XBmU7dK4fJcEaMcRC+DJpgZ5mDyUWzgPScut21iwnc9NSYrsy6y6lVQIjl3S0XPT72ogqrLksxqo6vSQC5vkX3qkSm2YaSDaHbSBKh+RbXuhHnXyL4JJutdE4sPl7JmqqbhxGLvOVTE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717141393; c=relaxed/simple; bh=B0fGci5myXdi4dLPi1r9qzO7UZqSVS6IjHMJiUm1a3Q=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=tfg4u3IWsHmuKcDJS2lXr3pkIaqArZcMQnWvggbPPvdkwJHe3E5vjW9tlXZalnrcf6sQvHYbfRpyJ4xUpX1ElEYY381NK6KWssWQRzxEtmZc3nLNUWFw96Rx9eKftiV0Q651SYJrAV65nHuTloXFBsOd257E+aa4mzp04TLtZBQ= 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=P327yaoV; arc=none smtp.client-ip=52.119.213.154 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1717141393; x=1748677393; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=/9TcEfcmlhOhbQiEqMm/468xXgoXewDL5g4J3m8ERMc=; b=P327yaoV+hH8En7p0/HxMaTQEvvG+43nPPtDXpvlvWxJFfgdUHNqaWGY 6uYtzLuSEUPm7r9TDR6c818YRBqGCUY/TZOHqSHsC4J89q+ARtnEXSiVA S/Er3Wow/VHPHf19U54yzEAynWnFgVeHckrax1EH3gv4hvVeFnSgbV5LQ s=; X-IronPort-AV: E=Sophos;i="6.08,203,1712620800"; d="scan'208";a="208722784" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 07:43:10 +0000 Received: from EX19MTAEUB002.ant.amazon.com [10.0.43.254:52673] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.27.131:2525] with esmtp (Farcaster) id dc36c378-df5c-4d05-b391-0f404366b043; Fri, 31 May 2024 07:43:08 +0000 (UTC) X-Farcaster-Flow-ID: dc36c378-df5c-4d05-b391-0f404366b043 Received: from EX19D008EUA004.ant.amazon.com (10.252.50.158) by EX19MTAEUB002.ant.amazon.com (10.252.51.59) 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 07:43:07 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) by EX19D008EUA004.ant.amazon.com (10.252.50.158) 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 07:43:07 +0000 Received: from dev-dsk-hagarhem-1b-b868d8d5.eu-west-1.amazon.com (10.253.65.58) by mail-relay.amazon.com (10.252.135.200) with Microsoft SMTP Server id 15.2.1258.28 via Frontend Transport; Fri, 31 May 2024 07:43:07 +0000 Received: by dev-dsk-hagarhem-1b-b868d8d5.eu-west-1.amazon.com (Postfix, from userid 23002382) id 0A2E120BED; Fri, 31 May 2024 07:43:07 +0000 (UTC) From: Hagar Hemdan To: CC: Maximilian Heyne , Norbert Manthey , Hagar Hemdan , Marc Zyngier , "Thomas Gleixner" , Eric Auger , , Subject: [PATCH v2] irqchip/gic-v3-its: Fix potential race condition in its_vlpi_prop_update() Date: Fri, 31 May 2024 07:43:02 +0000 Message-ID: <20240531074302.30563-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 and resolved using Coverity Static Analysis Security Testing (SAST) by Synopsys, Inc. Fixes: 015ec0386ab6 ("irqchip/gic-v3-its: Add VLPI configuration handling") Signed-off-by: Hagar Hemdan --- v2: moved the lock to its_irq_set_vcpu_affinity(). 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