Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1109504rdb; Wed, 6 Dec 2023 08:50:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IEHbCQBhGRFI5YWGN9SGhKhH1eSlBnOt7QTDRAEZEYK9GeQY2y2VIGqw0QxO1EtwXtv0O2t X-Received: by 2002:a05:6358:904e:b0:16e:29bd:37be with SMTP id f14-20020a056358904e00b0016e29bd37bemr1625969rwf.28.1701881405166; Wed, 06 Dec 2023 08:50:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701881405; cv=none; d=google.com; s=arc-20160816; b=TQzrIMZEBhxkhzrtUWc1m9khwULSfdYn0Nq5fymFOUjH+23F+AjQpx+mb/2V73/JxS /HnI/8ITgd7HiI4FTto7SnwtsMd5upyvzeaeHc+w0MlY2qs5P8i78S/pwALn4XXKQ7JK ibAlkGzfwo8iaR05CLZUZRjwADZkeVs4Q1T+DDVnXo7AgjJHzrRKiizg17OmqQgQag84 xVMMhl2MVTByo4kuOKr/obTm09O0y62eSgh+n/oihD4p6+aXvegWSyBiuyrTYFXNnBVM NvOFB2vMZFK4x4BMNrlv0Y7ziAQ0U+qG8FtNEj+MR3tnD3FrYiIozVZ+LFLl4MvDNicA Mhlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ERFfW62XDL6AD8643HMOYi7IE6HyEqiatYzrL78yKhM=; fh=kjt2qbL6Rtx7kW4MuJCrfq/exLwjXIyOYXMf/8vkhTg=; b=JogQ2JJSWtx4q40GA3eakj1TTx7B8840M/k+or98fQnEUEdWb7dm7aoMZH6azX5IHK ZhomnAJIKm3yn2Slxc5zCBh+T1RfX0SLYMT5F8iQ3PuXxFunVrGtr8/zdERlRV8Om0hP M9k0XN/S5+4/ZXf+b5USh6S0ENe3B391847qhlxKt3Qc4NhRbw3YqHm0byzfL1fkirTq DuE8R2nLZTnPgvx2kODBFRkNMssBsv4HQe3oGlwr8KMilNzK7QjpikA3sJCSJzRyC7KA aRv7rs9Hy6Q3YJIdOZBsy02jWtSEvpebm3PZ/Bn15+UI1rY+kU8CWKz7fRwmt68djo9x cUNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nxXEzb3u; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id e7-20020a056a001a8700b006ce37b9d2b6si205208pfv.253.2023.12.06.08.50.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 08:50:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nxXEzb3u; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 0A44C8028A5E; Wed, 6 Dec 2023 08:50:04 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442610AbjLFQtw (ORCPT + 99 others); Wed, 6 Dec 2023 11:49:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378787AbjLFQtv (ORCPT ); Wed, 6 Dec 2023 11:49:51 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29ED9B2 for ; Wed, 6 Dec 2023 08:49:58 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BFF0DC433C8; Wed, 6 Dec 2023 16:49:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701881397; bh=rMAoUjOmr5QtbI6rn6eo4WOMrnTR1D6zBGzrt2Afy9A=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nxXEzb3uTcRmZMsipF5O+JnEPiOGWtYp0M/22/GFpAre2r31rmbRgosxfeQy7MBQX kq8PRy7iC0/1XmiUQDXu771Y4nV++RTEguPHotHNSz/zO8Sxmi8ZRt0oc0eGgydDKU 6z4+YiNKQsrUHsKsGlGi5GrM0njU/F+OjNTDACkIJ4+7s+F+IMG1KvaQFi9ZSbRohG x/2v6f1Eypy+DwRBuqEQt18Z4FTSuZe4spozrr8nFoBOw+otVxNv0UhACO7mKs7tOe wyI2+KtO36rz1L/KZv6o1fr6TvEOCuHeLv9/jd26pDrwgG1ZDVWRLVRQGYE0pOaUnp vr1RQcbqs13XQ== Date: Wed, 6 Dec 2023 17:49:51 +0100 From: Christian Brauner To: Jann Horn Cc: Phil Sutter , Pablo Neira Ayuso , Jozsef Kadlecsik , Florian Westphal , netfilter-devel , coreteam@netfilter.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Network Development , kernel list Subject: Re: Is xt_owner's owner_mt() racy with sock_orphan()? [worse with new TYPESAFE_BY_RCU file lifetime?] Message-ID: <20231206-fixpunkt-annehmbar-d191785a09a3@brauner> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Wed, 06 Dec 2023 08:50:04 -0800 (PST) On Wed, Dec 06, 2023 at 05:28:44PM +0100, Jann Horn wrote: > On Tue, Dec 5, 2023 at 10:40 PM Phil Sutter wrote: > > On Tue, Dec 05, 2023 at 06:08:29PM +0100, Jann Horn wrote: > > > On Tue, Dec 5, 2023 at 5:40 PM Jann Horn wrote: > > > > > > > > Hi! > > > > > > > > I think this code is racy, but testing that seems like a pain... > > > > > > > > owner_mt() in xt_owner runs in context of a NF_INET_LOCAL_OUT or > > > > NF_INET_POST_ROUTING hook. It first checks that sk->sk_socket is > > > > non-NULL, then checks that sk->sk_socket->file is non-NULL, then > > > > accesses the ->f_cred of that file. > > > > > > > > I don't see anything that protects this against a concurrent > > > > sock_orphan(), which NULLs out the sk->sk_socket pointer, if we're in > > > > > > Ah, and all the other users of ->sk_socket in net/netfilter/ do it > > > under the sk_callback_lock... so I guess the fix would be to add the > > > same in owner_mt? > > > > Sounds reasonable, although I wonder how likely a socket is to > > orphan while netfilter is processing a packet it just sent. > > > > How about the attached patch? Not sure what hash to put into a Fixes: > > tag given this is a day 1 bug and ipt_owner/ip6t_owner predate git. > > Looks mostly reasonable to me; though I guess it's a bit weird to have > two separate bailout paths for checking whether sk->sk_socket is NULL, > where the first check can race, and the second check uses different > logic for determining the return value; I don't know whether that > actually matters semantically. But I'm not sure how to make it look > nicer either. > I guess you could add a READ_ONCE() around the first read to signal > that that's a potentially racy read, but I don't feel strongly about > that. It should be possible to split it into two static inlin helpers: owner_mt_fast() owner_mt_slow() And then abstract the lockless and locked fetches into the two helpers.