From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 218B3253F1F for ; Mon, 12 May 2025 07:33:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.151 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747035186; cv=none; b=B9GXmVRWyoKY/K0I7rz1VezRCey9Ee/v+RyRRF1OGCcO++t8xIO88MQz9XxNNOIIPxikV6KhUmsDmXLESBPLo8I011K4wMsb6xZ/AGdO7kk4R9M2H9jawRhTNFRdYfSbhJGyvd7HF/eSQHqwHYoJMCff9ezI0y5sAgG5KK+qqQ0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747035186; c=relaxed/simple; bh=MgUAJ+sEJUyyPDU8wMX6qVYqkHmu/ToCy1fkimOk138=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=Eojo4DHmy2tiUoohi3Ywj6YYjHHzSjtNRrS9OpB/Nn7VK71TTyhJi17VRORaQEYiWu8Zs0k7fT/Bg1dVF0bmooFWr6A6hPDhgwdJWMxoGIa7otLurBteUkXjd0/Dxn2hAPkTrMLmjQzW9mMPr1SsR2rfL5qngwuuXmknvixKG0g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mailbox.org; spf=pass smtp.mailfrom=mailbox.org; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b=o9JJ+kUZ; arc=none smtp.client-ip=80.241.56.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mailbox.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mailbox.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.b="o9JJ+kUZ" Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4ZwrtG0ClFz9spx; Mon, 12 May 2025 09:32:54 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1747035174; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=znH0XG7Kmtp8lN5ws28mSnlA5VbiwL23iVss3nnn1Jo=; b=o9JJ+kUZU8PHH7UBOjwTrtK7Q4hIMyITCfkHKi1P67T3dhBxYRgHrj3CiPP2O5FzMLRDfi xFR+lYeWEZfpcy/CTnhOt7we6AGH+1E1D/7Rg1FusaptD7//2Zelbxdsp/40CPOzIDxcK+ bdbvM+cyzW75afo5bwYE2XZrhd9byRjVozdk18Vpeljj3XkDqWr1RTHxe4cacS+cFaA8T6 FtobJHQFty09Lep4xmvQRep48Xku6D/fquC5XAsToNM9+VEOuHp0ASG9vd6PyvMxHfnR8v 6cb6Wvr0Q1a1HDKaeHvsjCRxp7dfpNqQXbKhtoyt7wnrrNG/huvOul8dK78Ung== Message-ID: <7d1d03702f4dffd68b18afa4f5ef242b4042188f.camel@mailbox.org> Subject: Re: [PATCH] drm/sched: Fix UAF in drm_sched_fence_get_timeline_name() From: Philipp Stanner Reply-To: phasta@kernel.org To: Rob Clark , dri-devel@lists.freedesktop.org Cc: Rob Clark , Matthew Brost , Danilo Krummrich , Philipp Stanner , Christian =?ISO-8859-1?Q?K=F6nig?= , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , open list Date: Mon, 12 May 2025 09:32:49 +0200 In-Reply-To: <20250509212936.490048-1-robdclark@gmail.com> References: <20250509212936.490048-1-robdclark@gmail.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MBO-RS-ID: 2eac22a50541d63a9ad X-MBO-RS-META: fagip3zimdb6a6bhbm8so988516ptigt Hi, On Fri, 2025-05-09 at 14:29 -0700, Rob Clark wrote: > From: Rob Clark >=20 > The fence can outlive the sched, so it is not safe to dereference the > sched in drm_sched_fence_get_timeline_name() Thx for the fix. Looks correct to me. Some nits >=20 > Signed-off-by: Rob Clark This is clearly a bug. So please provide a Fixes: tag and +Cc the stable kernel. > --- > =C2=A0drivers/gpu/drm/scheduler/sched_fence.c |=C2=A0 3 ++- > =C2=A0include/drm/gpu_scheduler.h=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 11 +++++++++++ > =C2=A02 files changed, 13 insertions(+), 1 deletion(-) >=20 > diff --git a/drivers/gpu/drm/scheduler/sched_fence.c > b/drivers/gpu/drm/scheduler/sched_fence.c > index e971528504a5..4e529c3ba6d4 100644 > --- a/drivers/gpu/drm/scheduler/sched_fence.c > +++ b/drivers/gpu/drm/scheduler/sched_fence.c > @@ -92,7 +92,7 @@ static const char > *drm_sched_fence_get_driver_name(struct dma_fence *fence) > =C2=A0static const char *drm_sched_fence_get_timeline_name(struct > dma_fence *f) > =C2=A0{ > =C2=A0 struct drm_sched_fence *fence =3D to_drm_sched_fence(f); > - return (const char *)fence->sched->name; > + return fence->name; Adding an empty line while we're here already would be neat. > =C2=A0} > =C2=A0 > =C2=A0static void drm_sched_fence_free_rcu(struct rcu_head *rcu) > @@ -226,6 +226,7 @@ void drm_sched_fence_init(struct drm_sched_fence > *fence, > =C2=A0 unsigned seq; > =C2=A0 > =C2=A0 fence->sched =3D entity->rq->sched; > + fence->name=C2=A0 =3D fence->sched->name; > =C2=A0 seq =3D atomic_inc_return(&entity->fence_seq); > =C2=A0 dma_fence_init(&fence->scheduled, > &drm_sched_fence_ops_scheduled, > =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 &fence->lock, entity->fence_= context, seq); > diff --git a/include/drm/gpu_scheduler.h > b/include/drm/gpu_scheduler.h > index 0ae108f6fcaf..d830ffe083f1 100644 > --- a/include/drm/gpu_scheduler.h > +++ b/include/drm/gpu_scheduler.h > @@ -295,6 +295,9 @@ struct drm_sched_fence { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /** > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * @sched: the sche= duler instance to which the job having > this struct > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * belongs to. > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * Some care must be tak= en as to where the sched is derefed, > as the > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * fence can outlive the= sched. > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 */ Just briefly hinting at the lifetime is enough here. Every developer understands what that implicates. "Might have a lifetime shorter than the owning &struct drm_sched_fence" Thx P. > =C2=A0 struct drm_gpu_scheduler *sched; > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /** > @@ -305,6 +308,14 @@ struct drm_sched_fence { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * @owner: job owne= r for debugging > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 */ > =C2=A0 void *owner; > + > + /** > + * @name: the timeline name > + * > + * This comes from the @sched, but since the fence can > outlive the > + * sched, we need to keep our own copy. > + */ > + const char *name; > =C2=A0}; > =C2=A0 > =C2=A0struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f);