From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (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 D248E415F1D for ; Mon, 24 Aug 2026 12:42:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787575369; cv=none; b=SWY9k57Ft4l6Rk9qRF7afYe+SlUvGuPEyYaK8JynODKw38VeZEeuWEQBXjWEXBcTAdxvOEEVnHZ2J1Aqkvu+aXiypOQD8wZ7f/cQgIT0+zPVWDc1EGIkxJK5eIAKRS3tekL8QSu87lheHcm7XLp249z4hOAMs+YUViVZm2h5Cm8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787575369; c=relaxed/simple; bh=pBwaVH3+F4aywsHC/pUjnCRqPo1dtZMH1dC6s1o4CP0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=GDrT+AEkFFhx+S6VmxFrNFHucGF6vKVZTNTGBCXBRp0Khle6tXStHxfc9+taXdYx++g34kQE2OjapH1sGoMMtFT9u7ZdzcuwAhqIIFhBD/WMVe41gI+3sT22lGqovgOKGcYIhDrCZytfSMtmuLXl2a0B1yBZWlhQPdCv93GU/wc= 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=HXcN5yWr; arc=none smtp.client-ip=209.85.218.49 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="HXcN5yWr" Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-c15ba3a2b4bso481308066b.1 for ; Mon, 24 Aug 2026 05:42:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787575366; x=1788180166; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=MzHfY6cefdCdPGqFyAr7rWtx24z4JIT/TgxLZWE2Czc=; b=HXcN5yWrSQQSX2BYCiSwY+TJ17P2X9I+8LolCl3MuhNgGyjVriAc4HDC5687TNAhAX QTpXe48pa7LqJb3cy7IuU4PdPXo+7tN/PRobYWJo52nOESEUpFhJqLdlWox8CxgqE0aC JzNJqp57feIwzKlq3KDmJ8ziaB7izbCILuiZqhmuvuFd2ej6DnkGUu+6gEPdr+1jzj5H 05RNdMZ0FnW+T/WAWgoHAIeh+ddaNy74opS73ZOE+R0q6NAnMuIWX++9JXSn+oyb3xL5 76NtgeaO7r2G+IHSMRr9x74wa+wigZctLv2ZRVMIHYZ0jI/LL/LSZ7/iMFHoQF3Wgo5r ZhBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787575366; x=1788180166; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MzHfY6cefdCdPGqFyAr7rWtx24z4JIT/TgxLZWE2Czc=; b=BtVKgr9uADlZ9r7pc1zZRK8pADP8YyfIBm6m8xu287MRrsJl+GjjRD7B11II4O1Ib2 mOskhrB13y3oE5Z1ojV2aDF6EEawGJtrgc14KTglR+MjMeEl6Y6XAxwm4Q/Q4eepWacg sTy3bK/FiQ0r6y2bpt4y9ve3Ztn/Gy8X+cXp4TmqntFcogmFhuCjh0w6aiuQrZfPfy7e y7J26uxznLza2fCQZYpnF9l4so7QpwtKMHKjJQauWdca7SeWw3AOjPzxFtRQ7bYrq8Ge W7lllc2ei6r051t4XZHuhh/4hcYPIelTJXJd9ds/mWchJ2wGhQtYkVaBhmF4U0EvNJYA yu3g== X-Forwarded-Encrypted: i=1; AHgh+Rrsny6R1OoNnGUWPDbp+ZhtAl21AjDNRs5rdjJFzDixUQhzV1+B6XXj10TFmIxtLxSG8XF6EmlpwmA4kp0=@vger.kernel.org X-Gm-Message-State: AFuF++mrCyy9ENqG7dBAyePoEdPgW8VnLf4KGff2mr9y8rJDRhHaxzuF dzEXxj8AVTqNNSwRhnlZARUEU8CgtBHp9UVJEjbdhbCFy6+/u7wFROHa X-Gm-Gg: AR+sD13fPQp1zoWVDk1eOW/2rj05rGcWWocg4v53D8pDlXQLmZLA2rtwR0jpUe6SXBU YqpOwX/zdPcYpDR5LA5PbPhDY+IOxavjs3X72RMEhjV5WQhnCsrBC6U0PgjDRWqesJhgkZTvqep krmNekV2E7AXOy+EosnB1VBm2DQXyLQVP/2KlfNgA/FrOGPF7H5h72cpeSfzA7n1I00YwBpQSpZ xTWe/2rfwAyU2//NZACE4l817JdW+PGofz/f6avCLqE661ky2nPMZk/7NSewUDh2IF2H7xvulAN r9rksW3wg18Axc6PHcU8KeLYGSdZBm+obC1IDrDjZXKQcaMO8Rq1Q0x4Gah/uIW09jo6+Fwrqb8 T7zhM+nGsxENDo1CVN3X/fNeoUXvXSzaxVCOjefhWhltScJ9jaFS/wu8JnRMNaRIpeB/9o13Ngu tMQiY1NRjy7fJr98aVIUtlyzX6TQbQHWQG79HYMY+xz14d35G4yb7zt92lxfdzw/i2PB+8v1VlG tjAs584m4AGSOGzxlaj X-Received: by 2002:a17:906:f246:b0:c21:61a6:8821 with SMTP id a640c23a62f3a-c246a6cd384mr2373422566b.23.1787575365769; Mon, 24 Aug 2026 05:42:45 -0700 (PDT) Received: from [192.168.178.84] ([134.19.53.142]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c249689fa97sm1090754566b.57.2026.08.24.05.42.44 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Aug 2026 05:42:44 -0700 (PDT) Message-ID: Date: Mon, 24 Aug 2026 14:42:44 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v7 0/4] dma-buf: fix fd leak when copy_to_user() fails after fd_install() To: Baineng Shou , Sumit Semwal , "T . J . Mercier" , Benjamin Gaignard , Brian Starkey , John Stultz , Sandeep Patil , "Andrew F . Davis" , Srinivas Kandagatla , David Airlie , Simona Vetter Cc: 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 References: <20260817050457.1005285-1-shoubaineng@gmail.com> Content-Language: en-US From: =?UTF-8?Q?Christian_K=C3=B6nig?= In-Reply-To: <20260817050457.1005285-1-shoubaineng@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I've gone ahead, added one more rb and pushed the result to drm-misc-fixes. Thanks for the help, Christian. On 8/17/26 07:04, Baineng Shou wrote: > 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 before the ioctl) 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/20260710105740.3080070-1-shoubaineng@gmail.com/ > v3: https://lore.kernel.org/dri-devel/20260714114654.3885457-1-shoubaineng@gmail.com/ > v5: https://lore.kernel.org/dri-devel/20260730062645.233148-1-shoubaineng@gmail.com/ > v6: https://lore.kernel.org/dri-devel/20260807101140.1357218-1-shoubaineng@gmail.com/ > > Changes in v7: > - Add Reviewed-by: T.J. Mercier to patch 4 (selftest). > - Add Acked-by: Sumit Semwal to the whole series. > > Changes in v6: > - Rework the selftest (patch 4) per review: extract a count_open_fds() > helper, fix the copy_from_user() comment, fail (not skip) when the > ioctl does not return -1, drop the bogus mprotect-race mention, and > reword the result message. > > 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 | 113 +++++++++++++++++- > 6 files changed, 180 insertions(+), 52 deletions(-) >