Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3496982rdh; Thu, 28 Sep 2023 13:30:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHkW+tHMlObQdrBz0X5HikNNRrAlGXWRYFMFJ/jUZuzTyw2t/YIY+GyTs6n+2yAXQj0N4Ks X-Received: by 2002:a05:6358:4298:b0:143:8af4:229e with SMTP id s24-20020a056358429800b001438af4229emr2385894rwc.9.1695933019779; Thu, 28 Sep 2023 13:30:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695933019; cv=none; d=google.com; s=arc-20160816; b=xE0MOQ93yJPRaiMLPZA/5s4zRt0EfYzAomIsuHqcV/reJbS/uqgN0utL2RocOC/BKA OJccqFhNPjmNJ4oxS4F4AOeiIP6mqLBOpAglqyRCe1PGDI+MoZ2UN+3dQlG7T9nLDKWH hH46obvsne/mZY06fn6yx5nqD5ptQ2AXg8SPR0pIbHBW3PZjtWR25m3dzPmqSeVvVRs0 waMpdJdQ6wkPPzVD0Qv36Zhlaxs7wRzOHue7vwb4r8sMQmre2rNUFeiOTLQR82lzWTNB Iv2S8sDHrsPX4235JQB9vGjY8GyYUhQNzA6w3csP25Nqd6xvNyA8YMw4rGZUnnGyeQOR LgDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=jbBBWlIG8ShXasc+/VZ6EV1FLcrQl5lcjcGdAjjkp7k=; fh=sbFywvjJ7agYhlF0MpCaxRXXmJsOoGes9mspPj3Y9MQ=; b=xw2VsUSudoV/dQq1QZLO0WcH7NZJb5QXbfjZyTDb1DGu7yVrIShUpJT0oePy5/oLoG ptlJT1l+qtSswmrK3YyOwiXoyAYEYkxhnoq7aIOIEKajJe6A6QwJB7BLSgflw0CGDNxV LphGscWrkUUs86uOzwLGsOEQFgTj+wVOb2/BOU1sR/ATCKMomIgmL/AkNrJezmOwFscs V7fQzZb5UY7aM1ZQ6R3YWmqKtZXcrs5ydteMgW9M2meu/vEs8XHbdRuMDcaZyeOgulaU ti+yWeajtl1zj5vWT3KQqI+Z3qYTCcTeN8/uGGgQJVX6LuTTKJ/UKwVXHnnYPAD/46W4 9EUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=ZU6ZHB1G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id x185-20020a6363c2000000b00578e9a29ee4si20138203pgb.240.2023.09.28.13.30.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 13:30:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=ZU6ZHB1G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id DF2C181FDB91; Thu, 28 Sep 2023 10:22:33 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231534AbjI1RWV (ORCPT + 99 others); Thu, 28 Sep 2023 13:22:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231935AbjI1RWT (ORCPT ); Thu, 28 Sep 2023 13:22:19 -0400 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DF5DDD for ; Thu, 28 Sep 2023 10:22:16 -0700 (PDT) Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-9a645e54806so1682494866b.0 for ; Thu, 28 Sep 2023 10:22:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; t=1695921735; x=1696526535; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=jbBBWlIG8ShXasc+/VZ6EV1FLcrQl5lcjcGdAjjkp7k=; b=ZU6ZHB1G6hrLMPSGlFT8Mt0LpqT8WdAbk7Jr/BInTp5SM6EkvLRAAgUhTQhwi4yDVf WYKRdzhuF5QBKZeuyA6aeDuKcJGjwU4XcrdV1jC27ron9kC1hjGq/ciXzCVSvYIdKLxu P27sACuCpDNP0U8veXpJuclNsl4gdkIsyRi00= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695921735; x=1696526535; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jbBBWlIG8ShXasc+/VZ6EV1FLcrQl5lcjcGdAjjkp7k=; b=iGOvbk1J0ma26ymA7PERqbX8xBNMVi6mUakMGP0OfP2aiW5Mxa3/tD7XanNFsRqCl/ GPwk2GpW+R2VMB8/ojGgtGSagxLvIZFWc44VN78FAtSlWU6TbXdRy9n+WS8qSXKrfonq QGeD1+6SbnecJOzCnOd/B9jSiJFN4qp9MHr5qighm5oEVJVS9wLXihPrHFJWhmSFZdTn dqJ26oFrKbIhY7GR8X2z9vaLw6YQcxfubUuu9T+GtQj2GFyagSaeWTqk2hDgPs7MaZZ8 YGMA/HilMADSG6l4J831pXDhHPqYgAwIGs5QxWTHU+5cg2j0qT2/5E45UVdu7NogYddf aDCQ== X-Gm-Message-State: AOJu0YyBUwujKSc5j7MiwmulLWRLaAp6pGQ6hhuGYk0/MpQQjjSs8K0X tUKDOPXahuiB3UyfX4LyvKgz3S5gEBli6qDcqGLxpuq7 X-Received: by 2002:a17:906:209e:b0:9a2:120a:5779 with SMTP id 30-20020a170906209e00b009a2120a5779mr1811530ejq.60.1695921734848; Thu, 28 Sep 2023 10:22:14 -0700 (PDT) Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com. [209.85.218.42]) by smtp.gmail.com with ESMTPSA id kg28-20020a17090776fc00b0099b921de301sm11178509ejc.159.2023.09.28.10.22.13 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 28 Sep 2023 10:22:14 -0700 (PDT) Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-9a9d82d73f9so1677189566b.3 for ; Thu, 28 Sep 2023 10:22:13 -0700 (PDT) X-Received: by 2002:a17:906:32cb:b0:9a2:232f:6f85 with SMTP id k11-20020a17090632cb00b009a2232f6f85mr1865368ejk.52.1695921733456; Thu, 28 Sep 2023 10:22:13 -0700 (PDT) MIME-Version: 1.0 References: <20230926162228.68666-1-mjguzik@gmail.com> <20230927-kosmetik-babypuppen-75bee530b9f0@brauner> <20230928-kulleraugen-restaurant-dd14e2a9c0b0@brauner> <20230928-themen-dilettanten-16bf329ab370@brauner> In-Reply-To: From: Linus Torvalds Date: Thu, 28 Sep 2023 10:21:56 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2] vfs: shave work on failed file open To: Jann Horn Cc: Christian Brauner , Mateusz Guzik , viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Thu, 28 Sep 2023 10:22:34 -0700 (PDT) On Thu, 28 Sept 2023 at 07:44, Jann Horn wrote: > > The issue I see with the current __fget_files_rcu() is that the > "file->f_mode & mask" is no longer effective in its current position, > it would have to be moved down below the get_file_rcu() call. Yes, you're right. But moving it down below the "re-check that the fdt pointer and the file pointer still matches" should be easy and sufficient. > There are also some weird get_file_rcu() users in other places like > BPF's task_file_seq_get_next and in gfs2_glockfd_next_file that do > weird stuff without the recheck, especially gfs2_glockfd_next_file > even looks at the inodes of files without taking a reference (which > seems a little dodgy but maybe actually currently works because inodes > are also RCU-freed?). The inodes are also RCU-free'd, but that is indeed dodgy. I think it happens to work, and we actually have a somewhat similar pattern in the RCU lookup code (except with dentry->d_inode, not file->f_inode), because as you say the inode data structure itself is rcu-free'd, but more importantly, that code does the "get_file_rcu()" afterwards. And yes, right now that works fine, because it will fail if the file f_count goes down to zero. And f_count will go down to zero before we really tear down the inode with file->f_op->release(inode, file); and (more importantly) the dput -> dentry_kill -> dentry_unlink_inode -> release. So that get_file_rcu() will currently protect against any "oh, the inode is stale and about to be released". But yes, that protection would be broken by SLAB_TYPESAFE_BY_RCU, since then the "f_count is zero" is no longer a final thing. It's fixable by having the same "double check the file table" that I do think we should do regardless. That get_file_rcu() pattern may *work*, but it's very very dodgy. Linus