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 3E601C83F2C for ; Sat, 2 Sep 2023 18:15:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230309AbjIBSPw (ORCPT ); Sat, 2 Sep 2023 14:15:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbjIBSPv (ORCPT ); Sat, 2 Sep 2023 14:15:51 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66E0BE42 for ; Sat, 2 Sep 2023 11:15:46 -0700 (PDT) Received: from [192.168.2.205] (109-252-153-31.dynamic.spd-mgts.ru [109.252.153.31]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: dmitry.osipenko) by madras.collabora.co.uk (Postfix) with ESMTPSA id EC29C6607285; Sat, 2 Sep 2023 19:15:42 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1693678544; bh=FyCtyr6Dwvn+sc0weVs44aNx+SOUMWKSFsC6ZkrwZvw=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=i/sTohqUQLU0kuEePtR6JuLlMa0Hy6AMJMbLi94fbE8Y9J9Rqb7Bf/Fm/9sduYJwE yFEikC6EgJiNDjK2AZavhcV+lN2yOJWOrrwrMjOxcYlimbFs84OLLAWXlJRRxUW516 KKvkFLWTzda0ojw2CT7AMqeUFI1e5ZXMXfK7srfyVDHMGu+1pWhUgWUdpJOCjeBU15 /34Mkv2C1BJEQsZE5dxDEHAgtITx5OZnIhgmYzSzaNPO53Yo2PpgFhkzFjks2aIpKx T87ZjlZwKcv/3sSjF9r0vxh1xRTCohX1PjD0mrrIAN9/7pitpSbweCiq9F5hS8pVai HcP9XPNwwdYWA== Message-ID: Date: Sat, 2 Sep 2023 21:15:39 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH v15 01/23] drm/shmem-helper: Fix UAF in error path when freeing SGT of imported GEM Content-Language: en-US To: Boris Brezillon Cc: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , =?UTF-8?Q?Christian_K=c3=b6nig?= , Qiang Yu , Steven Price , Emma Anholt , Melissa Wen , Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kernel@collabora.com, virtualization@lists.linux-foundation.org, intel-gfx@lists.freedesktop.org References: <20230827175449.1766701-1-dmitry.osipenko@collabora.com> <20230827175449.1766701-2-dmitry.osipenko@collabora.com> <20230828131647.18888896@collabora.com> From: Dmitry Osipenko In-Reply-To: <20230828131647.18888896@collabora.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 8/28/23 14:16, Boris Brezillon wrote: > On Sun, 27 Aug 2023 20:54:27 +0300 > Dmitry Osipenko wrote: > >> Freeing drm-shmem GEM right after creating it using >> drm_gem_shmem_prime_import_sg_table() frees SGT of the imported dma-buf >> and then dma-buf frees this SGT second time. >> >> The v3d_prime_import_sg_table() is example of a error code path where >> dma-buf's SGT is freed by drm-shmem and then it's freed second time by >> dma_buf_unmap_attachment() in drm_gem_prime_import_dev(). >> >> Add drm-shmem GEM flag telling that this is imported SGT shall not be >> treated as own SGT, fixing the use-after-free bug. >> >> Cc: stable@vger.kernel.org >> Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects") >> Signed-off-by: Dmitry Osipenko >> --- >> drivers/gpu/drm/drm_gem_shmem_helper.c | 3 ++- >> include/drm/drm_gem_shmem_helper.h | 7 +++++++ >> 2 files changed, 9 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c >> index a783d2245599..78d9cf2355a5 100644 >> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c >> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c >> @@ -141,7 +141,7 @@ void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem) >> >> if (obj->import_attach) { >> drm_prime_gem_destroy(obj, shmem->sgt); >> - } else { >> + } else if (!shmem->imported_sgt) { >> dma_resv_lock(shmem->base.resv, NULL); >> >> drm_WARN_ON(obj->dev, shmem->vmap_use_count); >> @@ -758,6 +758,7 @@ drm_gem_shmem_prime_import_sg_table(struct drm_device *dev, >> return ERR_CAST(shmem); >> >> shmem->sgt = sgt; >> + shmem->imported_sgt = true; > > > I feel like adding more fields that can be used to do the is_imported() > check is going to be even more confusing. Can we instead have > > /* drm_gem_shmem_prime_import_sg_table() can be called from a > * driver specific ->import_sg_table() implementations that > * have extra failable initialization steps. Assign > * drm_gem_object::import_attach here (even though it's > * assigned in drm_gem_prime_import_dev()), so we don't end up > * with driver error paths calling drm_gem_shmem_free() with an > * imported sg_table assigned to drm_gem_shmem_object::sgt and > * drm_gem_object::import_attach left uninitialized. > */ > shmem->base.import_attach = attach; > > here? AFAICT, this is not going to work because obj->import_attach will be released by drm_prime core by the time drm_gem_shmem_free() is invoked and drm_gem_shmem_free() uses obj->import_attach as well. I'll keep this patch around unless there will be other suggestions. To me the flag is good enough, I'll add a clarifying comment to the code in v16. -- Best regards, Dmitry