Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1120596lqa; Sun, 28 Apr 2024 20:41:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU3/YmECtmBAjgpgd32Oy4XIRoxRx4PQ+gNV6OENZTqhe7pO+ePzJPGwksc8yIegJPA2AF3MKZBwDUIV6T2R+Qig1l5UhU7lr3KvHiMMA== X-Google-Smtp-Source: AGHT+IGHC3l1uXsEAduT1GGiJZe/X4FT06XtXX30BfYIktaAcDXvWqqldQxF7QYPoAV1QGTR3CWf X-Received: by 2002:a17:90a:71c8:b0:2b0:ef31:1480 with SMTP id m8-20020a17090a71c800b002b0ef311480mr3747152pjs.33.1714362111469; Sun, 28 Apr 2024 20:41:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714362111; cv=pass; d=google.com; s=arc-20160816; b=LMdCL/1ryUJjGK9GKlGgGqh0OgyDQ3hwrllmoAv8surh+1W0ItE/qavUyMe9Z3bsO0 X7Fg50WRxYEg3tFepsEuR41uKK0b16lqAhgr5cle6o8m5SvhX2jnXm6fKW2cygXyrzfG FjRhNLw5g/OHDwhROuai6PGDQ7pk/oboq62MHlK6bprRHJyfOUSsGdjQZnOFaJOPmV7c Gj0rZ9s3MloBs2OULtDoOt0fqNu1U+P4qZCtbW651XnMTGNy6La+PIShyxQ6VOubuEFT fJaKl2SBo6zc5fSOXnpCkGCE67MPQOI8DhBmJI/ntE7hQv6Lhmdvax9pL5pW0mmkSJD0 1f7g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :date:dkim-signature; bh=K9jmX3QxBB0ptLSuKTUjjV92gcfpZ8FtQn0+7RhIfec=; fh=w2eTEGZ0XMWOx8nbgMtnaMCuCW8wPwLxHSJYxIjnqXo=; b=DQfHRE0mp4X8iwKdjoppK/9fUlq/Q6SB88upkjRdFUTIcG86kPbC1izXnFypt8NUGB c1RgxWNFZ177Wu6VBOjoI/mb+5h2W72Wb2JQk6Mz2s8Qz22fGQOVLjOH2+OnKte7F0Sp tmdCzB0GIGadVtzzi+WQ7WRQI3SWBm598TtIm5AwxpSRS1us1ykQqPziVIIofmpPtsUW Ta+LT6LA+SUez616rfK0Ad59SzmcJWpDrPKPcMu8E0VacjuXjIx8or+rC90b/H+Iu4tT awk8L4PX1o2DhV8EFmI0jMmwuRunz1H/HJEVo17kCWW8Pttmy4pd5ogtmO9djdYvg7Xs qykw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NFRyUeZU; arc=pass (i=1 spf=pass spfdomain=flex--smostafa.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-161705-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161705-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id v13-20020a17090a898d00b002b0f4ffa34dsi3171230pjn.38.2024.04.28.20.41.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Apr 2024 20:41:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161705-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NFRyUeZU; arc=pass (i=1 spf=pass spfdomain=flex--smostafa.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-161705-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161705-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 1BF4628299F for ; Mon, 29 Apr 2024 03:41:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 09CA98F6E; Mon, 29 Apr 2024 03:41:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NFRyUeZU" Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DB3F6FD0 for ; Mon, 29 Apr 2024 03:41:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714362103; cv=none; b=LIAM7x8WA5KNBVWvTB9RCF6GOFdLN0WD1h5PTY86BMQpb8jXNpi27hu4RtvZs9CxQGLvV7ikwDA0z5cHlnWNqOLp0NPzCZ7OumHLHKkPYgWnGcjZ8z8RNZ4wgtoYBvqJAxpnra+2kI+TjKmUXRhQFgEtM263F2SL2r7wrb/12Ms= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714362103; c=relaxed/simple; bh=XcC2SQ+hyRPVOuSGKyLwXqtjOb9L2X26n5bIqzPSuos=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=GzggFaL17gRmKkIpb2KWDLzkpf3N0XkOvUieVGVRDR2a9nbf04pbScBoJWMulh9RaEtGCnWvHm+YcU2sApI+DhSj/pXRnPG0UORdoyGhBbJ9FkzEtHUMaOIy3sCw/XZfEfrqFN2qUP0qb9o8xeJ7QQCJdbyhL3Zhht3VdGotBhY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NFRyUeZU; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-345aa380e51so2771850f8f.3 for ; Sun, 28 Apr 2024 20:41:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1714362099; x=1714966899; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=K9jmX3QxBB0ptLSuKTUjjV92gcfpZ8FtQn0+7RhIfec=; b=NFRyUeZUldSCicbDBMoR+9dqD4t38VnXs6qOTsc2UDom4nzzetWwbI0T70jYQWh+nS 8UR0mCx9SnMoZzg01hbpY55IHl0Gim2v8k+bk567wzzOP1dt+6auMtNd9aMjMCy4moxr 8dnthUQg16ZHfsXSeeFG1wkC6zjztRh3rIlEOFjNdNWa/Z+PBUMDq1gFB+LzvXipRDWr BRRyEXHbSLmjFrEKB3p4s9IIH4tFhKAJSuBE6ugTG7QAL324lzeFuyRfJ12zngFfBGY4 /+eVHh5WrgTKKraP2Fj4+w7bThTTE+6HGwvdJ8eY3CNdef5HOe1sQVR/3nINKH+Xnfyf FF0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714362099; x=1714966899; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=K9jmX3QxBB0ptLSuKTUjjV92gcfpZ8FtQn0+7RhIfec=; b=A7lNECphQdWdV0YNT7IxiJ1BusuOlilhqjE8y0WTVIV99da8I0436WuEyNaqixBRT4 vQeufBJOvqhvGLVDrTMFEkvuiRGC9/N+SFyIAC30czKYRG33fTG22D9P1jSglL/6WJaK sxzT7dnFQ022JeV78SX70QvDtzZzAzmbTNJIFG/3fz+4bjySDRI3i6XVh3vMtTS3eTjz ljdDZ5EaLndYmkPg6NtuKeUzv3qGzlBYYy/uPyRKDwLH9sky8O9GghnYo188rVPlhY95 myMDYu0NokmaxfJmChSQVe/te8yVZIwNvXMVMNVcocihro3Y+70CdHE8jXT+AIV8bdHK 02Jw== X-Forwarded-Encrypted: i=1; AJvYcCU+01sg/WvzqOrSNyG4Jq8PtPBvB/bnddTofpxqudf97vMB1oknNaZQPxp4KYzwjRogzfvheWGFTfaQ2Ad5e1nnnH0ePdwsIxsQY3Pm X-Gm-Message-State: AOJu0YwK8aNhKafNgPP5Em1PuvEtqCxfJ8NG2LBbBQwHuaQBJ+u+kEF5 wEIDHzlW9GB33icQlFiPmsFjXLkoydQ83RkKMmaMAlMjN63+ycgb36PEORPqAngMBiYVN8w5qcP QNVn9rQr9Zw== X-Received: from mostafa.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:333c]) (user=smostafa job=sendgmr) by 2002:a7b:cc0d:0:b0:416:3e78:2a2b with SMTP id f13-20020a7bcc0d000000b004163e782a2bmr41613wmh.6.1714362098699; Sun, 28 Apr 2024 20:41:38 -0700 (PDT) Date: Mon, 29 Apr 2024 03:41:12 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Message-ID: <20240429034112.140594-1-smostafa@google.com> Subject: [PATCH] PCI/MSI: Fix UAF in msi_capability_init From: Mostafa Saleh To: bhelgaas@google.com, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ilpo.jarvinen@linux.intel.com, tglx@linutronix.de, kernel-team@android.com, Mostafa Saleh Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable While running Android15-6.6 and hacking on latest Qemu, I observed the following with KFENCE: [ 10.761992][ T81] pcieport 0000:00:03.0: Adding to iommu group 0 [ 10.782536][ T81] pcieport 0000:00:03.0: enabling device (0000 -> 0003= ) [ 10.814259][ T81] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ 10.816534][ T81] BUG: KFENCE: use-after-free read in __pci_enable_msi= _range+0x2c0/0x488 [ 10.816534][ T81] [ 10.820189][ T81] Use-after-free read at 0x0000000024629571 (in kfence= -#12): [ 10.822652][ T81] __pci_enable_msi_range+0x2c0/0x488 [ 10.824304][ T81] pci_alloc_irq_vectors_affinity+0xec/0x14c [ 10.826129][ T81] pci_alloc_irq_vectors+0x18/0x28 [ 10.827606][ T81] pcie_portdrv_probe+0x1e0/0x608 [ 10.828979][ T81] pci_device_probe+0xa8/0x174 [ 10.830316][ T81] really_probe+0x150/0x2b8 [ 10.831717][ T81] __driver_probe_device+0x7c/0x130 [ 10.833368][ T81] driver_probe_device+0x40/0x118 [ 10.834899][ T81] __device_attach_driver+0xc4/0x108 [ 10.836464][ T81] bus_for_each_drv+0x8c/0xf0 [ 10.837915][ T81] __device_attach+0xa4/0x198 [ 10.839260][ T81] device_initial_probe+0x18/0x28 [ 10.840684][ T81] bus_probe_device+0xb0/0xb4 [ 10.842174][ T81] deferred_probe_work_func+0xac/0xf8 [ 10.843760][ T81] process_one_work+0x18c/0x414 [ 10.845129][ T81] worker_thread+0x41c/0x53c [ 10.846611][ T81] kthread+0x114/0x118 [ 10.847841][ T81] ret_from_fork+0x10/0x20 [ 10.849298][ T81] [ 10.850435][ T81] kfence-#12: 0x0000000008614900-0x00000000e06c228d, s= ize=3D104, cache=3Dkmalloc-128 [ 10.850435][ T81] [ 10.853455][ T81] allocated by task 81 on cpu 7 at 10.808142s: [ 10.856329][ T81] __kmem_cache_alloc_node+0x1f0/0x2bc [ 10.857988][ T81] kmalloc_trace+0x44/0x138 [ 10.859299][ T81] msi_alloc_desc+0x3c/0x9c [ 10.860582][ T81] msi_domain_insert_msi_desc+0x30/0x78 [ 10.862058][ T81] msi_setup_msi_desc+0x13c/0x184 [ 10.862942][ T81] __pci_enable_msi_range+0x258/0x488 [ 10.863847][ T81] pci_alloc_irq_vectors_affinity+0xec/0x14c [ 10.864821][ T81] pci_alloc_irq_vectors+0x18/0x28 [ 10.866011][ T81] pcie_portdrv_probe+0x1e0/0x608 [ 10.867047][ T81] pci_device_probe+0xa8/0x174 [ 10.867937][ T81] really_probe+0x150/0x2b8 [ 10.868774][ T81] __driver_probe_device+0x7c/0x130 [ 10.869755][ T81] driver_probe_device+0x40/0x118 [ 10.870665][ T81] __device_attach_driver+0xc4/0x108 [ 10.871654][ T81] bus_for_each_drv+0x8c/0xf0 [ 10.872585][ T81] __device_attach+0xa4/0x198 [ 10.873581][ T81] device_initial_probe+0x18/0x28 [ 10.874645][ T81] bus_probe_device+0xb0/0xb4 [ 10.875610][ T81] deferred_probe_work_func+0xac/0xf8 [ 10.876696][ T81] process_one_work+0x18c/0x414 [ 10.877573][ T81] worker_thread+0x41c/0x53c [ 10.878468][ T81] kthread+0x114/0x118 [ 10.879331][ T81] ret_from_fork+0x10/0x20 [ 10.880238][ T81] [ 10.880767][ T81] freed by task 81 on cpu 7 at 10.811436s: [ 10.882311][ T81] msi_domain_free_descs+0xd4/0x10c [ 10.883318][ T81] msi_domain_free_locked.part.0+0xc0/0x1d8 [ 10.884401][ T81] msi_domain_alloc_irqs_all_locked+0xb4/0xbc [ 10.885519][ T81] pci_msi_setup_msi_irqs+0x30/0x4c [ 10.886549][ T81] __pci_enable_msi_range+0x2a8/0x488 [ 10.887537][ T81] pci_alloc_irq_vectors_affinity+0xec/0x14c [ 10.888576][ T81] pci_alloc_irq_vectors+0x18/0x28 [ 10.889693][ T81] pcie_portdrv_probe+0x1e0/0x608 [ 10.890719][ T81] pci_device_probe+0xa8/0x174 [ 10.891623][ T81] really_probe+0x150/0x2b8 [ 10.892534][ T81] __driver_probe_device+0x7c/0x130 [ 10.893541][ T81] driver_probe_device+0x40/0x118 [ 10.894491][ T81] __device_attach_driver+0xc4/0x108 [ 10.895433][ T81] bus_for_each_drv+0x8c/0xf0 [ 10.896381][ T81] __device_attach+0xa4/0x198 [ 10.897359][ T81] device_initial_probe+0x18/0x28 [ 10.898401][ T81] bus_probe_device+0xb0/0xb4 [ 10.899268][ T81] deferred_probe_work_func+0xac/0xf8 [ 10.900314][ T81] process_one_work+0x18c/0x414 [ 10.901279][ T81] worker_thread+0x41c/0x53c [ 10.902138][ T81] kthread+0x114/0x118 [ 10.902916][ T81] ret_from_fork+0x10/0x20 [ 10.903816][ T81] [ 10.904429][ T81] CPU: 7 PID: 81 Comm: kworker/u16:2 Not tainted 6.6.2= 3mostafa+ #224 [ 10.906362][ T81] Hardware name: linux,dummy-virt (DT) [ 10.907934][ T81] Workqueue: events_unbound deferred_probe_work_func [ 10.909959][ T81] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Looking at the upstream code, it seems to have the same issue where: Descriptor allocation done in: __pci_enable_msi_range msi_capability_init msi_setup_msi_desc msi_insert_msi_desc msi_domain_insert_msi_desc msi_alloc_desc ... Freed in case of failure in __msi_domain_alloc_locked() __pci_enable_msi_range msi_capability_init pci_msi_setup_msi_irqs msi_domain_alloc_irqs_all_locked msi_domain_alloc_locked __msi_domain_alloc_locked =3D> fails msi_domain_free_locked ... That failure would propagate back till pci_msi_setup_msi_irqs() call in msi_capability_init() which have "goto err" which would access the descriptor to clear the mask. However, we can=E2=80=99t make assumptions if the descriptor is freed or no= t as it depends on the failure location and on MSI_FLAG_FREE_MSI_DESCS, so one simple solution is to re-read it. Also, calling pci_free_msi_irqs() after will trigger msi_domain_free_locked() again, however it re-iterates through the xa array, which should be safe. I hit this only once, but I can confirm the bug(and verify the fix) for the upstream kernel through stubbing return of __msi_domain_alloc_locked() to fail and with KASAN enabled. bf6e054e0e3f ("genirq/msi: Provide msi_device_populate/destroy_sysfs()") is the first commit that introduced the free logic from the context of pci_msi_setup_msi_irqs() Signed-off-by: Mostafa Saleh --- drivers/pci/msi/msi.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/pci/msi/msi.c b/drivers/pci/msi/msi.c index 682fa877478f..dd961151fecf 100644 --- a/drivers/pci/msi/msi.c +++ b/drivers/pci/msi/msi.c @@ -393,7 +393,10 @@ static int msi_capability_init(struct pci_dev *dev, in= t nvec, goto unlock; =20 err: - pci_msi_unmask(entry, msi_multi_mask(entry)); + /* Re-read the descriptor as it might have been freed */ + entry =3D msi_first_desc(&dev->dev, MSI_DESC_ALL); + if (entry) + pci_msi_unmask(entry, msi_multi_mask(entry)); pci_free_msi_irqs(dev); fail: dev->msi_enabled =3D 0; --=20 2.44.0.769.g3c40516874-goog