From nobody Wed Dec 17 17:23:48 2025 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 B6251C77B75 for ; Tue, 18 Apr 2023 10:05:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229682AbjDRKFs (ORCPT ); Tue, 18 Apr 2023 06:05:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229689AbjDRKFq (ORCPT ); Tue, 18 Apr 2023 06:05:46 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 892A7448B for ; Tue, 18 Apr 2023 03:05:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681812299; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=kkPmtUJM/y/8ZMLgmPzrDpSz/aD5s4kTz4AWJ+kwyns=; b=FXy+hhuLkrzXT8MprYeWrK1kuYnjpSdY5338FXwFYs/xooNOaGvQryLazxiwwUluyW/dAp y8oWFZVbX/ps3mstfeAkPqAT4qEHH8pIEsrKFAIwzvNjDkGW7VwVQy5pIdkHiORDxlmjtD nx/a5mTKsc9X847aq/MLvIR5W6okLVw= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-374-a2_MzuasOIy1lf15vIdRRQ-1; Tue, 18 Apr 2023 06:04:58 -0400 X-MC-Unique: a2_MzuasOIy1lf15vIdRRQ-1 Received: by mail-ej1-f70.google.com with SMTP id qk25-20020a170906d9d900b0094f0dd1166dso3026651ejb.9 for ; Tue, 18 Apr 2023 03:04:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681812297; x=1684404297; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kkPmtUJM/y/8ZMLgmPzrDpSz/aD5s4kTz4AWJ+kwyns=; b=AqP7v95T9HGFMfcZeWJbBBy4HOYydO8hpSmrKmww/2h//en/OQ4DQloTbY6+wITNGD nd0EgPXBK3v2oC9BZUmQsmnBx7VpdJUZksftZryUP2KdwajIu/jHw8Vgd4db+Hp06o5h adBaEDVyjG9zcBdTb69/cygaJQAhREwKB5jihgsnZkaYecPJpssQQgVmPuiH0hUOY6yv iyH/Rqrmrdpv6bCcH9TysOOmacoX9g7JFcPjOWdwdUsLplv3VGlywLv2nImaU+jlQkLO m92xzlsuoNQFnPhJbijUaIcHLnQKdJJBFmlvBfDxbtXeruzs/se+6sClhdFLbGfGdCEm H4Vw== X-Gm-Message-State: AAQBX9eldRRKASOhe2tA72JHQNWZtjQ8CZX0bk2KhGVh05lzCVaPj7ZI fLmbg56TV4biPLnM29zh93g7zeZqlbQF+4v38+Rur/36Ls47PW4YOX9n34PmYhav60LNbYJt453 WC5jS1QWTe2HJWMpF/pruCgXY X-Received: by 2002:a17:906:f6cd:b0:94f:3bf7:dacf with SMTP id jo13-20020a170906f6cd00b0094f3bf7dacfmr7523192ejb.71.1681812297377; Tue, 18 Apr 2023 03:04:57 -0700 (PDT) X-Google-Smtp-Source: AKy350a+XHfJpZZVIUYU09ecLUdbAZs0U1PMVcoHiowslaeLzqa8MByKR7gSpV0az5yz9+7T0p4Ipg== X-Received: by 2002:a17:906:f6cd:b0:94f:3bf7:dacf with SMTP id jo13-20020a170906f6cd00b0094f3bf7dacfmr7523179ejb.71.1681812297083; Tue, 18 Apr 2023 03:04:57 -0700 (PDT) Received: from cassiopeiae.. ([2a02:810d:4b3f:de78:642:1aff:fe31:a19f]) by smtp.gmail.com with ESMTPSA id q18-20020a170906361200b0094f124a37c4sm5131590ejb.18.2023.04.18.03.04.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Apr 2023 03:04:56 -0700 (PDT) From: Danilo Krummrich To: luben.tuikov@amd.com, airlied@gmail.com, daniel@ffwll.ch, l.stach@pengutronix.de, christian.koenig@amd.com Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2] drm/scheduler: set entity to NULL in drm_sched_entity_pop_job() Date: Tue, 18 Apr 2023 12:04:53 +0200 Message-Id: <20230418100453.4433-1-dakr@redhat.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Luben Tuikov --- 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/sch= eduler/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); } =20 + /* 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 =3D NULL; + return sched_job; } =20 diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/sched= uler/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 p= ushed. + * + * 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 any= more + * through the jobs entity pointer. */ =20 #include --=20 2.39.2