Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1844815rdb; Thu, 7 Dec 2023 10:10:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IH7VPirDj7AspcDVJhrG1BxB1ZxN9ppgcvcKtZrRWlIBXfetJPu+uVmWu3YEKtA+5/gjLzV X-Received: by 2002:a17:90b:607:b0:286:44ca:425a with SMTP id gb7-20020a17090b060700b0028644ca425amr6029322pjb.18.1701972632968; Thu, 07 Dec 2023 10:10:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701972632; cv=none; d=google.com; s=arc-20160816; b=ls0VIq7Y5sbRskEw+MvuWM+YyUr46ILerFWaEv8bx7MbQ0MiFqvFd/Semn/HOWAch6 v20MYv30f0tY71vqm/fBxOkBtOZEy97i4O+cT+DgW0OSMyM0RZCx9z+93sHLH44loDhV FxO03Kr/r/OzZtzVEm3UMqfNl1XJ7ESP5ZYhEC9lOIFmdqojYPn+05Xw+AvhD/t3lTYh oI7WrGeu71vX7GMJLyMSYOZ5c9yOyZMFJGcuttPFvwDoY22oIrprKQWpQqWqszBrtEKk gwFeCntKE7IO3K9ETquApUTzVQ/L543euQ6uGphqnVcnLxepbmC/L2ZBcw0sAIjo2o3E cUcQ== 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=tfIWnqZ3nQOuHBXi2xMiPcBXFezRuvO6di/KOho/tPw=; fh=cI+9URTgFpley0bzfT0QiKmI9whewfFNGUJ+V1kiTqI=; b=NNKwG15fp4Wgslzo58h0EpcJqpQdLVl7p2soTuaX42ladfcvHDTq6yfMTolYTYQUup Bb8ITxIWRjOIB0PpU5A2sPVaDc2tYgTJE7mGjs/RymSZ5wUjIB+IzS+XJZ36aH2jXZxr TChIyFr9ksRpF4BLPiLBm0aUjaV8bdT7RmiF6dfrz2oY0z9YFhfO6Jd2D+eVPK1VoRoT vf/jWe7FxzH2moM7ybZRRsNGFbMCrBgWei1wg55SYiEpdnepINytiLYTPufpYsXG5Brh zKG7sMKJgXm70YW2IxTbBJ5q2CHwsitpZjJrz0BKQaVg4gx139yWDOYR+YkmLk9mHo+J 34Mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nwl.cc header.s=mail2022 header.b=onMd3X3S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id x9-20020a17090a970900b0028865463406si1506785pjo.51.2023.12.07.10.10.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 10:10:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=fail header.i=@nwl.cc header.s=mail2022 header.b=onMd3X3S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 6166683EB323; Thu, 7 Dec 2023 10:10:00 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443661AbjLGSJi (ORCPT + 99 others); Thu, 7 Dec 2023 13:09:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443657AbjLGSJh (ORCPT ); Thu, 7 Dec 2023 13:09:37 -0500 Received: from orbyte.nwl.cc (orbyte.nwl.cc [IPv6:2001:41d0:e:133a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 859531716; Thu, 7 Dec 2023 10:09:41 -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=tfIWnqZ3nQOuHBXi2xMiPcBXFezRuvO6di/KOho/tPw=; b=onMd3X3SuTwSLahRkZwZpue9ag eVHFIhln8ZggmQOkmvTNEAxCHsfAZVlYrwD+kUuMBpmOgv3NClRTzVeNPAggdKSi/OUcevMR+gwmg M9qY2ajG4HZPhSvVXEnMeX44wGLpyZwL7dhxk/Tbq/3Y2ouDhpbrARu9jPUAnbv8ToKz6j27kEzsY bruJGfTsZoUiweDpyxtzRCO6MEy9LGKQmLRWJp/u+XVPursh1UH993u0eXzKty9oRbl7HhRVSe7MB DmwblMNTNno9ugtOxezOFSTvTnWJqWpAYzbMC7TMaleR2P/MtnxMPlLhaX+X5l5nrGezi1K6WR581 EMjNXDGg==; Received: from n0-1 by orbyte.nwl.cc with local (Exim 4.94.2) (envelope-from ) id 1rBIoa-0008TT-RL; Thu, 07 Dec 2023 19:09:36 +0100 Date: Thu, 7 Dec 2023 19:09:36 +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 howler.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 (howler.vger.email [0.0.0.0]); Thu, 07 Dec 2023 10:10:00 -0800 (PST) On Wed, Dec 06, 2023 at 10:02:04PM +0100, Jann Horn wrote: > On Wed, Dec 6, 2023 at 9:42 PM Phil Sutter wrote: > > > > 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? > > I mostly just meant that as an annotation. My understanding is that in > theory, racy reads can cause the compiler to do some terrible things > to your code (https://lore.kernel.org/all/CAG48ez2nFks+yN1Kp4TZisso+rjvv_4UW0FTo8iFUd4Qyq1qDw@mail.gmail.com/), Thanks for the pointer, this was an educational read! > but that's almost certainly not going to happen here. At least it's not a switch on a value in user-controlled memory. ;) > (Well, I guess doing a READ_ONCE() at one side without doing > WRITE_ONCE() on the other side is also unclean...) For the annotation aspect it won't matter. Though since it will merely improve reliability of that check in the given corner-case which is an unreliable situation in the first place, I'd just leave it alone and hope for the code to be replaced by the one in nft_meta.c eventually. Thanks, Phil