From: Yuansheng Yang <0xbeac@gmail.com>
To: Konstantin Sinyuk <ksinyuk@kernel.org>,
Koby Elbaz <elbaz.koby@gmail.com>,
Oded Gabbay <ogabbay@kernel.org>
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: [PATCH] accel/habanalabs: fix double-free in vm_ctx_init_with_ranges()
Date: Sat, 5 Dec 2026 18:00:58 +0800 [thread overview]
Message-ID: <20261205100058.1569600-1-0xbeac@gmail.com> (raw)
vm_ctx_init_with_ranges() allocates all HL_VA_RANGE_TYPE_MAX entries of
ctx->va_range[]. When !hdev->pmmu_huge_range (Goya and Gaudi), it frees
ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE] and aliases it to
ctx->va_range[HL_VA_RANGE_TYPE_HOST].
If va_range_init() subsequently fails for HL_VA_RANGE_TYPE_DRAM (for
example due to -ENOMEM in add_va_block_locked() during hl_device_open()
context creation, or -EFAULT), cleanup jumps to clear_host_huge_va_range
and falls through to free_va_range:, where the loop calls kfree() on
every slot in ctx->va_range[], freeing the HL_VA_RANGE_TYPE_HOST object
twice:
BUG: KASAN: double-free in hl_vm_ctx_init
Call Trace:
kfree
hl_vm_ctx_init
If ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE] aliases
ctx->va_range[HL_VA_RANGE_TYPE_HOST], clear it to NULL before the
free_va_range loop. Checking pointer equality rather than
!hdev->pmmu_huge_range avoids leaking the separately allocated
HL_VA_RANGE_TYPE_HOST_HUGE entry on earlier hl_mmu_ctx_init() or
HL_VA_RANGE_TYPE_HOST failures before aliasing occurs.
Fixes: 64a7e2955d9a ("habanalabs: split the host MMU properties")
Cc: stable@vger.kernel.org
Signed-off-by: Yuansheng Yang <0xbeac@gmail.com>
---
Compiled drivers/accel/habanalabs/common/memory.o with W=1 on x86_64
(0 warnings, 0 errors) and verified on v7.4-rc5 in an x86_64 KASAN
QEMU guest via hl_vm_ctx_init() with !hdev->pmmu_huge_range and a
failing DRAM va_range_init(): before this fix, KASAN reports a
double-free in hl_vm_ctx_init(); after this fix, 10/10 iterations
unwind cleanly with 0 KASAN, BUG, or WARNING diagnostics.
drivers/accel/habanalabs/common/memory.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/accel/habanalabs/common/memory.c b/drivers/accel/habanalabs/common/memory.c
index 2f76776b4..d90c8bbfb 100644
--- a/drivers/accel/habanalabs/common/memory.c
+++ b/drivers/accel/habanalabs/common/memory.c
@@ -2686,6 +2686,9 @@ mmu_ctx_fini:
mutex_destroy(&ctx->mem_hash_lock);
hl_mmu_ctx_fini(ctx);
free_va_range:
+ if (ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE] ==
+ ctx->va_range[HL_VA_RANGE_TYPE_HOST])
+ ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE] = NULL;
for (i = 0 ; i < HL_VA_RANGE_TYPE_MAX ; i++)
kfree(ctx->va_range[i]);
--
2.53.0
reply other threads:[~2026-12-05 10:01 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205100058.1569600-1-0xbeac@gmail.com \
--to=0xbeac@gmail.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=elbaz.koby@gmail.com \
--cc=ksinyuk@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ogabbay@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®