Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp793881ybg; Sun, 26 Jul 2020 22:44:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJOE/Mm8LUYI86rkWvSSnG3rk7ZObITU4crCZYrXmVX5WYZWAgq0/OqJ2IvmLyfZJ9lzHA X-Received: by 2002:a17:906:b353:: with SMTP id cd19mr11049750ejb.395.1595828656500; Sun, 26 Jul 2020 22:44:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595828656; cv=none; d=google.com; s=arc-20160816; b=s94HviqAtI6Eqe/Df+72kguDu8xP8wzPy/hW8YZq96uPbd903S5XPxh1A5pJ9QjGAD pYXvIunSPA2u0Dx8K2B2P6tLRef5lYqQShnugjgGlwQiuWM7XVSXdrC1g5J/e4CFDp65 CJbn8mxYpP9CgTR9x3Vq8m+gWXEQVS0V6yiM/0yYLZvNey7oc0L1DNOV0fLe7y5fEYKn AOHVnwtvDWEBbBudV4e1ROrt8/NHnwnYt8jtCcwVuMT3OhIBHmHVrGu46x5RciEAwyYh Fx7ql0CieZ0Ewocdd3sww7EhOyZ/kA0kVu8CPPgSpbULZU1EB8gQPcY+JwNdjAB4ibcV JeFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:subject:reply-to:cc:from:to:dkim-signature:date; bh=f9E9zZQz6TG7mCxptgTH09Y9wak5F4Jj1SYOm0YNufQ=; b=xVx5AWatk6ucrmRrh722CZynLm2hsEpWXPVI25k5AQCdVFjsGhZbPz/pyB2r1VIigb YVbIHI5ETpVfRXCjYohPFJE5hlqzDseUdxwMA4U24pfTpnO1MrCP1ixan4KrZfBTiOUj CehWc82XddESWMioqJ0E1ypS7XmHoYpKIAH0ufg3D8dcuSGopGNrMHIFLxohSUWVYeDg KUQg6wbp6CMH90XiV7Wq7/YJ7U956RH4UYnY0YPhZmcR3XQ68uPKYvlO522SkAiPep6h on6hio+ccN1YBciwN3JGlcJ+J/f2f/0kzoRDJVLgng1N1KvmJtrCEqeZIY/P5BOIIJ++ pUFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail header.b=Int6NshK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v6si673442ejr.682.2020.07.26.22.43.54; Sun, 26 Jul 2020 22:44:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail header.b=Int6NshK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726387AbgG0Fk5 (ORCPT + 99 others); Mon, 27 Jul 2020 01:40:57 -0400 Received: from mail4.protonmail.ch ([185.70.40.27]:15455 "EHLO mail4.protonmail.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726313AbgG0Fk4 (ORCPT ); Mon, 27 Jul 2020 01:40:56 -0400 Date: Mon, 27 Jul 2020 05:40:46 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail; t=1595828452; bh=f9E9zZQz6TG7mCxptgTH09Y9wak5F4Jj1SYOm0YNufQ=; h=Date:To:From:Cc:Reply-To:Subject:From; b=Int6NshKrx1Ffc8MyTRH9fefJnr1kVBjUJWpHQdx46ak6d8wSiiMTanKhTGRZnX7G frVWJoGPVO3m258gffeEbskhsffc4E24gz305kTL0piiDDhEGxr0j3saXzoLRJMP+O XwztT1DUGAXlX41dPvlHcn9M6Rt8Al/vqzodxUaw= To: "linux-kernel@vger.kernel.org" , "amd-gfx@lists.freedesktop.org" , "dri-devel@lists.freedesktop.org" From: Mazin Rezk Cc: Andrew Morton , =?utf-8?Q?Christian_K=C3=B6nig?= , Harry Wentland , Mazin Rezk , "Kazlauskas, Nicholas" , "sunpeng.li@amd.com" , Kees Cook , Alexander Deucher , Duncan <1i5t5.duncan@cox.net>, "mphantomx@yahoo.com.br" , "regressions@leemhuis.info" , "anthony.ruhier@gmail.com" , Paul Menzel Reply-To: Mazin Rezk Subject: [PATCH] drm/amd/display: Clear dm_state for fast updates Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.2 required=7.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HK_RANDOM_REPLYTO shortcircuit=no autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on mail.protonmail.ch Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch fixes a race condition that causes a use-after-free during amdgpu_dm_atomic_commit_tail. This can occur when 2 non-blocking commits are requested and the second one finishes before the first. Essentially, this bug occurs when the following sequence of events happens: 1. Non-blocking commit #1 is requested w/ a new dm_state #1 and is deferred to the workqueue. 2. Non-blocking commit #2 is requested w/ a new dm_state #2 and is deferred to the workqueue. 3. Commit #2 starts before commit #1, dm_state #1 is used in the commit_tail and commit #2 completes, freeing dm_state #1. 4. Commit #1 starts after commit #2 completes, uses the freed dm_state 1 and dereferences a freelist pointer while setting the context. Since this bug has only been spotted with fast commits, this patch fixes the bug by clearing the dm_state instead of using the old dc_state for fast updates. In addition, since dm_state is only used for its dc_state and amdgpu_dm_atomic_commit_tail will retain the dc_state if none is found, removing the dm_state should not have any consequences in fast updates. This use-after-free bug has existed for a while now, but only caused a noticeable issue starting from 5.7-rc1 due to 3202fa62f ("slub: relocate freelist pointer to middle of object") moving the freelist pointer from dm_state->base (which was unused) to dm_state->context (which is dereferenced). Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=3D207383 Fixes: bd200d190f45 ("drm/amd/display: Don't replace the dc_state for fast = updates") Reported-by: Duncan <1i5t5.duncan@cox.net> Signed-off-by: Mazin Rezk --- .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c b/drivers/gp= u/drm/amd/display/amdgpu_dm/amdgpu_dm.c index 86ffa0c2880f..710edc70e37e 100644 --- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c @@ -8717,20 +8717,38 @@ static int amdgpu_dm_atomic_check(struct drm_device= *dev, =09=09 * the same resource. If we have a new DC context as part of =09=09 * the DM atomic state from validation we need to free it and =09=09 * retain the existing one instead. +=09=09 * +=09=09 * Furthermore, since the DM atomic state only contains the DC +=09=09 * context and can safely be annulled, we can free the state +=09=09 * and clear the associated private object now to free +=09=09 * some memory and avoid a possible use-after-free later. =09=09 */ -=09=09struct dm_atomic_state *new_dm_state, *old_dm_state; -=09=09new_dm_state =3D dm_atomic_get_new_state(state); -=09=09old_dm_state =3D dm_atomic_get_old_state(state); +=09=09for (i =3D 0; i < state->num_private_objs; i++) { +=09=09=09struct drm_private_obj *obj =3D state->private_objs[i].ptr; -=09=09if (new_dm_state && old_dm_state) { -=09=09=09if (new_dm_state->context) -=09=09=09=09dc_release_state(new_dm_state->context); +=09=09=09if (obj->funcs =3D=3D adev->dm.atomic_obj.funcs) { +=09=09=09=09int j =3D state->num_private_objs-1; -=09=09=09new_dm_state->context =3D old_dm_state->context; +=09=09=09=09dm_atomic_destroy_state(obj, +=09=09=09=09=09=09state->private_objs[i].state); + +=09=09=09=09/* If i is not at the end of the array then the +=09=09=09=09 * last element needs to be moved to where i was +=09=09=09=09 * before the array can safely be truncated. +=09=09=09=09 */ +=09=09=09=09if (i !=3D j) +=09=09=09=09=09state->private_objs[i] =3D +=09=09=09=09=09=09state->private_objs[j]; -=09=09=09if (old_dm_state->context) -=09=09=09=09dc_retain_state(old_dm_state->context); +=09=09=09=09state->private_objs[j].ptr =3D NULL; +=09=09=09=09state->private_objs[j].state =3D NULL; +=09=09=09=09state->private_objs[j].old_state =3D NULL; +=09=09=09=09state->private_objs[j].new_state =3D NULL; + +=09=09=09=09state->num_private_objs =3D j; +=09=09=09=09break; +=09=09=09} =09=09} =09} -- 2.27.0