OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
From: Mirian Shilakadze <mirian.shilakadze@virtuozzo.com>
To: khorenko@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2 2/4] ms/gpu/buddy: Fix use-after-free in split_block() call sites
Date: Wed, 18 Nov 2026 15:38:11 +0400	[thread overview]
Message-ID: <20261118113935.381228-3-mirian.shilakadze@virtuozzo.com> (raw)
In-Reply-To: <20261118113935.381228-1-mirian.shilakadze@virtuozzo.com>

From: Francois Dugast <francois.dugast@intel.com>

When split_block() fails it returns before calling mark_split(), leaving
the block in the FREE state and still linked in the rbtree.  The four
err_undo paths then call __gpu_buddy_free() without first removing the
block from the tree, which leads to two distinct bugs:

 - If the buddy is also free, __gpu_buddy_free() merges the two siblings
   by calling gpu_block_free(mm, block) while block->rb is still linked
   in the tree.  Any subsequent rbtree traversal will follow the now-
   dangling pointer, causing a use-after-free.

 - In alloc_from_freetree(), where there is no buddy guard,
   __gpu_buddy_free() always reaches mark_free() -> rbtree_insert() with
   block still in the tree, corrupting the rbtree.

The same pattern is already used correctly in __force_merge(): call
rbtree_remove() to unlink the block before handing it to
__gpu_buddy_free().  Apply the same fix to all four err_undo sites.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Assisted-by: GitHub Copilot:claude-sonnet-4.6
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Link: https://lore.kernel.org/r/20260522092600.32818-2-francois.dugast@intel.com
Signed-off-by: Francois Dugast <francois.dugast@intel.com>
(cherry picked from commit 0251963afd22d1365d07bf81135ef0c694f6c3b5)

Context adjusted: the allocator is drivers/gpu/drm/drm_buddy.c with drm_
names here, and only three of the four err_undo sites exist (there is
no offset aligned allocation function).

https://virtuozzo.atlassian.net/browse/VSTOR-155318
Feature: fix ms/drm

Signed-off-by: Mirian Shilakadze <mirian.shilakadze@virtuozzo.com>
---
 drivers/gpu/drm/drm_buddy.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c
index 640d93070bb7..87c65923259f 100644
--- a/drivers/gpu/drm/drm_buddy.c
+++ b/drivers/gpu/drm/drm_buddy.c
@@ -684,8 +684,10 @@ __alloc_range_bias(struct drm_buddy *mm,
 	buddy = __get_buddy(block);
 	if (buddy &&
 	    (drm_buddy_block_is_free(block) &&
-	     drm_buddy_block_is_free(buddy)))
+	     drm_buddy_block_is_free(buddy))) {
+		rbtree_remove(mm, block);
 		__drm_buddy_free(mm, block, false);
+	}
 	return ERR_PTR(err);
 }
 
@@ -794,8 +796,10 @@ alloc_from_freetree(struct drm_buddy *mm,
 	return block;
 
 err_undo:
-	if (tmp != order)
+	if (tmp != order) {
+		rbtree_remove(mm, block);
 		__drm_buddy_free(mm, block, false);
+	}
 	return ERR_PTR(err);
 }
 
@@ -880,8 +884,10 @@ static int __alloc_range(struct drm_buddy *mm,
 	buddy = __get_buddy(block);
 	if (buddy &&
 	    (drm_buddy_block_is_free(block) &&
-	     drm_buddy_block_is_free(buddy)))
+	     drm_buddy_block_is_free(buddy))) {
+		rbtree_remove(mm, block);
 		__drm_buddy_free(mm, block, false);
+	}
 
 err_free:
 	if (err == -ENOSPC && total_allocated_on_err) {
-- 
2.47.3

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

  parent reply	other threads:[~2026-11-18 11:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 11:38 [Devel] [PATCH hci-8.2 0/4] drm/buddy: backport buddy allocator fixes Mirian Shilakadze
2026-11-18 11:38 ` [Devel] [PATCH hci-8.2 1/4] ms/drm/buddy: Prevent BUG_ON by validating rounded allocation Mirian Shilakadze
2026-11-18 11:43   ` no-reply
2026-11-18 11:38 ` Mirian Shilakadze [this message]
2026-11-18 11:43   ` [Devel] [PATCH hci-8.2 2/4] ms/gpu/buddy: Fix use-after-free in split_block() call sites no-reply
2026-11-18 11:38 ` [Devel] [PATCH hci-8.2 3/4] ms/gpu/buddy: bail out of try_harder when alignment cannot be honoured Mirian Shilakadze
2026-11-18 11:47   ` no-reply
2026-11-18 11:38 ` [Devel] [PATCH hci-8.2 4/4] ms/gpu/buddy: do not pass caller list to __alloc_contig_try_harder() Mirian Shilakadze
2026-11-18 11:45   ` no-reply
2026-11-18 15:37 ` [Devel] [PATCH hci-8.2 0/4] drm/buddy: backport buddy allocator fixes Konstantin Khorenko
2026-11-18 15:37 ` Konstantin Khorenko

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=20261118113935.381228-3-mirian.shilakadze@virtuozzo.com \
    --to=mirian.shilakadze@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=khorenko@virtuozzo.com \
    /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