Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2358022rdb; Mon, 5 Feb 2024 04:19:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IGKJDqwamAxFU0IuePj2SXXJvPn+zyMm+QhjtT6AtaoTSAzMq+yf4sRsE2oAfEWucAlRSsh X-Received: by 2002:a05:620a:410a:b0:785:61c7:6b24 with SMTP id j10-20020a05620a410a00b0078561c76b24mr7937080qko.4.1707135561008; Mon, 05 Feb 2024 04:19:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707135560; cv=pass; d=google.com; s=arc-20160816; b=EEA34ky+tTxDVO/BDyZ4kHx4APj8F00VHx3YS4arQ6duGN66PPTgc7tTYIfkVspg1u pWf8q/w2g/3R3sc5z3OeY/jwN47U7wSWFgtyJyLNp0yw4zsChilnTAutGCvyyYRRQV3m Y4MlMhB0CAZvoXw8tBzLtPyuaPcokoRGY4dqZCbUD5dB/Rnos53RgsuQOMNQO4Uc0heH aLjyT+mQm8gORX4wTEaaZoUJIqpbrJIN5SswqXUWD0H9lsODWGjowoEsH6fhH+Vc3OvJ 0YVNbrkBN9NJrOjj7mNvkL1CYvmg3lnUZ0AK1p2GpK5uQBn78lf1Mzo2vkkWjUjS4HXE u0ww== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=Weor1BS+GjUl++QK7p/VyHltAw7gfa56ze5aB5NLLeI=; fh=psDG1NZuKNB3e11EoSiH4xN1teWuZBT0Wn14hLe9QUI=; b=Z4KHXdyDWCJBrryvuYMm9zV10B4srny2k2oD9TVkwbelTwsxZJ0YFVCqwIR+W7YM91 tMF6TA6HTuPkBP0kV8D+rG9YDlYTKQsZ7+x/tfO2htDDDCL7Tf94cJMxf1n58g/yNgsC Eu69JlGnx+Z1SUYgLZEAy2rismPJe8q2L+U4ugUBhEsMKYkj/EEd7VAwXZv0o5TOyWgx H0luhA5Ek10eQcUQGsK3lx0D3nSGZNZslB/MVKLdF4EtZt+eP12YAG6nRYxufDHKVLng apIPx7+WlU4pBYpSQYTWPRwHx2DgKT+Ejc2hJl9d9hFvvyQ7yR9vTg4ru+lbRWRd7rbn wdxg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=YlIgIGRf; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-52585-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52585-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me X-Forwarded-Encrypted: i=1; AJvYcCVa2z8P0jDijs4BBYNeZGzDRCPh0LziITq6kKxkT3cE1M2+0aba7NC4ZWqccl1YU40lf5UXJExdrGDpSPEBPq+AYu11zyUyi2NGZ1RhAg== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id z23-20020a05620a101700b0078333a213cdsi8363485qkj.368.2024.02.05.04.19.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 04:19:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52585-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=YlIgIGRf; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-52585-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52585-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BFB081C22BD1 for ; Mon, 5 Feb 2024 12:19:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2DADF1B95F; Mon, 5 Feb 2024 12:18:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="YlIgIGRf" Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD75C1B948 for ; Mon, 5 Feb 2024 12:18:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707135520; cv=none; b=GBKOkJBuTZciFaBkuvuWLkVf4IoWA5IYNwVcCuLm73tRPPoXlIK42zcupw+ZnMR6FGfE5F3z2WjaiI+Z+W6fXTQUJh1kI/4Nw76VM9hckg5G/qzzCSUdkSuNqn2zvQDBKrevn3MxugVa+yBE7zmU5JmAcWcTH4G1W9uZWyWYELk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707135520; c=relaxed/simple; bh=Ysr9xtLV4DrX5hUg539QdBNlVtKZkT6XZzfhMfs+j3I=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=t1oxBpmqd0QNndbeDMXf9zXUOT0yQXgDy2TqvpJLw02q/fd6jZZh1SV8C+xvdryJGO67+J8zY89KLWXxtp8t+z5QsAAXsU1Epx/W/74z05A/XOLAgxYMwgomLeZ3UMobw65WOk9KGRftHQQIsFiYw3TNOEY7DSbnn+TYBxVz560= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=YlIgIGRf; arc=none smtp.client-ip=185.70.40.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1707135510; x=1707394710; bh=Weor1BS+GjUl++QK7p/VyHltAw7gfa56ze5aB5NLLeI=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=YlIgIGRfvEO3k9det+3i+l6muBxeH6zeZ/tcO+ysqcA7PsuK0N3Qv/Ha33jOIcnmj yK+bdDBG4loaRK9Spqh03gqrcUt9vT/ZQEIumTpvGX7/xC+wQc9/1lxMO2xG0rUtDT N6SdpKi4xoBELSy+CXmjyz00NbUDH/B00083CyDe7nkIF6PqgYnC9i3CWptWgfmKaQ GigpldkEtp5j40t8B9TqaFt5iQM/xCo+bErSmFKel6X6LFjIndoGuIQdZcURJLimAD /pEZz4Ej8IxpAB1LiDW5SoR6Knx06fY4TXLHVcIPU2NISxsrI9kkmTtx70JuV6B82Y 4uUlOxpKq5tVw== Date: Mon, 05 Feb 2024 12:18:19 +0000 To: Alice Ryhl , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Peter Zijlstra , Alexander Viro , Christian Brauner , Greg Kroah-Hartman , =?utf-8?Q?Arve_Hj=C3=B8nnev=C3=A5g?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Dan Williams , Kees Cook , Matthew Wilcox , Thomas Gleixner From: Benno Lossin Cc: Daniel Xu , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v4 8/9] rust: file: add `DeferredFdCloser` Message-ID: In-Reply-To: <20240202-alice-file-v4-8-fc9c2080663b@google.com> References: <20240202-alice-file-v4-0-fc9c2080663b@google.com> <20240202-alice-file-v4-8-fc9c2080663b@google.com> Feedback-ID: 71780778:user:proton 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: quoted-printable On 2/2/24 11:55, Alice Ryhl wrote: > To close an fd from kernel space, we could call `ksys_close`. However, > if we do this to an fd that is held using `fdget`, then we may trigger a > use-after-free. Introduce a helper that can be used to close an fd even > if the fd is currently held with `fdget`. This is done by grabbing an > extra refcount to the file and dropping it in a task work once we return > to userspace. >=20 > This is necessary for Rust Binder because otherwise the user might try > to have Binder close its fd for /dev/binder, which would cause problems > as this happens inside an ioctl on /dev/binder, and ioctls hold the fd > using `fdget`. >=20 > Additional motivation can be found in commit 80cd795630d6 ("binder: fix > use-after-free due to ksys_close() during fdget()") and in the comments > on `binder_do_fd_close`. >=20 > If there is some way to detect whether an fd is currently held with > `fdget`, then this could be optimized to skip the allocation and task > work when this is not the case. Another possible optimization would be > to combine several fds into a single task work, since this is used with > fd arrays that might hold several fds. >=20 > That said, it might not be necessary to optimize it, because Rust Binder > has two ways to send fds: BINDER_TYPE_FD and BINDER_TYPE_FDA. With > BINDER_TYPE_FD, it is userspace's responsibility to close the fd, so > this mechanism is used only by BINDER_TYPE_FDA, but fd arrays are used > rarely these days. >=20 > Signed-off-by: Alice Ryhl > --- > rust/bindings/bindings_helper.h | 2 + > rust/helpers.c | 8 ++ > rust/kernel/file.rs | 184 +++++++++++++++++++++++++++++++- > rust/kernel/task.rs | 14 +++ > 4 files changed, 207 insertions(+), 1 deletion(-) >=20 Reviewed-by: Benno Lossin -- Cheers, Benno