From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 867EEC6FD18 for ; Wed, 19 Apr 2023 09:39:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232279AbjDSJjk (ORCPT ); Wed, 19 Apr 2023 05:39:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230211AbjDSJjh (ORCPT ); Wed, 19 Apr 2023 05:39:37 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B8FE419BD for ; Wed, 19 Apr 2023 02:39:35 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4A1FE143D; Wed, 19 Apr 2023 02:40:19 -0700 (PDT) Received: from [10.57.20.93] (unknown [10.57.20.93]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7EB0D3F5A1; Wed, 19 Apr 2023 02:39:33 -0700 (PDT) Message-ID: Date: Wed, 19 Apr 2023 10:39:31 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH v2] drm/scheduler: set entity to NULL in drm_sched_entity_pop_job() Content-Language: en-GB To: Danilo Krummrich , luben.tuikov@amd.com, airlied@gmail.com, daniel@ffwll.ch, l.stach@pengutronix.de, christian.koenig@amd.com Cc: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org References: <20230418100453.4433-1-dakr@redhat.com> From: Steven Price In-Reply-To: <20230418100453.4433-1-dakr@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 18/04/2023 11:04, Danilo Krummrich wrote: > It already happend a few times that patches slipped through which > implemented access to an entity through a job that was already removed > from the entities queue. Since jobs and entities might have different > lifecycles, this can potentially cause UAF bugs. > > In order to make it obvious that a jobs entity pointer shouldn't be > accessed after drm_sched_entity_pop_job() was called successfully, set > the jobs entity pointer to NULL once the job is removed from the entity > queue. > > Moreover, debugging a potential NULL pointer dereference is way easier > than potentially corrupted memory through a UAF. > > Signed-off-by: Danilo Krummrich This triggers a splat for me (with Panfrost driver), the cause of which is the following code in drm_sched_get_cleanup_job(): if (job) { job->entity->elapsed_ns += ktime_to_ns( ktime_sub(job->s_fence->finished.timestamp, job->s_fence->scheduled.timestamp)); } which indeed is accessing entity after the job has been returned from drm_sched_entity_pop_job(). And obviously job->entity is a NULL pointer with this patch. I'm afraid I don't fully understand the lifecycle so I'm not sure if this is simply exposing an existing bug in drm_sched_get_cleanup_job() or if this commit needs to be reverted. Thanks, Steve > --- > drivers/gpu/drm/scheduler/sched_entity.c | 6 ++++++ > drivers/gpu/drm/scheduler/sched_main.c | 4 ++++ > 2 files changed, 10 insertions(+) > > diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c > index 15d04a0ec623..a9c6118e534b 100644 > --- a/drivers/gpu/drm/scheduler/sched_entity.c > +++ b/drivers/gpu/drm/scheduler/sched_entity.c > @@ -448,6 +448,12 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) > drm_sched_rq_update_fifo(entity, next->submit_ts); > } > > + /* Jobs and entities might have different lifecycles. Since we're > + * removing the job from the entities queue, set the jobs entity pointer > + * to NULL to prevent any future access of the entity through this job. > + */ > + sched_job->entity = NULL; > + > return sched_job; > } > > diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c > index 9b16480686f6..e89a3e469cd5 100644 > --- a/drivers/gpu/drm/scheduler/sched_main.c > +++ b/drivers/gpu/drm/scheduler/sched_main.c > @@ -42,6 +42,10 @@ > * the hardware. > * > * The jobs in a entity are always scheduled in the order that they were pushed. > + * > + * Note that once a job was taken from the entities queue and pushed to the > + * hardware, i.e. the pending queue, the entity must not be referenced anymore > + * through the jobs entity pointer. > */ > > #include