From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) (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 377EC258EE9 for ; Tue, 16 Jun 2026 03:42:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=209.85.208.173 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781581345; cv=pass; b=UT66K39K/0wzpqCbncIwYEXuUvMS0nfvyc+cMvNqG8MovAJw7wVAztXHGxxg+spMk6KOsWPpDLIgr6GUHd33+w8H3e3LwSt/jjJowKjsJJHzWDvVof/q+4v5aOy/kUXzxNhem27W80TRC1cKDxiReraPyYNh1XOXxOVOGTtkCVc= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781581345; c=relaxed/simple; bh=MSBWYbSsC+ptuLs9u8xQS5Sj8CYtZkMnZNFejaHPPRk=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=uuZLVI5xy+J15XL9J1i3E55gqX2KQ5f9ULiRBieXynAU+c36MWiYJ1FVFtF+6LCJruHGR9sfHBmoe50n+1bL0KQXgJLPE1q5jaIcK7BkXfUNzRxcvWBKmUiASEETQ8toKZ7WOFLawlIMzGQNRxDgzL5WlglcO2lxGxezmG4e/qw= ARC-Authentication-Results:i=2; 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=gxK2lSAf; arc=pass smtp.client-ip=209.85.208.173 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="gxK2lSAf" Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-39676ff4674so35436221fa.0 for ; Mon, 15 Jun 2026 20:42:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1781581342; cv=none; d=google.com; s=arc-20240605; b=X9izH594QXHgSE0W82+9pZ8iv7z1YjVFr/yEEqEtUvi0BffsmdC9sfwX7n/inNeyR7 cPcNiJPI03fihLOMqsZwTumLMUSRdoHw48hN7N1sjIp1/EnlZRo+go1smQzZf0ld4s1v CotixYzlEp6hEyoQhiBLpfxlyDgEliicfMEUorHb7rOazE4lPYHUaaNFp4P/ElCpe2M5 6k1d8eEhda017McP3ahJB/CAZVjES6fc+fVET542imQ2e2Gy5r+W9/I/xHgQA6D6uk1L AEn2Xr3Vy9Wffdyb3ryLYlP2vY7SVAShwVhRwq1Bz59ZhBPRlRseEJwnQipXkXT7pS8A K4mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=6QkO242zjqJOUCCQGA9Iv8kbh0YooY0Noa0v7iQvj0Q=; fh=DH29vxpr+cMCO8fvgqFKhi1dITK0FSFR3QfmG/nAkmI=; b=XsUzCa6d9+shOoHwDltgmhTIK4ReIMdOkLGXe4RhOlAsiq9eTi7gb+v+x2Q8x1dWaf AC20VQhrjT5lcQa1R8SRApcSEygR3bv0OGsIrnyVGakjiPH8z6L56BAzGE+dvzuDiA8i 8hnsDEo2qiDraOno6rXlpqrgvX+Y+IqgJ9hVOIyDKOkuPq2W/ZTiamCCPIZnGaUDOUfp 40Fw/tB3BvLgRgwcm2TwjksZBqabenhUTEK7IggzJBlyE0ZTfGlRnFSyNYrrVUtO3MEP kHCSq2xEvJn3ynroxG21C86UkA8FQ+k54oH+C1ZiICKgvR3kVYN6lXJLxByPcgHDDjdx buzg==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781581342; x=1782186142; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=6QkO242zjqJOUCCQGA9Iv8kbh0YooY0Noa0v7iQvj0Q=; b=gxK2lSAfUasIYgx9M037/p4Su4HLVw3rTTRaQa+at5hdZJkzpdO9B/B73GV/+HRSXx 9I0eVdK+dCaaphBJtwu8h9LDBgjk0bODtFeUvs//uurshY0ZChW8sD2LauJS/2M1pkaF SxW3oypf3VUpCFg4wYNdt8AeMfOf2gFI0W5rXK1W0xpcKVIhYXCCB4yaw2OIb5Kdr4V2 bTqpZakYiqwDES/w6xWrxoI0uglP2T01rSaesNqsCsikj/FQ9LM7ZjsLEm/SEUIkAKW7 3Kt+0CfTWqiBKRSNbudON3GXuZtiLHmduEx2jAb9VRvDxRC7Sz+2tIwadnybdk3O7wGl bR7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781581342; x=1782186142; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=6QkO242zjqJOUCCQGA9Iv8kbh0YooY0Noa0v7iQvj0Q=; b=SS1SP7UBnQSioHHLowiLqgLsKcVHbCGP82Y3+EV5GzLOXBo6kfXmnIdSYg70i52xhF 55NJZQNSqEqGUcSRZOvXRdqLX9YEzZTmVIJut/Yla9PmX49DC/uQPPcDvQ4C40PsBasR 8j78jqd9FNxNbVyyTFlXa71iDp/A1eP1zOz+J80LbxG4n6LI2IT52+NZczKNTNBEX/ng VnsPVXpz/EVLspqKCVp+Sm2fuEwCDToz6aX3Q7pYItPyueD3Dj3Zf41JiqeSTt6QPU6C z7dPRKAxvJW1r0RKP49gKjRVKfpTWzurV/KBxX4bC+wtVTMbk1BmTSYGwhKvAx8atdyh peDQ== X-Forwarded-Encrypted: i=1; AFNElJ8NVissdMtzydne5vy94U1uss/CGTYgdzO4wcBacz5+JMCNjBLaejyRRQVUVzZUjkb1/UQBq2dJVqeUZNg=@vger.kernel.org X-Gm-Message-State: AOJu0YwJMZJvfUft3WG3YGBZQPDR4Ja9VOV0lzyzOH0Y6NAZ8ZQ0HKgw gHMKHF9l5QI9JxOmYcGsPu0wQayysOEhM3ldA96LhtQ0xsN2tfnMTM8E2DxpI0ASI/lK2Qd19+H qQk2PPXQZKz4l+4cwzOwOeVJU4Ib1LLM= X-Gm-Gg: Acq92OGnunN/d6Ws1ImGYDjMaoZuBKYFngN4CmhNkJ8xAHfXITGAFyQqwwNsq4HR2vy 3O0v5O+TOOokzQ57HxZ2KKZGyQVawjGZ7ezawxPdi9fszgxudai3zoq79SYW4RKXjXX90TyCKWx 08PzmryTiGdkl7YVYLUYckTfX/dn9+SYxUfWieCg+alxVFAknvg8QeGdgakWAkyZy2AimU2WAyL o6xVhIdFRtkoxD1pj4LmJE0EZkvBBEVGRZfP/teXtl2o8UR3/xfMcQBBESkRPuPyND9k0A33bMl prwV46ev+omcvzU23j8uwr9uY6U6mYliiIBVWK074CVtjMw= X-Received: by 2002:a2e:bd8a:0:b0:393:a7d7:7e84 with SMTP id 38308e7fff4ca-39935773dd6mr36326101fa.24.1781581342125; Mon, 15 Jun 2026 20:42:22 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20260615234922.151263-1-skainsworth@gmail.com> In-Reply-To: From: Samuel Ainsworth Date: Tue, 16 Jun 2026 03:41:44 +0000 X-Gm-Features: AVVi8CeqzzoRgnheQTFzthQuFy62FqPdkdsu_fToWpW-W3S4M8fVlIw4YMeHeVs Message-ID: Subject: Re: [PATCH v1 0/2] drm/ttm: fix bulk_move cursor use-after-free for unevictable resources To: =?UTF-8?Q?Christian_K=C3=B6nig?= , Huang Rui Cc: =?UTF-8?Q?Thomas_Hellstr=C3=B6m?= , Matthew Auld , Matthew Brost , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Welp, I spoke too soon. v1 is broken. While stress-testing the fix on the affected machine (KASAN + DEBUG_LIST, ordinary desktop use rather than only hibernate cycles), I hit a new use-after-free that this patch introduces: BUG: KASAN: slab-use-after-free in ttm_lru_bulk_move_tail+0xa93/0xcb0 [tt= m] Read of size 8 ... by task .swayosd-server ttm_lru_bulk_move_tail+0xa93/0xcb0 [ttm] amdgpu_vm_move_to_lru_tail+0x29/0x40 [amdgpu] amdgpu_cs_ioctl+0x4247/0x5090 [amdgpu] Freed by: ttm_resource_free+0x1f8/0x390 [ttm] ttm_bo_delayed_delete+0x69/0x100 [ttm] By dropping the ttm_resource_unevictable() gate from ttm_resource_del_bulk_move() and removing any tracked resource, the del now= runs for resources that have become unevictable. ttm_lru_bulk_move_del() updates pos->first/pos->last by walking the manager LRU (ttm_lru_next_res()/ttm_lru_prev_res()), but an unevictable resource's lru.= link has been moved to bdev->unevictable, so the walk lands on an unrelated reso= urce and leaves the cursor pointing outside the bulk-move range. That resource i= s freed later, and the next ttm_lru_bulk_move_tail() dereferences the danglin= g cursor. IIUC the resource needs to be taken off the cursor at the point it becomes unevictable (while its link is still on the manager LRU), not deferred to free/teardown. The kunit tests passed only because they used a single-eleme= nt cursor, where ttm_lru_bulk_move_del() clears pos without walking; they did not cover the multi-element / moved-link case that this UAF needs. I am not 100% convinced I have this pinned down yet, but I believe I may be making progress. Feedback welcome! I'll try to follow up with a v2 that fixes it at the unevictable transition= and adds tests that actually exercise the multi-element case. Apologies for the confusion. Best, Sam On Tue, Jun 16, 2026 at 12:46=E2=80=AFAM Samuel Ainsworth wrote: > > Welp, I spoke too soon. v1 is broken. > > While stress-testing the fix on the affected machine (KASAN + DEBUG_LIST, > ordinary desktop use rather than only hibernate cycles), I hit a new > use-after-free that this patch introduces: > > BUG: KASAN: slab-use-after-free in ttm_lru_bulk_move_tail+0xa93/0xcb0 [= ttm] > Read of size 8 ... by task .swayosd-server > ttm_lru_bulk_move_tail+0xa93/0xcb0 [ttm] > amdgpu_vm_move_to_lru_tail+0x29/0x40 [amdgpu] > amdgpu_cs_ioctl+0x4247/0x5090 [amdgpu] > Freed by: > ttm_resource_free+0x1f8/0x390 [ttm] > ttm_bo_delayed_delete+0x69/0x100 [ttm] > > By dropping the ttm_resource_unevictable() gate from > ttm_resource_del_bulk_move() and removing any tracked resource, the del n= ow runs > for resources that have become unevictable. ttm_lru_bulk_move_del() updat= es > pos->first/pos->last by walking the manager LRU > (ttm_lru_next_res()/ttm_lru_prev_res()), but an unevictable resource's lr= u.link > has been moved to bdev->unevictable, so the walk lands on an unrelated re= source > and leaves the cursor pointing outside the bulk-move range. That resource= is > freed later, and the next ttm_lru_bulk_move_tail() dereferences the dangl= ing > cursor. > > IIUC the resource needs to be taken off the cursor at the point it become= s > unevictable (while its link is still on the manager LRU), not deferred to > free/teardown. The kunit tests passed only because they used a single-ele= ment > cursor, where ttm_lru_bulk_move_del() clears pos without walking; they di= d > not cover the multi-element / moved-link case that this UAF needs. > > I am not 100% convinced I have this pinned down yet, but I believe I may = be > making progress. Feedback welcome! > > I'll try to follow up with a v2 that fixes it at the unevictable transiti= on and > adds tests that actually exercise the multi-element case. Apologies for t= he > confusion. > > Best, > Sam > > On Mon, Jun 15, 2026 at 11:49=E2=80=AFPM Samuel Ainsworth wrote: >> >> A resource added to a bo's bulk_move LRU cursor can become unevictable >> (pinned or swapped) after it has been added. ttm_resource_del_bulk_move(= ) >> then skips removing it -- both on free and on ttm_bo_set_bulk_move() dur= ing >> bo teardown -- because the resource is unevictable, leaving the cursor's >> pos->first/pos->last pointing at it. Once freed, the next allocation on = that >> bulk_move dereferences the dangling cursor (use-after-free) and corrupts= the >> LRU list, which CONFIG_DEBUG_LIST turns into a fatal BUG. >> >> In the field this is a hibernation-triggered panic on a Framework 13 (AM= D >> Ryzen 7040): a buffer swapped out during hibernate is closed after resum= e >> (amdgpu_gem_object_close -> amdgpu_vm_bo_del -> ttm_bo_set_bulk_move()), >> which leaves its unevictable resource on the VM's bulk_move cursor; a la= ter >> GEM allocation on that cursor then faults (drm/amd issue #5387). >> >> Patch 1 tracks cursor membership explicitly so the del always undoes the >> add, regardless of any pin/swap transition. Patch 2 adds kunit regressio= n >> coverage. >> >> Validating the bug (no GPU required, patch 2): >> >> - ttm_bo_bulk_move_swapped_free_dangles allocates a resource on a >> bulk_move cursor, swaps out its bo's ttm so the resource becomes >> unevictable, frees it, and asserts the cursor no longer references th= e >> freed resource. Without the fix this fails: pos->first/pos->last stil= l >> equal the freed pointer. >> >> - ttm_bo_bulk_move_dangling_corrupts then allocates on the same >> bulk_move; without the fix, KASAN reports a slab-use-after-free in >> ttm_resource_add_bulk_move(). >> >> On the affected machine, a throwaway debug kernel that WARN_ONCE()s when >> ttm_resource_del_bulk_move() skips an unevictable resource still on the >> cursor was used to test. It fired during a normal hibernate/resume cycle= , via >> amdgpu_gem_object_close() -> amdgpu_vm_bo_del() -> ttm_bo_set_bulk_move(= ). >> That confirmed the production trigger and that the planted state matches >> the field crash (the ttm_resource.c WARN_ON, then list_del corruption). >> >> With the patch 1 fix, both kunit tests pass and the full TTM kunit suite= is >> green (no KASAN report, no CONFIG_DEBUG_LIST splat). Furthermore, I set = up a >> kernel with the fix, built with KASAN + CONFIG_DEBUG_LIST + lockdep, and= ran 10 >> hibernate/resume/GEM-close cycles under GPU load with no use-after-free = nor LRU >> list corruption. >> >> I am new to this area, so review of the approach is very welcome -- in >> particular whether tracking membership on the resource is preferrable vs >> removing it from the cursor at the point it becomes unevictable. >> >> Samuel Ainsworth (2): >> drm/ttm: don't leave bulk_move cursor dangling for unevictable >> resources >> drm/ttm/tests: add bulk_move cursor regression tests >> >> drivers/gpu/drm/ttm/tests/ttm_bo_test.c | 163 ++++++++++++++++++++++++ >> drivers/gpu/drm/ttm/ttm_resource.c | 18 ++- >> include/drm/ttm/ttm_resource.h | 9 ++ >> 3 files changed, 187 insertions(+), 3 deletions(-) >> >> >> base-commit: 2c7d5b0a5ec0fc713a7f350806553643e87e6f43 >> -- >> 2.54.0