Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1243740rdb; Wed, 6 Dec 2023 12:42:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IGP5h7qq1frQa2ihbYArLL4odYMunA4whzPfI5+7wZcDKfGZ2CAEobseBatJzQp5aFSCTs6 X-Received: by 2002:a05:6a21:188:b0:186:de7:e803 with SMTP id le8-20020a056a21018800b001860de7e803mr1557726pzb.53.1701895372760; Wed, 06 Dec 2023 12:42:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701895371; cv=none; d=google.com; s=arc-20160816; b=Fs42C15gBwI60On1stXDB268IKt3ieXBxygLnDr2PYi2WTQANDhjEV7QCUCm2tjKdV 01IdpZB74z0Wy+hN/68WW8i5QyfWeui0MUD+eBgvHexHSCg8XKeQ68oW5qfJk6vPk/Nk wZqyMiD4LM6G6ZF/ASLEA8wjE3bbycI6ZTq00O+e+6TG+fwplWO2gBzsgz2fediCIWHw dvWZvT+gibp431Nw/Z6vyKewTTd7n8fZ2vMeU0fMXR9OYpecWtm5poQUutChNpXgySHp 7+pnc8tTMOOI4DeFlDfda6WZkrRaDY7XnOMZFAk+b0cFfkfO0dg+DQR5wIfPmScVnJoC hBxw== 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:mail-followup-to :message-id:subject:cc:to:from:date:dkim-signature; bh=5X7bJGIMfmpn3bbdaGyZfOpNZRgMQhcfVmmJOst3z7M=; fh=cI+9URTgFpley0bzfT0QiKmI9whewfFNGUJ+V1kiTqI=; b=M3OUNRp2Zwc666bHqKbjYI9xHBo97k85HuSB9i3b6Zlq10brNvWsayZWMBlF51xHJ4 UI+H45nVl80OIp+/PhoOH68tks6634Ve6YY1WBroThY3Csai9V/n/AnQwLNXxSBuV8zQ oH2JW/CDRDQeE8sSwfvI8APaUYl3qwVwrK9KK+IgD8wJ8vFGQ+CsZ1cZL42sczd6PeRU LlMcFaYJGYmO4OEpzgVG8assDnKNQhuFXsaWewMQl71ghdXiAEqucsL2UoU7VAj4dZMN 9nDp3Sgw7+UouGTMjAPu3cotDxTyzzA2JdeLhrvdYz9FJ5z8GnBJ9B5LaMIWXwaKdX2N ogNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nwl.cc header.s=mail2022 header.b=fJJ3Gea7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id u13-20020a056a00158d00b006ce826158f0si499471pfk.111.2023.12.06.12.42.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 12:42:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=fail header.i=@nwl.cc header.s=mail2022 header.b=fJJ3Gea7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 7FCE280AF24A; Wed, 6 Dec 2023 12:42:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379489AbjLFUmb (ORCPT + 99 others); Wed, 6 Dec 2023 15:42:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230291AbjLFUma (ORCPT ); Wed, 6 Dec 2023 15:42:30 -0500 Received: from orbyte.nwl.cc (orbyte.nwl.cc [IPv6:2001:41d0:e:133a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5A17CA; Wed, 6 Dec 2023 12:42:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nwl.cc; s=mail2022; h=In-Reply-To:Content-Transfer-Encoding:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=5X7bJGIMfmpn3bbdaGyZfOpNZRgMQhcfVmmJOst3z7M=; b=fJJ3Gea7zb7nFaK23Yzspw8VxF 0kCgM38MKJ/yd3J1SX/jSt3A0Dh47YcF4C35ilwrwLCCX68r6ibmq5ixJtCS9cxFtXN9W2u8/IKnf C0lm7ANdk+o2exFsA/0liT88VpjpIQzlqD0Z5791AQq4jUIUIlN+tmzcRmexnpWvLvrdeP5h2pYUL L/VbJhciTG05DEkExeG+oDxoTxXCDnLtoQD1B1z7bHiXdD1zZjuX3s80Vd8Z+HD9qPFr7yZ4BRx9V frH+f0S8hB2iF7+A5HFOm3nnz2h/Mwmen/SFhJ1aQHB0/eH+9KtOVV43wsN6mBmsa7NRdIAhjg/ue c1rnIl7g==; Received: from n0-1 by orbyte.nwl.cc with local (Exim 4.94.2) (envelope-from ) id 1rAyiz-00042g-Sb; Wed, 06 Dec 2023 21:42:29 +0100 Date: Wed, 6 Dec 2023 21:42:29 +0100 From: Phil Sutter To: Jann Horn Cc: Pablo Neira Ayuso , Jozsef Kadlecsik , Florian Westphal , netfilter-devel , coreteam@netfilter.org, Christian Brauner , "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: Mail-Followup-To: Phil Sutter , Jann Horn , Pablo Neira Ayuso , Jozsef Kadlecsik , Florian Westphal , netfilter-devel , coreteam@netfilter.org, Christian Brauner , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Network Development , kernel list 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=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Wed, 06 Dec 2023 12:42:48 -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 find the code pretty confusing since it combines three matches (socket UID, socket GID and socket existence) via binary ops. The second bail disregards socket existence bits, I assumed it was deliberate and thus decided to leave the first part as-is. > 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. Is this just annotation or do you see a practical effect of using READ_ONCE() there? Either way, thanks for the review! Phil