Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp117806pxu; Tue, 5 Jan 2021 06:41:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJzZcu7hdoBaGRYXdKtV7HSWPrGDhwb6tg5fQhve0e1B54rTPnSsQ3AyG5kNnm6ECPsfVIYz X-Received: by 2002:a05:6402:1816:: with SMTP id g22mr75926edy.80.1609857688288; Tue, 05 Jan 2021 06:41:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609857688; cv=none; d=google.com; s=arc-20160816; b=d9irxEnBq6PBbEHyLj9/1GcXPHdOCheH8squvCwzF0BUl/EZGIE7l65RJAJG3XpKCa YiFvuNz22saT+LsUp07nEHX9E6Loput9iUYl0+5k5NpfQSIpRovp7MMW5Q8SwXh9Nebq Zn4DhLtVqMa/oJ5PuWcf0koCZcZPBhlB+UHRUWAnMoFcV03H7Cdm+osqOAOGkzSb/AjX dFfgowvS6pbrFgZibwmwuQ/GsIsPjPtTKf0hXCIJEXPyLtTfTmBsiWc4aNXfTBktJulL pOIUnTmaqUOfAuNU0Il5mtmVG8z7ywcF6M7Gzj553Vyc9RBLEbC3oFeMJaiLKpSOmKjQ Eaew== 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 :message-id:date:subject:cc:to:from:dmarc-filter:sender :dkim-signature; bh=4DiNJfGzzRCDF2FL02HtG0SOdr7LDb46DzlmbUJfV6s=; b=jZbHD6e13t7Kl0bOtteA3tQcAbuzz/A+s8uvocwSdsNZTjz4iMFMrWFcqHwefemnYv GIM9Jo/SsUMedbcioEAVlb9Vbv48GeqEJH01nUmghX5Lv/F0K9vDUBrzWK9mR7TPRQq7 /vT/n7ihvAj0cLQeJMnTUbn2tH7TWEbmI9DBhvPB42uVzKU6mMzTV1EGJKa/7GJzv11n qNI63px6fhybZUeP3AaDCET4u1kCasFnGYg2HCgcGuRBL2V0sqsXcemlBdXbI3tlOoNI gL2BpE5Vcy1A4CBP8nenPrk9OSmQie7VS1RZwRGRSlcDnsqDiyza+1O1b/sktHJa5qJh xgSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mg.codeaurora.org header.s=smtp header.b=Mc79uCTk; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q26si36195360edi.38.2021.01.05.06.41.04; Tue, 05 Jan 2021 06:41:28 -0800 (PST) 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=@mg.codeaurora.org header.s=smtp header.b=Mc79uCTk; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726974AbhAEOhy (ORCPT + 99 others); Tue, 5 Jan 2021 09:37:54 -0500 Received: from so254-31.mailgun.net ([198.61.254.31]:51024 "EHLO so254-31.mailgun.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726901AbhAEOhy (ORCPT ); Tue, 5 Jan 2021 09:37:54 -0500 DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mg.codeaurora.org; q=dns/txt; s=smtp; t=1609857453; h=Content-Transfer-Encoding: Content-Type: MIME-Version: Message-Id: Date: Subject: Cc: To: From: Sender; bh=4DiNJfGzzRCDF2FL02HtG0SOdr7LDb46DzlmbUJfV6s=; b=Mc79uCTk8J+0zXYJOEpu98hPISZsRm++QulwcBbuRCceBLqVoLJsaYAQw+HFdA9q76CoFzZZ JcDFT2cjBScRSrBuVXJTD5KNcnzqBnk4NUTHa9osQLwqApbUVIHSM27AMqNIbsY0odQX8Mlj 5O8XwCd+j0gXFLRrU7V/N24JVnQ= X-Mailgun-Sending-Ip: 198.61.254.31 X-Mailgun-Sid: WyI0MWYwYSIsICJsaW51eC1rZXJuZWxAdmdlci5rZXJuZWwub3JnIiwgImJlOWU0YSJd Received: from smtp.codeaurora.org (ec2-35-166-182-171.us-west-2.compute.amazonaws.com [35.166.182.171]) by smtp-out-n08.prod.us-west-2.postgun.com with SMTP id 5ff47988b73be0303d2da1cf (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Tue, 05 Jan 2021 14:36:56 GMT Sender: charante=codeaurora.org@mg.codeaurora.org Received: by smtp.codeaurora.org (Postfix, from userid 1001) id AF37CC433C6; Tue, 5 Jan 2021 14:36:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-caf-mail-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=ALL_TRUSTED,BAYES_00,SPF_FAIL autolearn=no autolearn_force=no version=3.4.0 Received: from charante-linux.qualcomm.com (unknown [202.46.22.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: charante) by smtp.codeaurora.org (Postfix) with ESMTPSA id E3526C433CA; Tue, 5 Jan 2021 14:36:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org E3526C433CA Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: aws-us-west-2-caf-mail-1.web.codeaurora.org; spf=fail smtp.mailfrom=charante@codeaurora.org From: Charan Teja Reddy To: sumit.semwal@linaro.org, christian.koenig@amd.com, arnd@arndb.de Cc: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, Charan Teja Reddy , "# 5 . 4+" Subject: [PATCH V2] dmabuf: fix use-after-free of dmabuf's file->f_inode Date: Tue, 5 Jan 2021 20:06:39 +0530 Message-Id: <1609857399-31549-1-git-send-email-charante@codeaurora.org> X-Mailer: git-send-email 2.7.4 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 It is observed 'use-after-free' on the dmabuf's file->f_inode with the race between closing the dmabuf file and reading the dmabuf's debug info. Consider the below scenario where P1 is closing the dma_buf file and P2 is reading the dma_buf's debug info in the system: P1 P2 dma_buf_debug_show() dma_buf_put() __fput() file->f_op->release() dput() .... dentry_unlink_inode() iput(dentry->d_inode) (where the inode is freed) mutex_lock(&db_list.lock) read 'dma_buf->file->f_inode' (the same inode is freed by P1) mutex_unlock(&db_list.lock) dentry->d_op->d_release()--> dma_buf_release() ..... mutex_lock(&db_list.lock) removes the dmabuf from the list mutex_unlock(&db_list.lock) In the above scenario, when dma_buf_put() is called on a dma_buf, it first frees the dma_buf's file->f_inode(=dentry->d_inode) and then removes this dma_buf from the system db_list. In between P2 traversing the db_list tries to access this dma_buf's file->f_inode that was freed by P1 which is a use-after-free case. Since, __fput() calls f_op->release first and then later calls the d_op->d_release, move the dma_buf's db_list removal from d_release() to f_op->release(). This ensures that dma_buf's file->f_inode is not accessed after it is released. Cc: # 5.4+ Fixes: 4ab59c3c638c ("dma-buf: Move dma_buf_release() from fops to dentry_ops") Acked-by: Christian König Signed-off-by: Charan Teja Reddy --- V2: Resending with stable tags and Acks V1: https://lore.kernel.org/patchwork/patch/1360118/ drivers/dma-buf/dma-buf.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 0eb80c1..a14dcbb 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -76,10 +76,6 @@ static void dma_buf_release(struct dentry *dentry) dmabuf->ops->release(dmabuf); - mutex_lock(&db_list.lock); - list_del(&dmabuf->list_node); - mutex_unlock(&db_list.lock); - if (dmabuf->resv == (struct dma_resv *)&dmabuf[1]) dma_resv_fini(dmabuf->resv); @@ -88,6 +84,22 @@ static void dma_buf_release(struct dentry *dentry) kfree(dmabuf); } +static int dma_buf_file_release(struct inode *inode, struct file *file) +{ + struct dma_buf *dmabuf; + + if (!is_dma_buf_file(file)) + return -EINVAL; + + dmabuf = file->private_data; + + mutex_lock(&db_list.lock); + list_del(&dmabuf->list_node); + mutex_unlock(&db_list.lock); + + return 0; +} + static const struct dentry_operations dma_buf_dentry_ops = { .d_dname = dmabuffs_dname, .d_release = dma_buf_release, @@ -413,6 +425,7 @@ static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file) } static const struct file_operations dma_buf_fops = { + .release = dma_buf_file_release, .mmap = dma_buf_mmap_internal, .llseek = dma_buf_llseek, .poll = dma_buf_poll, -- QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation