From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 C494341D135; Sat, 12 Sep 2026 09:33:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789205603; cv=none; b=Z6T38nWaCoKzbIWy9POgoMK54+2yVYOYExhcUKDs3kNlQ5ImBOcuCq2mkdfkozXomgfnhMrEiRTFaVI8lGvL+/PFixYfbsfJwwhBbjJbtpSxekGK0GULgAjN1cCqW7sacgZZ2cwz8mkvAyTOHA7Jp73Ta27vnT+bW9sXZuD15Q4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789205603; c=relaxed/simple; bh=Ba8OQhwuBy/0NkTU+6Tqe1B49aWFVcGCts8xc5hgw3I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AdHippKvoXJatRsVgnJtMhDj0akaI2KfC5SAK7Mz9a6o/bgtu/TCTZDFZ/uZRn0IUNna9Did3LFMX23ybChEDQt/kvu9OtMeiXzuEe/iw7xuRXeznXFS+IuX1JIHAX4l92sYoKWkQ6SZ8S2jq7SHHGiDBU0BvtlF8fCYACintak= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=aBNBWMin; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="aBNBWMin" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C2DA1F000FF; Sat, 12 Sep 2026 09:33:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1789205599; bh=IM8pPYfopMTDImAmm2yUngWnQkdmfJsn/y9IeEmogZI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=aBNBWMinPNf0xqwdrd/SnOPzr7ZnhkOuIT32OK1vG5MCEpPeW+gRrHGi59kRO+A6e NVJDbi4wNreHuFjWHhVuKGRVojIstFO0bP3P5MY94Mj2yHUoGRcyYc3JtAvFWBniUj b0qygH1/G96qPNMN/B5c8P3YjZLGmxILUgEp6Wsc= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Semwal , Baineng Shou , Sasha Levin Subject: [PATCH 6.18 0034/1518] misc: fastrpc: dont publish fd before copy_to_user() succeeds Date: Sat, 12 Sep 2026 08:36:43 +0200 Message-ID: <20260912065624.201059686@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260912065623.398859879@linuxfoundation.org> References: <20260912065623.398859879@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Baineng Shou [ Upstream commit a4a1a2bfcb29785292d634d7787edc6fb550714d ] fastrpc_ioctl_alloc_dmabuf() calls dma_buf_fd() which installs the fd into the caller's fd table before copy_to_user() copies the fd number back to userspace. If copy_to_user() fails, the fd is already visible to other threads in the same process but the ioctl returns -EFAULT. The existing comment in the code even acknowledges the problem: "The usercopy failed, but we can't do much about it, as dma_buf_fd() already called fd_install()..." Now that dma_buf_fd_install() is available (introduced to fix the same issue in dma-heap), apply the same pattern here: reserve the fd with get_unused_fd_flags(), attempt copy_to_user(), and only on success call dma_buf_fd_install() to publish it atomically with the tracepoint. On copy_to_user() failure, put_unused_fd() and dma_buf_put() cleanly unwind without any user-visible side effects. Fixes: 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter") Cc: stable@vger.kernel.org Acked-by: Christian König Acked-by: Sumit Semwal Signed-off-by: Baineng Shou Link: https://lore.kernel.org/r/20260817050457.1005285-3-shoubaineng@gmail.com Signed-off-by: Christian König Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/misc/fastrpc.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -1681,24 +1681,20 @@ static int fastrpc_dmabuf_alloc(struct f return err; } - bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE); + bp.fd = get_unused_fd_flags(O_ACCMODE); if (bp.fd < 0) { dma_buf_put(buf->dmabuf); - return -EINVAL; + return bp.fd; } if (copy_to_user(argp, &bp, sizeof(bp))) { - /* - * The usercopy failed, but we can't do much about it, as - * dma_buf_fd() already called fd_install() and made the - * file descriptor accessible for the current process. It - * might already be closed and dmabuf no longer valid when - * we reach this point. Therefore "leak" the fd and rely on - * the process exit path to do any required cleanup. - */ + put_unused_fd(bp.fd); + dma_buf_put(buf->dmabuf); return -EFAULT; } + dma_buf_fd_install(buf->dmabuf, bp.fd); + return 0; }