From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1831B3AB294 for ; Thu, 30 Jul 2026 06:27:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785392832; cv=none; b=ZT6lbcJMeb0k5Twpw2oUafY7ksqOa55iNCQNeCH5m5hc+JdSP9/PjlrDpSBGwyFjI3RhEiHJP6l+RDgwT7J6lS8YbIy7JY9ets17SLOAzME6IJ1pSjFO7rH/mxRro9UrpiiZ78Xq8M1XciT/EpfIfJZPS+tXyDIqtbRSyDZMJBI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785392832; c=relaxed/simple; bh=F9WeV/nxAP6fHlwe0aqZ4cby4GCtvCwnHIr/wOKq+vA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=CFTfu1CVboJMbvP0LfmNXRQ6rrGuPQEThJpzV+KxcIk2kDrsB/g/dDxBO4rZ9+q2K9cyLWGyaBBqaXu1MTEdxD5xcCTgo7Lubv1IjksJSo378UOq7M9QC5WtVYwTx1+PEte/jL37U8FsCHKPjQi8nP1owEnuMr0EFbxB/1WP/M4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=S1w0edeF; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S1w0edeF" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-8485ef63b68so2005906b3a.1 for ; Wed, 29 Jul 2026 23:27:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785392830; x=1785997630; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=lC5eDEn/dzL9Eec299VWkBRGUg0dXbB9NQvY7s3O+kI=; b=S1w0edeFDpRDZQ3BaEigFHNvpdsf35Ha8TgPZVWhLdqlnnHO/1sDKYCL1KlZMvGEHi cbO3z5cDvuHlVBxVc9NEoK++OU9tzePta2Bx/CtBtYpk461p9WMpW8B4vRlARHoEv3M1 zc1vYvDSpYGxw+4OC12FIWXIjHFSJA0GH5vORS191ZGN/gXz6C4Jyug3m2nvwafA0niR eRGhLqpcODTPSGCSLnNL6ISgsIgl4Bu57OyBhs/pae17bzPIzxMA8xObK93ogLhaRQr9 kTnsgzNhuZbPXli8qCqKO7/xw2ArOnLYkcBWfONNNzywEjhZJ/WQMm9VipoQIkMXkhjS LVQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785392830; x=1785997630; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=lC5eDEn/dzL9Eec299VWkBRGUg0dXbB9NQvY7s3O+kI=; b=k4bhYhGSBSzmn6P0UiToJoJx6E1E/cI5bbnuA2ZY56GPEOwlpv6tXBZ9FrCLJOW3cQ GJjctIzAURcQTGrv8zpVSsLeA7QrV9E7pL7q1WxDw8f6FJZUQKB3RrJzBo1kAf73OY8u xIrQDlN6xFNpc9VWxfKfk90T18X3Hqa6SwtTKDR/qLPHWSkYmhxwh6nDtFTg3BMEIZQ4 y7XftiwYAoRMDlPn9RUkslnsD+s6Sa/PmnUsseeX1PDZuuHxrjk/bh7mvpETppPC07M2 dJl3kUumDTYPVMDscKuSbZoquyxydIezD4GVkwygjKSZ3PvM26gHlSrq37tzIks+ZV9H sfMg== X-Forwarded-Encrypted: i=1; AHgh+RoxC9gUgeYKTjAyNDneIwPf1cv1u+EONKad7IqG2Bs2WXXeDBb/tsaFw4s7/WC4+Z+CyqSFDSh7Hh54PFQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzOKRzif6Rx7wFsQhO4zMNffVhtGwq4v8q2HVUhk+n+VNvU+VhZ OK4lu0Qy1fUec8lFtEL1mNlLYnsQQfz5aJzvg2uay3x1XTn9JMxVL+rE X-Gm-Gg: AR+sD13ZdhiOjN/uR7yNBjXH/i1xvAXM/ZLsd0fHj3vchF4Jfw+nHVMetUgxiCh84jI jo3J1X6TA63eAMmfMdSy+/yJBQP+x21tCWZx7ywHm4LOazqUsjAqqZh449MQaIyksHcgqvlpHUf 16qU2rfSDafpZfjezEub6UlFTaLb3CcSfAae5YkPNRYDi8nLV9n3gpZLKEn7k0oOgMSy5sT5OLT ucTf5yu5d/C3Aj+U9EZ4H+IlR6F1JNsjaoGD0JjUvVm90BGV0wIY4veRruKAm6lNOd0Jmaz8twr LT5shRjzUlLFqI+NSsMnTEe17SOow/bPGQEJF8rO4U7D3Sx6DBMO1f6FGVTUIqcEV7CER9pQMDp uThxsbuN7jz6B7z34w+fWUfAu5INeMoIT3jW5n6cWqEsErX9MOPQIot904ASwugkied3sD56vW1 9QO0tcZ5H9gYwOEIYJyvy7YjPLQF+H9935uZZknkbu1oL8LqTbNJF32GtnQDvexqfZer1CsGY0S 8E= X-Received: by 2002:a05:6a00:b44:b0:845:e8f3:bdd0 with SMTP id d2e1a72fcca58-84ebc2623b1mr1371121b3a.33.1785392821527; Wed, 29 Jul 2026 23:27:01 -0700 (PDT) Received: from baineng-pc.. ([117.133.183.252]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84ea00507b0sm2503229b3a.14.2026.07.29.23.26.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jul 2026 23:27:01 -0700 (PDT) From: Baineng Shou To: Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , "T . J . Mercier" , Benjamin Gaignard , Brian Starkey , John Stultz , Sandeep Patil , "Andrew F . Davis" , Srinivas Kandagatla , David Airlie , Simona Vetter Cc: stable@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Baineng Shou Subject: [PATCH v5 0/4] dma-buf: fix fd leak when copy_to_user() fails after fd_install() Date: Thu, 30 Jul 2026 14:26:41 +0800 Message-Id: <20260730062645.233148-1-shoubaineng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Several drivers call dma_buf_fd() — which internally calls fd_install() — before copy_to_user() returns the fd number to userspace. If copy_to_user() fails, the fd is already published in the caller's fd table but the ioctl returns an error, so userspace never learns the fd number. Worse, the window between fd_install() and copy_to_user() allows other threads to observe and manipulate the fd (dup, close, SCM_RIGHTS), making any "close it on the failure path" fix unsafe. The fix is to split the allocation into three steps: reserve an fd with get_unused_fd_flags() (not yet visible to other threads), do copy_to_user(), and only then publish the fd with fd_install() via the new dma_buf_fd_install() helper. On copy_to_user() failure, put_unused_fd() + dma_buf_put() cleanly unwind with no user-visible side effects. Patch 1 introduces dma_buf_fd_install() in dma-buf.c (wrapping fd_install() together with the DMA_BUF_TRACE call to preserve export tracing) and applies the fix to dma-heap. Patch 2 applies the same fix to fastrpc, which even had a comment acknowledging the problem could not be fixed before. Patch 3 replaces the bare fd_install() in drm_gem_prime_handle_to_fd() with dma_buf_fd_install() to restore tracepoint coverage for DRM PRIME exports (suggested by Christian König). Patch 4 adds a selftest to tools/testing/selftests/dmabuf-heaps/ that reproduces the fd-leak scenario (mprotect flip between copy_from_user and copy_to_user) and verifies the fd count is unchanged after a failed ioctl (suggested by Sumit Semwal). v1: https://lore.kernel.org/dri-devel/20260703080922.1838362-1-shoubaineng@gmail.com/ v2: https://lore.kernel.org/dri-devel/20260710105430.3059661-1-shoubaineng@gmail.com/ v3: https://lore.kernel.org/dri-devel/20260714114654.3885457-1-shoubaineng@gmail.com/ v4: https://lore.kernel.org/dri-devel/20260714114654.3885457-1-shoubaineng@gmail.com/ Changes in v5: - Add selftest (patch 4) reproducing the fd-leak scenario (Sumit Semwal) Changes in v4: - Add patch 3: drm/prime: use dma_buf_fd_install() (Christian König) - Add Acked-by: Christian König to patches 1 and 2 Changes in v3: - Split into two patches (dma-heap + fastrpc separately) - Add dma_buf_fd_install() to preserve trace_dma_buf_fd tracepoint - Add fastrpc fix using the new helper (T.J. Mercier) Baineng Shou (4): dma-buf: dma-heap: don't publish fd before copy_to_user() succeeds misc: fastrpc: don't publish fd before copy_to_user() succeeds drm/prime: use dma_buf_fd_install() to preserve export tracing selftests: dmabuf-heaps: add fd-leak-on-EFAULT regression test drivers/dma-buf/dma-buf.c | 20 +++ drivers/dma-buf/dma-heap.c | 80 ++++++------ drivers/gpu/drm/drm_prime.c | 2 +- drivers/misc/fastrpc.c | 16 +-- include/linux/dma-buf.h | 1 + .../selftests/dmabuf-heaps/dmabuf-heap.c | 115 +++++++++++++++++- 6 files changed, 182 insertions(+), 52 deletions(-) -- 2.34.1