From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796066450; b=j8PCs0q3qGNNEDJlymalIJ3IQCugZUoQ0FEYc88hPkd+M6KTw0dp9vD5bgf5XSFMNCgN oPYivzD9gm+a3WASnS+Ku1YapKMnnjO8KLpcXS8Jf4R470JSHjIaWs4mtZklqb8raLGlY O4XnKi+BESpg4V03MrwPxKT0q2mIetZrVDbSfbX2ccBEf4QAJ5XUdUuAB4p0s5fPl8jZY U9K1od8WGLuGjOrBXF2EBC0RawhuMFHjzrTV4tsW+Aw1yRq9e5bpUy87VzxWsIVR6X3LR A3R+E60jRQHqt5ju0Y/17Mh7oUQBBhKEcCzzR3UumThOvujK+bHYCALe7EiH1yuqEOg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796066450; 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=a+rIHP9mHb67wk5u0wujXpkknsk4COrQENBOixI3hhBXOryzJlval9zb2znqAsNJqBCx +P0vRXGcc6yLZaRod+E8GokJ6BYrwlCOnb3JGEfkzPEs2rDsxcG7QKrTLWbaoYpuRYpuZ 95LSvokMDW5C8yheGNDjCKygQwLWLBdXqcfOlOViYM8nLKGC/Jm2woNQOHrk31R4IQPqv +ns7bVwRoN9zcrzfub4nx42VjrRmXdi632zknhzpu4T1OSMj8Q0iaooGSilGmfcW57XI7 uC08T8KqC3wRuBmr7uvRrWwCj+7uv0BEkxZJlkAORbygrANETlQ+Infl28B2dEFvvdQ== 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=mrLWDZdv; arc=none smtp.remote-ip=209.85.128.47 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 6A458606A31C for ; Mon, 30 Nov 2026 19:20:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6A458606A31C 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=mrLWDZdv Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4b79502c27aso109601375e9.1 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=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=kUB3fscZZkDCLerz05MZvgi9v7RV0PiGNWy7Rbx5nIM=; b=mrLWDZdv1X88CiFVq4KOQE8MJJgBYh65agJjp4ZIM9JJiOI4rSXoDFy2a1DwSyU9T5 1Vuhv3jJ+b+uWflQXOFJL665I00XgHoUyFnOZMlVT+VaMRhclm1XHPjZarsnRepSrcBu Sf8kZLuCuB+EAm3ngNmd1z8yXq89Q6BwXkn/MeV9G3UeR74yHwQBJiS/4JMP2YT93lyV 8Rix5PPnvfNfu7p5u8DIJ7jluAamdFgWnnURZrjx5JXlzDMDLia6dLJb3+kM7J+t70O7 GwzO+C/GSG+HlJ5SV2lqJ7zQ4F/1Aa+SMSbjfZHjek+euC6Bda7Y6CaTZ2voOfrhRREk Q9cw== 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=aO24/XiDwSd/Fk1uFkg81bukhe4MKZMYLzvkDa7YUar2XYpPCRKXku53+nwgfJu07F sM9QpyQhredCQXiagW4AMjv+gCjJoD9bYH+1RrqWKxDZ84dFY4fWHRqEUX21wnVQhVGv OZ5ONDOj8bGlNCKhVhnfD6AVsOxvy2TX1oOOOrkp+uGgatM6VxiSPcB/LQI/Snu8Cpug Tdxd/ZYIIHZx/yOz/oquAPAaKKeuakhr3iW2HBf29SdZBZfYKO5hX+VSL3Ax+g9R2/iM sscbxm5edv9WaKY+23x7lIOWKJBkpcEhpUX10FP0PluIR+ZJP5H8A6Mwfsyvg497zJW3 sOIg== X-Forwarded-Encrypted: i=1; AL+hErFOBFRHOKY/UDtfJm1BUbgVwpuOB4pX11xIhsE+B3C8XF2nmUH5GIj5B8ItiaFtYQUQYTPqwRxD/uiKr6I=@vger.kernel.org X-Gm-Message-State: APuU4Z+V+adrjLJQJGcXwluY/tDcwpl1aqB0bCySr8yH/9RpResEdub/ A698FE8/0rwo3HNk6f25WYaNcLBawKSirVnvEZ3l9PDOJ0P7TOvKY3NS X-Gm-Gg: AZFdTZeAgxMmhpLf7TRhj2Hq7Fw9I7mLPdeIrpS5sweNjROPKG7KCkzS1gu2sfI91uL VSHo13djoCocLjln5/RbA0cAWuj9rtOkNX7qg5Ir4cjfBwVJt+5prlthDP4P5SMuVbdpBwaO/Nz 5PpTyGtoEmQ+mBVdb8cBbX8wnzO05iWHBM9+yvWtBuvtLNg6EcSCd0X4uX9UmodX7P2GuEbHg/3 MUuR2JehKUz1u4+GtoPD6hNhCESbxzgnTZx3uqayOp9xzabpzTjAWObctPrl6xj2aoPwnSKcsrA 74WTUmPmQJWaj7MA3EfUCg6bM8UGojvyvyJlPcu0dw5h4L8mkKKzRlRmJWffBQsw971YFA+vld3 kT2AtLDxuy3cP/0nm71CPjpp9GiQZRXxp0P3hZtChVsHNfI+wPufpp9/YTxq9DKIDCEy4CMIsvn thBxbHP+j+g4aXYNtHBYIW+Q4joa+PwvS82/SC63TyPm3S0EZlYwynGsS5senGHUIdvAoadj9sF rugmkGFRRg/YDvBhKJxfrzqDPNQJbhpMcFQXEW7raa+O5lowkSds4K/8JR9R8SHBole6VXfNNB0 MkBOBrGes/+kFQ/2ScvhMJOFf/a4vOpEyJg= 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: linux-kernel@vger.kernel.org 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