Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3509160pxf; Mon, 15 Mar 2021 11:06:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4tMruQL5xTVstBcR8D1xXKDt/0c9PT8Mpth/B9f8kyvBt0USUPw7FDWqkoF9n+KPrK5v8 X-Received: by 2002:a17:906:3849:: with SMTP id w9mr25439395ejc.7.1615831593996; Mon, 15 Mar 2021 11:06:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615831593; cv=none; d=google.com; s=arc-20160816; b=A+ZAk7HeLGdx2xV1yMb1SWxM49Tx1qEwVVxrPahmWZYb/MleHRoTRPGYTfn9vvVo9/ FLJKuHbFZBkqEkRyRMVx5AlV9Raydnz5nOOPPVgZSt+2/XoXz0HSgcLB0H0CUWFeLfs/ rKf9Cs5SuaREC30WkAnThrhsl/BAYs0n+Pdaj3YU2McJvkeLop6DdQmi+uVsKZ6p+Frp JRWh8T1RI9Yo7Sxq1k8kXxnOfGjJNNu1FhD1A1ktmBpYwM8XTKHWIH8jUSx2YJKTvavE 6zq2o+3Z8lWZud1kDYKCixfqldg2VEBZO7UIbWcZWfXFekXPCuo+GuTv9StcvTxu1/Qh R5qg== 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=JvBcRfyYlOOvG+pG7bSHhanExH3AkWb6xK8u5GM0kJw=; b=ZKHw4owW9mZ0eRonFb5U6JZt0KAWktnOEoJgNFAUyQqBT7oGQMuZXDoHK1sFFHQ5nk 28tHJOsb4at8dGJsZWn7gA8jDSPGsVvlqY3Ocknh8B8LUCulztnH1rMOshqh+X5ZqIw7 yR84L8JyRDR1DAYXiKMIpDSYdL4KT1Ckh0tRy123ErqGxSUj+LsHjOVK923I7PXxtcht yI5GX71IFzg5AHQxZaf+ZNPhLepkRjSAHyUQTZ3kKZUjDUDc/Uzp8P/5ptetxfQ0aGUM gX3W9QK/hg7taN4++YR3/kGAMhA5cW2nhBGFO06l8jJcsW8gNBowJk64PFX1QaWicKYm 05Xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=NzMPgJ3K; 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 j7si11651273ejm.408.2021.03.15.11.06.06; Mon, 15 Mar 2021 11:06:33 -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=NzMPgJ3K; 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 S235526AbhCOOHt (ORCPT + 99 others); Mon, 15 Mar 2021 10:07:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:36728 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232071AbhCON6J (ORCPT ); Mon, 15 Mar 2021 09:58:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C9A9C64F38; Mon, 15 Mar 2021 13:58:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816689; bh=s0igxtKMfYxA8SVvVJiw7ZJu0AVVuctGKDjev/kC1WE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NzMPgJ3KjK5rRQUIqRfT0zj1kfF8ClOBYHC4UG86/hLVPcACQxNv8qeOxDAj1QREU MFONFfn9V+HwrIBJufYP2xjNlhnBno0Cx8ieQjr1vok3J0IhDN5ltdeV/yVIcM2suT o217AmVfmMaKs/7ct2oNXkyhXq8QFKd36W4ebZ1w= 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.4 048/168] drm/shmem-helper: Check for purged buffers in fault handler Date: Mon, 15 Mar 2021 14:54:40 +0100 Message-Id: <20210315135551.941962641@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135550.333963635@linuxfoundation.org> References: <20210315135550.333963635@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 @@ -474,14 +474,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)