From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796066451; b=WolV1qKaDrt2wROoV9aiF2azPl/2Qk1XlDajznfBT3bzBxO6vgW0YapYi/EfLahj+0Ap gE0PHagqMVobnnEM2+AZf6bKO9LsaPAvIjcJ+gwn7PW+o0lIlIk29wPyNyv7V2H5MfKU1 x7b3mg1fDPZs8nb0kikMI/9xdksw8U6EF96Is8aFy2CJqeLHEZny2OgovXmNBlNDSE7lK ZcqGIiwJ/XsoA6FcZGxc7Y+zTJh2rBeehSq6rFjXTRg/D4TBP+OC5FC+QaJECij00TTeS W90noiHEQCpiIEKauRvToU0YngOESqROyhjJ72aRBve+4VSnuNzB6te8YpFmKXzz7gg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796066451; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=kUB3fscZZkDCLerz05MZvgi9v7RV0PiGNWy7Rbx5nIM=; b=Jr6Fj7jrEtaXnhV1arQheIvCm2c1I6bgoQVV0A09MXquRUcO5e7QEI++jdXFcMrp4X04 cBlDDprxFJgBEO3Vpi+pZQjU9uqpnmo7aAvkpmSWA1VYUdWNaBAxQEXLBQHlzQgPxpB74 CjHkv2tUT+XUbTCM0a2/QVCYeNK/YvPZzWBFGi/7e7V5Nrq6nGFmDfvgGCYiR38tIT2+C hG2UZKGAG4LqgNDK9mOuOyufZbTW4mAqCqy8bVyVqqOJyB/5KY/71DyJSaivIlLuWuRhg WYbAKIDzJzVPub7oDd0ABRYYLSJd+MVymv8xUb5VS3AC/iv12pLYb8L7/c3lkuND7IA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=KlQeoJX3; arc=none smtp.remote-ip=209.85.221.48 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6DF0E606A320 for ; Mon, 30 Nov 2026 19:20:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6DF0E606A320 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.a=rsa-sha256 header.s=20260707 header.b=KlQeoJX3 Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-491f34b4e0fso4856417f8f.2 for ; Mon, 30 Nov 2026 11:20:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796066449; x=1796671249; darn=lists.linux.dev; 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=kUB3fscZZkDCLerz05MZvgi9v7RV0PiGNWy7Rbx5nIM=; b=KlQeoJX3BrLEqpQuovoSLq0J4ZQ5K/wy3zmNnmIzssXJGqh+py9VxgBN5XBIz+Evpj YRC1xOAGXwgiSIMPgmToBnJo+CG763XaBr+cFKUcB0dtEceWxOF51b/zQSt9ju9ZsYST 5WWkP6ui1il3CaUrIY7EekJWxo4wBTWLbc/l4YLZNqaKBHFWohVxRYbbfnJ0q0Y0o9wl zJaouqi4QAieTOgA/DsjxNX/hEDZCu5xkiLxxg2Rb+8xMO5flBoTzFIepf/5fOoqI7a2 tZxidKq7/s6EJk54WXBHLcew5NUSSblQrs7TZKZNtbOnlXnXyWCuE50e4YZFOUw0ZV8s bO+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796066449; x=1796671249; 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=kUB3fscZZkDCLerz05MZvgi9v7RV0PiGNWy7Rbx5nIM=; b=MQz3LXavu1GZE2EFancgAR7DqKu33p7Lo1Y8diIlTXU3gyyWbhn8Wgs3Y07497jEQ9 MkwbLvjWCMLQa1IfKvJDWJ5Mz4v6PHfQnvI33Vcy69aNsws3y18auUafpOynA0CGatVZ 7o0l4yXDT7AvuiwH2i/N3xSM2lQxqfm8HUspckTvSIcxoGc3sdFfc5/oDtJbftaLy/X8 3ADu2S1M+K7o97w3WtuY2mFtPxGdUpW4ZVQ0mt1EjSS5N2z7Ex8T9oIdawDl+Wmgp+vg glli2e5nzijo5oFj1fqazDec6Cq745pKNeVsLg8FNuahp+BYPO7XwyShu+8dX2gdjQTl EkVQ== X-Forwarded-Encrypted: i=1; AL+hErGw49p7zbztSMvOALarfTkt+R7Ec/DuGPyYA7foriHqbbt+YALSTlFQHRt5KMhSDFLOGzoCI0muJ2FXBYbCNA==@lists.linux.dev X-Gm-Message-State: APuU4Z/Hh6oVfGUyumBKHMS3sIoMtrCl2wdgJr5mL9Ruufk/f2u9484R /hYB/Av0Ksu4MSiO0bpc++DtoKQNIBjRefOOY2b/NxCvO2jVsEQ1F3UB X-Gm-Gg: AZFdTZf8cMaEKnOWEDHmysoJ7QeE0rZD7UEu1gh3zWtDBA2dCHHAiebrK8fZyydbZNM IWqE7BBKIQ6+w5IHEgS5dsf8Msy76CdVhEjj08GVrXadVG24eHgujGNhXwKmuhtoCJMHaq0qUWq yeF/fbDbU3UUUpQiY6ZX6F975+/EhhEpllTo5Z791/g/d1S4RmNrNUsoMW/YG8clGdsASMNXy69 ucfJZBFlR6oiezeNEqzDM+ZiYOJp9tmrUp051BUQt0z+YCjGG22h3rKxXNq3v9N53rzwd8S18JQ h2NKykE4CR7MZMACKPw154sY9YmA4OlYaMsWh/mqxU+cMIHa/my/SQwr4i3tpukw2ljFMUD3oHP oT6BDjas3Ys3p8TxLk2RCgdmKrhWCLTmXstHZ4jd1q7sOZTyOfJEtDj7LG8kLQj8wf5SAA9AwHv X4U5hsCDN5qa90uT91s5/8dw9O+59VkrW+Ff0cy5X5AoBzddjUywYnasQWEko1dLZhOdY24Ch4d /p70ioQCA7z4HNjbFSnbNyIbzyibrdacZYbBg8zgEEx3MNS8O7JcbkF4/UAxCPnofElUGs8lsqi fgfaZ4wNy8n4OQ0+LA6xRnFSKCYTJ3gZXmc= X-Received: by 2002:a05:600c:6383:b0:4bb:c0b7:6cb4 with SMTP id 5b1f17b1804b1-4bbc0b76d82mr122763055e9.11.1796066448562; Mon, 30 Nov 2026 11:20:48 -0800 (PST) Received: from gmail.com ([2a00:1bb8:11c:ad4d:cd83:c66f:8de4:f9f1]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb884865a2sm995246845e9.3.2026.11.30.11.20.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 11:20:47 -0800 (PST) From: Stjepan Poljak To: "Michael S. Tsirkin" , Jason Wang Cc: =?UTF-8?q?Eugenio=20P=C3=A9rez?= , Xuan Zhuo , Jiri Pirko , Paolo Abeni , Karl Mehltretter , virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH] virtio: Remove debugfs entries when device_add() fails Date: Mon, 30 Nov 2026 20:20:27 +0100 Message-ID: <20261130192040.1931044-1-stjepan.poljak@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit register_virtio_device() creates the device's debugfs directory before calling device_add(), but the device_add() error path does not remove it. The files in that directory keep the virtio device as their private data, and the caller is required to drop its reference with put_device() on failure, which frees the device. Reading any of the leftover files afterwards dereferences freed memory. The device index is also returned to the IDA, so the next device to reuse it fails to create its "virtioN" debugfs directory because the stale one still exists. Commit 3f9a0fceb730 ("virtio: fix use-after-free in unregister_virtio_device()") fixed the same kind of lifetime problem on the unregister side. Remove the debugfs entries on the device_add() failure path as well, before the caller can free the device. Reproduced on x86_64 with CONFIG_KASAN and CONFIG_VIRTIO_DEBUG by rebinding a virtio-mmio balloon device with fail-nth slab fault injection: once an allocation inside device_add() fails, the stale debugfs directory remains, and reading its device_features file triggers a slab-use-after-free in vm_get_features(). With this patch applied, no directory is left behind and no report occurs. Fixes: 96a8326d69ff ("virtio: add debugfs infrastructure to allow to debug virtio features") Cc: stable@vger.kernel.org Signed-off-by: Stjepan Poljak --- drivers/virtio/virtio.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index c8be1a3f479d..22cc2d71c5a3 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -580,11 +580,12 @@ int register_virtio_device(struct virtio_device *dev) */ err = device_add(&dev->dev); if (err) - goto out_of_node_put; + goto out_debug_exit; return 0; -out_of_node_put: +out_debug_exit: + virtio_debug_device_exit(dev); of_node_put(dev->dev.of_node); out_ida_remove: ida_free(&virtio_index_ida, dev->index); base-commit: 44293796526f4343639f87b2acc53811c78b5d54 -- 2.53.0