Received: by 2002:a05:7412:98c1:b0:fa:551:50a7 with SMTP id kc1csp320712rdb; Fri, 5 Jan 2024 10:53:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IEhjU3ln+MkVpElMVqAy1+SysKdlSrMo5neNWhgnEPWGqpjIBfPMXLA354lQAmfLdSDpw6G X-Received: by 2002:a17:903:32cd:b0:1d4:d024:b5dc with SMTP id i13-20020a17090332cd00b001d4d024b5dcmr2617767plr.134.1704480796989; Fri, 05 Jan 2024 10:53:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704480796; cv=none; d=google.com; s=arc-20160816; b=KXS7xgYRjwksDqss2muvDk1Il1YstlGpho2BFQ1H1Wq7yrgGoyftq1/h6HkZ77Kjvu aXD1GG/9VajxzZ9v+4ltRxJfhoEFdskorjDzzwNZVRG3WI7mIL1vJvpW9C1QAZJxFivw Y5qBOXbApy+W4m+lvuxcf2zJtHXy6wEtMhbipkDrWLxUW18g1czYNLHb5khIZK4zB5bv uyN/cZu5TpK9NaBfvdBWCS4945vxje1/bFwzrcyLT0W0u8HS+n5G/SeD3TmifiguS6bs /qn5P0bqe1U6MpffGS/37fgn+ITYUWfeHwnuPcfkQzOdnjBEmJ0ixiA+N6LWgloLbgOa c9QA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=sL6LvkBQEqLS66m6jd/juufzq5b3ymkotl8lXmDTNYY=; fh=5cel2jD5h+yPMXVwxbomyVhwojHUqATy6nFjd4aOh4o=; b=VZt8WSrdGGA9bQo1gX3XnAbYjX625yIoFBAsPWzpoJSBqbfrE5lbPvufghraRlkFgo NYHTymYbE+J1IqunWyJKrRfkdU2g4TNCztnqNWM6++F6QiP0MNMsbdQQ5CxphKINVKXQ wjo89efqo5V/GU5Lpo9TsEqsdj+/nNZ0ETngTffkM3ikc2gDVDarxAO4v5HRf6JHLJqQ 1Gc/9OF1Q88psSA+nlBbUTkQuXvXbYZ2dqfBlCj5G7plJf7jeYwUjbdIVfniNx22yRm4 UMgB2iyPI/CBBWRPi27mA7oNxUOYyF4Uxz7Fm1r8O9m39HxOjne/E3TCYyTwqLNg/Gmw cF7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=bC32xTjF; spf=pass (google.com: domain of linux-kernel+bounces-18263-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18263-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id u23-20020a17090ae01700b0028d158b5885si707877pjy.33.2024.01.05.10.53.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 10:53:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-18263-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=bC32xTjF; spf=pass (google.com: domain of linux-kernel+bounces-18263-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18263-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A5136286778 for ; Fri, 5 Jan 2024 18:53:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 588453B793; Fri, 5 Jan 2024 18:47:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="bC32xTjF" X-Original-To: linux-kernel@vger.kernel.org Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (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 452603B19B for ; Fri, 5 Jan 2024 18:47:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1704480444; bh=fLJxMhHQEYSpt8Bqc4/bJy7tBd8v7hDnk2PHVorE3qc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bC32xTjF4i+0BoGAcr5FknxLJTdvHfAZsGZ7CI25/elJxlKVuE3vR6OrI4/hvmfCa ThiwAnjeHHzZDCJRTuX2A8OwgkO2x+1+kcDl/KxfVobeazReMlnDhpm9QhLTtOfFdo R5RnbvFX6znHhPkHoEv7fDKAfzt7Psl7QnWxYeWNUbj/ZxJxmxwUNaL6hOMbZfK0jp hXiZblVuJ9mVWrTR4w/GzjJS1IAQ2qHE5F3R7ZxexU6Zsvz0qub7pSxo3zrJsxvCs7 zVn+JG5PlHelFoc2HMTKOnOfQ1QldimrwoTjebvnpk/OtRQZK7lNR7tTn+X37ZbwTz dujX/5z5C4kCA== Received: from workpc.. (cola.collaboradmins.com [195.201.22.229]) (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) (Authenticated sender: dmitry.osipenko) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 50EDA3782046; Fri, 5 Jan 2024 18:47:23 +0000 (UTC) From: Dmitry Osipenko To: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , =?UTF-8?q?Christian=20K=C3=B6nig?= , Qiang Yu , Steven Price , Boris Brezillon , Emma Anholt , Melissa Wen Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kernel@collabora.com, virtualization@lists.linux-foundation.org Subject: [PATCH v19 25/30] drm/shmem-helper: Don't free refcounted GEM Date: Fri, 5 Jan 2024 21:46:19 +0300 Message-ID: <20240105184624.508603-26-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240105184624.508603-1-dmitry.osipenko@collabora.com> References: <20240105184624.508603-1-dmitry.osipenko@collabora.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Don't free shmem object if it has pages that are in use at the time of the GEM's freeing if DRM driver doesn't manage GEM/pages lifetime properly. This prevents memory corruption due to the use-after-free bug in exchange to leaking GEM. Signed-off-by: Dmitry Osipenko --- drivers/gpu/drm/drm_gem_shmem_helper.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c index e6e6e693ab95..0d95d723b90d 100644 --- a/drivers/gpu/drm/drm_gem_shmem_helper.c +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c @@ -205,9 +205,15 @@ void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem) if (obj->import_attach) drm_prime_gem_destroy(obj, shmem->sgt); - drm_WARN_ON(obj->dev, refcount_read(&shmem->vmap_use_count)); - drm_WARN_ON(obj->dev, refcount_read(&shmem->pages_use_count)); - drm_WARN_ON(obj->dev, refcount_read(&shmem->pages_pin_count)); + /* + * Prevent memory corruption caused by the use-after-free bug in a + * case where shmem user erroneously holds reference to pages while + * GEM is freed by leaking the GEM. + */ + if (drm_WARN_ON(obj->dev, refcount_read(&shmem->vmap_use_count)) || + drm_WARN_ON(obj->dev, refcount_read(&shmem->pages_use_count)) || + drm_WARN_ON(obj->dev, refcount_read(&shmem->pages_pin_count))) + return; drm_gem_object_release(obj); kfree(shmem); -- 2.43.0