From nobody Mon Sep 28 12:33:47 2026 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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 6DE3939D6DD for ; Fri, 21 Aug 2026 21:40:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787348407; cv=none; b=dDNa3YJArBcFGEZgCjUOWzjTFBaM/rKTu3l+7j3dsi3m6eXSE00PmmBE1Rxz2KQmbJ0wzN6DBCvEmFkwEiTINam20RNEIzjgdBrEo+j+bSC05ZE2xJSom0n7RPqyWt1Zp1eYl1wcElhsYclK4zPKCJ+35Nea8sSSqFpzNmn22Cc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787348407; c=relaxed/simple; bh=vHUUkPXFwLf4KGnZSOqColwII/6gvw5qWQ+yFfH1aOk=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=nzjQqHIq8wAQv+FplWvg+S+0NmKrohEec1gXkb3R5srQxAzF2Ni6xK/tz7KSEFHi0jgDa+8dIdjdzdvpOhT5iSWKxTwIsKowLDY5SEkak7pAlqMdYFaCIC/xPieHbnXsyGE4ngMMJFZt8Qev8sh/ZyK2GCStHJekHJUKr0tc5EY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Cp5XNT9K; arc=none smtp.client-ip=209.85.218.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Cp5XNT9K" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-c2055f5a993so176152166b.2 for ; Fri, 21 Aug 2026 14:40:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787348404; x=1787953204; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=ITnR6ks3UA4W9A1ERgkESDSmUijciQ0O4sxmZ41mlKo=; b=Cp5XNT9K6R5Y7YqKlDUEMemw7JEVD5PszgM2t9EOHpQB3ASq0g5106jNXnOacxT9tp y7XLqV9TbfUpAsJUpXAN9GHDpnzrBSLhz7lxUfSfWLBLgt0OBmgYBsmIfEndT/jb0YeK 3k3dVWByYdKJXbDQG0bBbie2iyoXsY9UbZVbkSFwm2SAo/kw9CDgOV5l07ZrQPfp3PHt 6FDwKnkL5koCuqr8Yez3qMd1n1QTizhDM5gF/WnlhEHgGU2oJEB2D2BG9kgfjId2ocb+ ZPdWxUKuLo9W8B4e6fPqs2BN0qiuoj54Pa4TljUDCEzeOH/GaDJRQJT8EPSg++AnbiK2 OHDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787348404; x=1787953204; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ITnR6ks3UA4W9A1ERgkESDSmUijciQ0O4sxmZ41mlKo=; b=KgqXimQ6U5IcHLSW/1vsFdqMRFwMiRXPNLh7AahdDIa8xZMiQYr0Oku4UGo+ssEIG4 bNLKTTt90Kh8S8L3HvQvKdWvikX6QT1xzlKNlXCBf7muNwB2SMvCTXLOSuPlfjMdUxjH 8qdls2lpvr/2eKHI5sw26LzLrTxmkkUfy1F3ltb24inBu6YMulfAQRGXUb9WT74jDA1m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a17:907:c50a:b0:c21:1b58:fa96 with SMTP id a640c23a62f3a-c246a39f3a8mr1027520066b.3.1787348404303; Fri, 21 Aug 2026 14:40:04 -0700 (PDT) Received: from localhost.localdomain (dynamic-2a02-3100-a103-bf01-4ca5-89c5-9aa3-9754.310.pool.telefonica.de. [2a02:3100:a103:bf01:4ca5:89c5:9aa3:9754]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c2496738e7esm5834966b.46.2026.08.21.14.40.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 21 Aug 2026 14:40:03 -0700 (PDT) From: Karl Mehltretter To: "Michael S. Tsirkin" , Jason Wang Cc: Karl Mehltretter , Xuan Zhuo , "Eugenio Perez" , Jiri Pirko , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH] virtio: fix use-after-free in unregister_virtio_device() Date: Fri, 21 Aug 2026 23:39:53 +0200 Message-Id: <20260821213953.76906-1-kmehltretter@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" device_unregister() is device_del() plus put_device(). When the caller holds no extra reference, that drops the last one and runs the release callback, which for several transports frees the memory the embedded struct virtio_device sits in. unregister_virtio_device() then calls virtio_debug_device_exit(), which reads dev->debugfs_dir out of the freed object. Affected transports are the ones whose release callback frees and whose remove path takes no reference: virtio_mmio, virtio_vdpa, virtio_uml, mlxbf-tmfifo and virtio_ccw. virtio_pci is unaffected because virtio_pci_remove() brackets the call with get_device() and put_device(). Remove the debugfs entries before the device can go away. They are only accessed through the protected debugfs interface, so debugfs_remove_recursive() waits for in-progress file operations before returning. Tearing them down while the device is still alive is therefore safe. Reproduced on User-Mode Linux with CONFIG_KASAN and CONFIG_VIRTIO_DEBUG by unbinding a virtio-uml device: BUG: KASAN: slab-use-after-free in virtio_debug_device_exit+0x36/0x4d Read of size 8 at addr 00000000616e0b10 by task init/1 __asan_report_load8_noabort virtio_debug_device_exit+0x36/0x4d unregister_virtio_device+0x48/0x75 virtio_uml_remove platform_remove device_release_driver_internal unbind_store Freed by task 1: kfree virtio_uml_release_dev device_release kobject_put put_device device_unregister unregister_virtio_device+0x48/0x75 virtio_uml_remove With this applied, the report is gone and unbind is clean. Fixes: 96a8326d69ff ("virtio: add debugfs infrastructure to allow to debug = virtio features") Assisted-by: Claude:claude-opus-5 Signed-off-by: Karl Mehltretter --- drivers/virtio/virtio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 75bb4ffe3b877..b6c9e927bef57 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -604,8 +604,8 @@ void unregister_virtio_device(struct virtio_device *dev) { int index =3D dev->index; /* save for after device release */ =20 - device_unregister(&dev->dev); virtio_debug_device_exit(dev); + device_unregister(&dev->dev); ida_free(&virtio_index_ida, index); } EXPORT_SYMBOL_GPL(unregister_virtio_device); base-commit: 4477a78374a57c3809b172ad30cceabda48c47c6 --=20 2.53.0