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 0656832D0E3 for ; Mon, 17 Aug 2026 05:05:18 +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=1786943120; cv=none; b=MX3UXcyk1TOtTssYMcizybX7JmwSebxkHeQdXaKXQQmigGcUx4fCEAh9CXGGZ1OqT9qpYZ7KUzYzd9jUfo7CYQ+iGuLVQ6hVtBslsxfSsAIgo5r9eoJ5MzYBlX9IjMGvt0ydf6ihMvVW+EQCuVtOF03+X58e5azwXfu7Jqrsl/M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786943120; c=relaxed/simple; bh=ErYC+NBUB1uSMVe7dsSUwt3ETggiJBFdGT3p09rSUlA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=L1iqM+XubinXmhJvcRT7fE06luZCdX0UC+RxGa1aj83MJRVcey62KB4laPZToxOX7y0EHD9cejofGMRwNszV0mzJ1QYQz/coMTanPVJD/pZrgfhM9vfwXCNkP5iK9MdZdPCPKpu22G9RpWm2NvjDYjgoCDRIl7JyGbKVE7PRjz4= 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=O2iYdAev; 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="O2iYdAev" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-84e3007a2b7so2469300b3a.0 for ; Sun, 16 Aug 2026 22:05:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786943118; x=1787547918; 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=wA2XmPl+hovwGxL3ai/U+yA7AWOU/owR4IcUEcMXDYY=; b=O2iYdAevYt5f4khFQNgP5bu5iMekzcpaFVoh/eGfulEMTn9UNF+KYNGAj56vk/Xe5J uOOqtzH0EO6K7p8vNfUw9vJWjI/iHDdhutTGn/6TjBk1+WPzXKvgDVijfWZZBruDW4tR nkMWI+ZGTIV3paoQSx9VO+ExdUsk3Qrq9vn3DnRlS9mrNyNM7ynhOx1ClzwwlaNY90w9 RB5dg73VoVq7hJs4DkKG2nQMvrHPNYxqdxCe0l6aIcFQNVNgkirANDhegr6HaKDjeaPw PGrcJ85cMgQamjtj6dErJWJoJ/339Q8MGelQnQYEvPbEl8D0pDsCaqEpJHsXVXv50CwL dB0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786943118; x=1787547918; 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=wA2XmPl+hovwGxL3ai/U+yA7AWOU/owR4IcUEcMXDYY=; b=DG3nudbE8NLKpEIcesrhjyY15mfzN0truRv7sBuAX5MvKpu9CnCZgQb7OZA3MwkRu3 SzFAKJVZgNd/IzJxB44+Xxd2qUbXVCdH0qL5j3LAunvAPi0crC2YDSgh2UlfrLCj9AF0 jkZORGYDviV06diAT/fRKoCvRkEGUn15Qg8xuMAFZzoyAMnzR5WejB1K7rLSD6Tgt81a bFj11DTDjmBqF8crj6Gl/rcqdDYjmYng+VOvCaq7rVPJ+qElHlxIRcpa7gRQFEjN0HXH 5HrwjdNV4u7PNA/fWqwCyJXcp2F3bL/cn+V+opnaIo/w/O879un9+9FU9aKuyqiXkDkg TBMA== X-Forwarded-Encrypted: i=1; AHgh+RpRpsSBLRrbV0vV10yNDaVY05Y2iM/TK5phIuoWYMy3CDniLgG4tHWbkBl3qspIVh2/B7l2xUizRgeIimo=@vger.kernel.org X-Gm-Message-State: AOJu0YzsR6V6gwtsI27t98z3Kz0oyuZEFLq0vhpTObXpCoBBWPoQ7pDX PsTM2/iOgW8DcC5Rc13q4tAG6xB2JsEAl9p1hwemN2FCi+EXYyS+TCtw X-Gm-Gg: AR+sD10Z0KKKIeibH5ynQL55R4uUnwHh/ou6Em1lJYoPzxuohqv1J9vTW5EOJJs7bVF MjkUVtsGfwVn7w9P5lzJe0NA7+bFMMp7RdSseIPVSKpqqeAJrpxHcUtyMDMPYKWQzpE+dT8qm1k odi9YkEk5qUXLhphymaZlzIMlbGMw5H+adVHt+OPrWILRiy0LkIle4qIlDdUBGZn8Zktm/lVe48 ZbhNu07WyJl8ZMQW61H3cj4UHJWM0xx5zqLGgA4XPWlN6lUTnWkqXzWBC203gTOQO0VrlyJj2tb D43nNCqPwkSihVtpuPU6Ja1zeYqKYTXNJSjSnBed9qGQ5LxAHyOgjsbyvu3PvxTtknEL08emEZt 4r2IuB2oDuqCNkuEe8nDqIgkbjBqtd+eMEwcsk8l35OIsKlB0i0OsTLou0to5Vmca9+D5LPcJTP wHQORUn0hax+NtMxwXXKM+Ich4xUBQlir/DtXhWv04dgjjw8dOP1SAZiNDg6esEIuN X-Received: by 2002:a05:6a00:94fb:b0:842:5da3:9b89 with SMTP id d2e1a72fcca58-84fde3e397cmr21923062b3a.38.1786943118310; Sun, 16 Aug 2026 22:05:18 -0700 (PDT) Received: from baineng-pc.. ([117.133.183.252]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8517d267310sm2131036b3a.53.2026.08.16.22.05.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Aug 2026 22:05:18 -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 v7 2/4] misc: fastrpc: don't publish fd before copy_to_user() succeeds Date: Mon, 17 Aug 2026 13:04:55 +0800 Message-Id: <20260817050457.1005285-3-shoubaineng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260817050457.1005285-1-shoubaineng@gmail.com> References: <20260817050457.1005285-1-shoubaineng@gmail.com> 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 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 --- drivers/misc/fastrpc.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index f3a49384586d..c5143cd25767 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -1709,24 +1709,20 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) 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; } -- 2.34.1