From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 BF6193A0E99 for ; Tue, 14 Jul 2026 11:47:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784029629; cv=none; b=LUBYhM5uEMauAp4onPWEp+3EgiTsvrdFROAnO2TiKgEeQGALqKKqo/j/QLkQBYF6UtOXezl3CPZjTQiTnP0/ENNtT5emNnPejdQTs4QdTL/I1ajaHMA801CwK3OnyyUvG7XRGHfxACG9ZmUKnYjsapV7h6UA7sVjtYo2oW+BJrE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784029629; c=relaxed/simple; bh=X2wVqRoit1+4E3SJBgb2YLDCRJq7n5J1UWwaW8Wo32c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=ZgKOnrmC2MVRLcoPQgNbRWlsee2SaziPbT0BGV6YYkPTw7IakSggKW052779P3Va1MNcIfwF0Urjx6wo07Q92tH16A00o2uduJGS+3T5WvNW0a8WIP4YKwyqtIjb33tla/c/SAwWuCUbJtXm6zDbyq/qWeH3pQEkYS2Q2/YzBtI= 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=ArLxSAFx; arc=none smtp.client-ip=209.85.216.52 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="ArLxSAFx" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-38deea72eebso2090720a91.1 for ; Tue, 14 Jul 2026 04:47:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784029627; x=1784634427; 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=CQQ+xhhQTJIQk1G/3qoal2tftp25+Ya+e3y9IcyhICU=; b=ArLxSAFxkT8wPJ4FUtusvN4arssaKMGkBVoyTZkrkBNkQdaGd7icp9J+xwfvjhVkKz kMzGj9xygRiapIDXvDuaon7eB/jDOt6d5JH1pG2FgfygAqzVA78kO35gIld/z0quviN4 8mJZaJbPMXGpl0Rt1Vhvvx5pqSIQvewd/lG1Tkh9Yd7DHUD1uDfKvLFco48N+TDiVY2f SGxoJEHUWSxpciaorcmUfE7eJZp9gzODwjmNXye3v1CBWUiWARAYkvLxo3UcpX1eU6Lu C/iKy/EAi+VJ715mQg7Mva4eQLF84hs6XtgI4xF318mq5vF7l5Tq4gL1XF39eljdjfaK f+tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784029627; x=1784634427; 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=CQQ+xhhQTJIQk1G/3qoal2tftp25+Ya+e3y9IcyhICU=; b=kDCjucmokFfSdxjLf9HPh9yziFIYQo3zAjFz3FijO20KboT+k8da1Eo3onMU0/hfwL VKNboJo+TPVeEnhT7FPYzDS9K+DPjIi4a1gXrvIVzTIn368dpXcRiH3PT8HwJIkElbmi XzNezvhO+VFlYfLE8tRa300MSGPBu9uf4L99Be9SYhv1I8ssz1biv/3YKVN2ExowcnT5 NAofhF6+4iQEbxcyUlChBIjIIOtP7/xpFnz0POf9ICw+9dcquLtDe0JdDyqd5TCWHsMy vgkf4E0fJ/+R/IgaZqSN3p6BblGAQhs6x5RNaI2t+is/T+xSOg1FLMMa5IXdjcZtdW4w vY2g== X-Forwarded-Encrypted: i=1; AHgh+RpqDrGrwF9C7JJxykxc9XlbUFHTILsMSbFdCqwZ7Ui/XmRCo/AqpPaU/R4ZCvSdNo2DgiZyVbrCbxydaYo=@vger.kernel.org X-Gm-Message-State: AOJu0Yx6tLk5AtnybuQifC40L6VbcORWq6jgGDMiIekP7W9YaETvX/oh p01Xlfu1fi9CQEOZPlVa7E0eMyEE4Fv2KlQAvq+h35Im+1CHyF+0UTMk X-Gm-Gg: AfdE7ckpMlqX7KvB4wVMnaV/So77GzAjSnOjZ6zJphaYhwJAKmYnL44qmd9VcqaM7UT uzwZAzZKLvW9wLoDeHXYgM1Ff5DXntz0TWR1UdRWkReIUSXtqOipn5X0r/+bttmYe00C3tRXTNp CERGfDe1knwa8WQWX+u7Z6xPY9KDjEP2/w7REZOkSw9O9NnkkjDIos8nPGjC3EEhimW+6alyqjs IDdOpNwzHjgnNVgrQs0b2PhMul3BbBTNxe30izW8XbkX83mbfMnivr43GM1jruxJY4UzlaBCJP5 UWa/JxTkUIEDMZ3CMDrNB0ACcOnKfWRdISj4dm8PWJ5+3n+G29EcpbZERR6NWP87HajJ36untVc Fsk/iEICTgn2bU+QuUCd4VWIHGYSN91lt2ndqvTBqKmMZLiKlY1lxYFhOob2HH6Sfdn2tjnUjq9 7AZuAuJ9b9pFGA5b/K X-Received: by 2002:a17:90b:2c83:b0:380:f34a:6ff1 with SMTP id 98e67ed59e1d1-38dc75eb657mr11649534a91.17.1784029627050; Tue, 14 Jul 2026 04:47:07 -0700 (PDT) Received: from baineng-pc.. ([117.133.183.252]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-38e17470008sm1356162a91.17.2026.07.14.04.47.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jul 2026 04:47:06 -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 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 v3 0/2] dma-buf: fix fd leak when copy_to_user() fails after fd_install() Date: Tue, 14 Jul 2026 19:46:52 +0800 Message-Id: <20260714114654.3885457-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. 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/ Changes in v3: - Split into two patches (dma-heap + fastrpc separately) - Add dma_buf_fd_install() to preserve trace_dma_buf_fd tracepoint (spotted by T.J. Mercier and sashiko-bot on v2) - Add fastrpc fix using the new helper (suggested by T.J. Mercier) Baineng Shou (2): dma-buf: dma-heap: don't publish fd before copy_to_user() succeeds misc: fastrpc: don't publish fd before copy_to_user() succeeds drivers/dma-buf/dma-buf.c | 20 ++++++++++ drivers/dma-buf/dma-heap.c | 80 +++++++++++++++++++------------------- drivers/misc/fastrpc.c | 16 +++----- include/linux/dma-buf.h | 1 + 4 files changed, 67 insertions(+), 50 deletions(-) -- 2.34.1