Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3520231pxf; Mon, 15 Mar 2021 11:22:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz2DixdFJ18okTHxIyiFadUV+ycEEqhQxjs+7Hg4dBsYv/T7VSnr1qi0BpxKUxygy5nJBco X-Received: by 2002:aa7:d74d:: with SMTP id a13mr32744415eds.199.1615832563221; Mon, 15 Mar 2021 11:22:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615832563; cv=none; d=google.com; s=arc-20160816; b=HLUOVVjalm0RClurMwQp9JQagQgjq/IyOGJETmjzlwhT1u4BUYGbP9Udzcq4fyUvY6 yk3s9o/gWhOPNlZeSpvfT0x7yX2TU2H4oZkOwIVb2ZT86gdAds2Wz3kp7qUKkcISF8u7 h1ag4z/VOXDQRVFK/4U9LWW2GZPHD8XM/3t3uRmqaAiac+mzIoPtsdXzFMdfaccvHPom xs8GeeCSYOMp6eqM7H/2JKFhUjnPaI7ofFr+sas3YCC5Hkszo5R6IFJc9vu1xCLJdU8X 1IG5Ypb1KT/0t6iLUDJD/K+pbzxUrErWoWyBi8sdYOgREhwzAJWG+GNZiWLmGOaVb60T XHrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zkW820mX1tetk2wsshAtGa2j0O3kHQskzLvOTKoiEvk=; b=NlDSNqI6ofCe5Wf2Xl0cXSuh83FkJJsi3w8RmXA8w1YxsbAfyoOwarWOMTK5+xVoZ3 97iIKTaF3sYlDTdF2RWWnlH6ZpNG0jeBQE2GeekCXsbMeO+Lb0je8wTyTh6bS4h4U8sW +lxytT5mH39R5KwagQC0uaimoHa0ru6cU67ljvYE9IEJdh1vheVSPmAvidXnWe8bzYnA EqtEp7ZlsnUA+3HUwM225j4clniQDZlR2xGEi7ufLSwO5JqZLrsPq9nrsUpPMxjqWhI/ 1d27ugYOzRcabaFlOcqpQCCPBreeJCSPfdK/N+68tfNlqATctcYNWHW9iRFD6kXGUTc1 q+gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=saOAKY6C; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t19si11100956ejx.42.2021.03.15.11.22.20; Mon, 15 Mar 2021 11:22:43 -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=@linuxfoundation.org header.s=korg header.b=saOAKY6C; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237668AbhCOOT3 (ORCPT + 99 others); Mon, 15 Mar 2021 10:19:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:37820 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230422AbhCON7q (ORCPT ); Mon, 15 Mar 2021 09:59:46 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 755C064F90; Mon, 15 Mar 2021 13:59:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816754; bh=uargg+QuviotxUvpEmJqGKGs4hZ8bX0N8YM8IfRjFws=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=saOAKY6C7Y/luCy/LDUUB8Gb/TKuiLvbVCr/thYGZc7K3O1z6bDGlhv89+39iTnpI a+uz8cv/N37QANBPulFYjSJtaoKGPTMyzE5ElSMiL3yAIEWD2VsgPPxcbOmW2huhos WO3+kwnZDTr+MitbVgaj9NNXLtcjZHJvcSfzVRIE= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Neil Roberts , Steven Price , Maarten Lankhorst Subject: [PATCH 5.11 105/306] drm/shmem-helper: Check for purged buffers in fault handler Date: Mon, 15 Mar 2021 14:52:48 +0100 Message-Id: <20210315135511.190882487@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135507.611436477@linuxfoundation.org> References: <20210315135507.611436477@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Kroah-Hartman From: Neil Roberts commit d611b4a0907cece060699f2fd347c492451cd2aa upstream. When a buffer is madvised as not needed and then purged, any attempts to access the buffer from user-space should cause a bus fault. This patch adds a check for that. Cc: stable@vger.kernel.org Fixes: 17acb9f35ed7 ("drm/shmem: Add madvise state and purge helpers") Signed-off-by: Neil Roberts Reviewed-by: Steven Price Signed-off-by: Steven Price Link: https://patchwork.freedesktop.org/patch/msgid/20210223155125.199577-2-nroberts@igalia.com Signed-off-by: Maarten Lankhorst Signed-off-by: Greg Kroah-Hartman --- drivers/gpu/drm/drm_gem_shmem_helper.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) --- a/drivers/gpu/drm/drm_gem_shmem_helper.c +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c @@ -525,14 +525,24 @@ static vm_fault_t drm_gem_shmem_fault(st struct drm_gem_object *obj = vma->vm_private_data; struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); loff_t num_pages = obj->size >> PAGE_SHIFT; + vm_fault_t ret; struct page *page; - if (vmf->pgoff >= num_pages || WARN_ON_ONCE(!shmem->pages)) - return VM_FAULT_SIGBUS; + mutex_lock(&shmem->pages_lock); - page = shmem->pages[vmf->pgoff]; + if (vmf->pgoff >= num_pages || + WARN_ON_ONCE(!shmem->pages) || + shmem->madv < 0) { + ret = VM_FAULT_SIGBUS; + } else { + page = shmem->pages[vmf->pgoff]; - return vmf_insert_page(vma, vmf->address, page); + ret = vmf_insert_page(vma, vmf->address, page); + } + + mutex_unlock(&shmem->pages_lock); + + return ret; } static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)