From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 BF18C44BC9A for ; Tue, 14 Jul 2026 14:33:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784039605; cv=none; b=CdLSqqD+5+Z+ET8zoO1oz69N0cfZr8q+1Z+19pmtsjylW8pyESFp9HthavgJdCG0ieaXgeR5B1WA+tQ5ZKwU7ibRGQrfEl04L2HXoEAoF0GsCCUbF+eav0bdOQLQtVyJ8586ZCoMka3zijnxst2/1mDvpg5X5iSCKwMCtEKzkQc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784039605; c=relaxed/simple; bh=t3+FgGxIANUdTs4dodfcPA0vqCjpmZzzbODy8MT0F5Y=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QrSyeUqueIPjbITDMuXPYWeAyJkYRkpaLyuySIKsCz37qAt6/AM8LH2LXIxIZ7Q8T9hp+KjsWi8IrTDa6LeO5ZMuz5prJPqDQGgocXk4d1vMOFtP0tAwKjMDCVlfUYAxPaVe1qcJgPEImtrj+EJ5Sjj57tRj9xSd6df22qpLWKw= 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=b7paIVOD; arc=none smtp.client-ip=209.85.128.44 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="b7paIVOD" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-493f0ae9572so17932885e9.3 for ; Tue, 14 Jul 2026 07:33:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784039602; x=1784644402; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=yqr76+rJ8iwHayK7NjLGqhPMmOMQyjm2NesJQTgS2Xo=; b=b7paIVODVxIpb40kzN5mRJBFqqVsZcioNrHpT1aNmYqxF8tVuz2kyfOREywwF1RHFQ 7tEi//lXOT01O3qB0l/BU6dC55LcgsIScWk3TXPB8FxDnCVGCZxlu+Gbl0wBvweX/LIj zChGMtezvGI1H+DeGyD+toHJ1qteKG0Wq4EAiMZ8aXlTITdgcC5hpg7/a2ZwA4KiGRDu Cts/RJ1FcUJo0FybUBGRuiT/DGIbIVL/rgRiD7dnDig+Dc216f9HNuaUcWjxiwsbHXel UZcsedff2r/R2Sci3a2Be8y2Bul9M57oqQclxArFE54HcbUddHPFJ6KPkoVBlWAQm6JM 6emg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784039602; x=1784644402; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=yqr76+rJ8iwHayK7NjLGqhPMmOMQyjm2NesJQTgS2Xo=; b=PkrvO74UaTgUBuFpgff8sEMOxbh4MRX6UdLtm76CrNtKYSOSty6zsYINDC/KAsTrYZ PFXZfZuN6zMXPt+QJ6LbzWN8zPGjS01dY+VSZnLaEs42kMIIATi1WSRQQ5ZEZ1bPlnG+ IjRFLasnzeVCGuQSOdv7vBNM02TXRuneNCb8ts6B5Qg63GIiSKh4S1APYZXhSDTaTS6h Bubia13ZDwefFOYqwYfiXrb32pYJelDzvmn3gvTgIieq51Q72cYM8VJ5lLAtiLdyEBjK X/MfyenpzySZVUFiBkaqa1cH+2xdOvCs2ocwqaxZz8/jdSBfcMmAm9HI3Gt8ICdfmkg7 12lw== X-Forwarded-Encrypted: i=1; AHgh+RpC+OE8b5Y/GVoCnOAGStPpRsVOkloTCWHpGELM4q1kw8kURRCxZADvb4a3zfpUjy7MCozw9WqwGTJByA==@vger.kernel.org X-Gm-Message-State: AOJu0Yw4/z277n7My2kLiyhkH/fWdfRczGh7aSkX9Cy+hS25HKMS6LUo zkAnm4kL01OIlSCF/eB/NMy2zQpjjep2Ukvmx4e1KpKRdjEnVzSfcc6u X-Gm-Gg: AfdE7cmRg4GqTz9bSguAeC53nS/yPpVpZelBCoYT5O+jC/5C2EInwF+NvnQ0iHOaCPy lBG1X4VcW16NQBSpY6iIi39WV9p/7VYBgiEk5x6K5i2s6M0Vwz34vXMCZybAhxDBIjGON80/rYh irWTRL1Zt89za4Ce3tHPxjTkQ+0Cp9v/kqYYYYZH2OaIhw4jnD6cwz/mclEqoyVjnTQjMFNQywM z95aoDVMQGTRHQfU/8w5p6nYyQaOIPUCPzDEjedA+O5tpd41GDOhEnhZb8W5wDkxrJ9MpwNc37e H9I/oIFbCwI1kKFVF9rfwHjl54KvrZk2MzV7fnYIUMTUIeeSiyRTIjQBtEr6ECMSZNA2uZmKcrx ZSBL1E1UdIBavZV3XRmKuGcpcEZ7CHhADu4Y2OLZFlolCtKtu61JSJjuRxsIn49B6dXz37CebCk 0Y010tEoybw2OWno8MIB8JlTaQQN/grPNN3esTnftQ9EiEnEo3dA== X-Received: by 2002:a05:600c:3e1b:b0:493:e890:9e0b with SMTP id 5b1f17b1804b1-493f883157bmr144382565e9.26.1784039601550; Tue, 14 Jul 2026 07:33:21 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493f2e0f165sm270374515e9.0.2026.07.14.07.33.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jul 2026 07:33:20 -0700 (PDT) Date: Tue, 14 Jul 2026 15:33:19 +0100 From: David Laight To: =?UTF-8?B?5a+/5p+P6IO9?= Cc: Sumit Semwal , Christian =?UTF-8?B?S8O2bmln?= , "T . J . Mercier" , Benjamin Gaignard , Brian Starkey , John Stultz , Sandeep Patil , "Andrew F . Davis" , Srinivas Kandagatla , 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 Subject: Re: [PATCH v3 1/2] dma-buf: dma-heap: don't publish fd before copy_to_user() succeeds Message-ID: <20260714153319.07b2b1e1@pumpkin> In-Reply-To: References: <20260714114654.3885457-1-shoubaineng@gmail.com> <20260714114654.3885457-2-shoubaineng@gmail.com> <20260714141359.7758575d@pumpkin> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Tue, 14 Jul 2026 21:38:07 +0800 =E5=AF=BF=E6=9F=8F=E8=83=BD wrote: > Hi David, >=20 > Thanks for the feedback. >=20 > The concern is not just about the EFAULT return =E2=80=94 it's about the = race > window between fd_install() and copy_to_user(). Once fd_install() > returns, the fd is immediately observable by other threads in the same > process (via /proc/self/fd, SCM_RIGHTS, etc.), even before > copy_to_user() has a chance to fail. The triggering condition is a > deliberate mprotect() flip, not a corrupted heap. That is what makes doing the close wrong. But that is a program aggressively trying to hit the timing window, not a normal program that has managed to pass an invalid pointer. The most likely reason for a real program passing an invalid pointer is a corrupted heap (assuming the stupid coding errors are fixed). It is really no different from the sockopt code that receives SCM_RIGHTS messages. In that case once you've removed the FILE from the socket (or similar) you really don't want to have to put it back because the write to the sockopt buffer or length field fails. The chance of correctly reverting the kernel state is small - and won't be tested. David >=20 > The fix itself is small and follows the standard kernel idiom: > get_unused_fd_flags() reserves the fd without publishing it, so the > window between reservation and install is entirely under kernel control. >=20 > Baineng >=20 > David Laight =E4=BA=8E2026=E5=B9=B47=E6=9C= =8814=E6=97=A5=E5=91=A8=E4=BA=8C 21:14=E5=86=99=E9=81=93=EF=BC=9A >=20 > > On Tue, 14 Jul 2026 19:46:53 +0800 > > Baineng Shou wrote: > > =20 > > > DMA_HEAP_IOCTL_ALLOC allocates a dma-buf and installs an fd into the > > > caller's fd table via dma_buf_fd() -> fd_install() before > > > dma_heap_ioctl() copies the result back to userspace. If the trailing > > > copy_to_user() fails, userspace never learns the fd number, but the > > > fd (and the underlying dma-buf reference) are already visible to > > > other threads in the same process and are leaked for the lifetime of > > > the process. > > > > > > The obvious "close it on the failure path" fix is unsafe: once > > > fd_install() has run, another thread can already dup() the fd, send > > > it via SCM_RIGHTS, or close() it and let its number be reused, so a > > > subsequent close_fd() from the ioctl path can operate on an unrelated > > > file. This was pointed out by Christian K=C3=B6nig on v1 [1]. =20 > > ... > > > > My 2c: > > > > The other option is just to leave it as a 'problem for user space'. > > No reasonable program is going to handle the EFAULT return by doing > > anything other than exiting. > > Even getting an EFAULT is really an indication that the application > > is already in a real mess - most likely with a badly corrupted heap. > > > > Anything else leaves error recovery code in the kernel that is pretty > > much never executed and open to a variety of bugs. > > While the recovery here is probably ok, there are some sockopt calls > > where it is all more complicated. > > > > David > > =20