Received: by 2002:a05:6500:2018:b0:1fb:9675:f89d with SMTP id t24csp563619lqh; Fri, 31 May 2024 09:23:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXJG+nYhXi+v5Sn/gkrYEpywXEqgVJ20Zo2+HecrPhb616hT64VwMqx22+3j8kk6wasR6lFLGVJHxZhVkcSzXYosLiQQMi9/oEzm8tpLQ== X-Google-Smtp-Source: AGHT+IG5hYoBCNebIVTGRixePEi4eYy8c2dZw1tMsyqhquvkXjLHC1LOn6RoS4fxhskUadhnrFJy X-Received: by 2002:a05:6358:2810:b0:199:29f4:c57b with SMTP id e5c5f4694b2df-19b48c28200mr282724555d.11.1717172604968; Fri, 31 May 2024 09:23:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717172604; cv=pass; d=google.com; s=arc-20160816; b=kByIy6GIfdLStUk5k0zaPdPmZzaGZv8u+jC0bwrEIQLqdhCiLQ7hED4THolw1cIPuy 74jrg5saM+vksTj1+Zm3k0Gqq2y83hMiG9zI1EeU6y6kXtQp53/MCoRcCvfPK3PlFt25 KCkb8Yn/OVtQdkG0V9qZ07cGrthcxXhI+68HIposnhgNuVAwcmKMmejk6WqwRW5R4QGb CB4Xidotb3s3qX0wYSRDoI7GBBGck1p1NKakg/aPPcW+HAs8NcSTE7ea8MQxiWgn8ycR xnGxMLh08DYj1mxuaSRLbNA0Ij3MW3TShTTiP9A+cZw+P+Yz2W+j3EVEcaRjuPyuzY/S z5Rg== 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=Kkjdm7vLzgsvxwTIl6a+cQaY/194/UYr9RfyTUcw400=; fh=M/R+Cd5xTY5VaEyQPIjUavZyTYFiPJYv9Rbv7ueof+s=; b=FgfMj/YTO6hv0CIeXD34FPbbHLuaUNmKIIL70tqVhsHHitk5rnex06KhYMO+57GBz9 AniW6HHBu7kDpYrCzl1R2t67yWGzhbDS+GgvtQJrIeU4ii4SslbL3rsSDEt9TuYBMhYz BgeM5TRRJoxnIA+UquElvN7nTQiWQV9YsnaiQK4OjpVThF2pU+JhjyIQgAkwnH/afESV Ilbtlq9bkONpGwEyGpezVk7ziCVNgGIIhPdsgm6F74WwOqB1pQ3JwY9GGzrbRjCrtcQI 0w3MwG/Q8gw4XRT2ufDveMdrUCExcynqERQuXXYXKdWvHPR7fLGQQyil0CeBRFKfHJXD XSjg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=R3yoT+pG; 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-197139-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-197139-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 41be03b00d2f7-6c35d59d911si1816920a12.810.2024.05.31.09.23.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 May 2024 09:23:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-197139-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=R3yoT+pG; 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-197139-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-197139-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 678C028922E for ; Fri, 31 May 2024 16:22:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C5427158D7A; Fri, 31 May 2024 16:22:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="R3yoT+pG" 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 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