Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1110139rdb; Wed, 6 Dec 2023 08:51:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IGjGHz3iboWoyYRFUqSDdgAZ5Wo6ASegBw3C2q9xFt7GtzcPaxri2ZUpj5MDQv0iE3YKtzp X-Received: by 2002:a17:903:503:b0:1d0:6b95:9c06 with SMTP id jn3-20020a170903050300b001d06b959c06mr751667plb.16.1701881480741; Wed, 06 Dec 2023 08:51:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701881480; cv=none; d=google.com; s=arc-20160816; b=H0heVfSR0oo+tDnOQugMRcpt6ABkviPNxQfo19Ir4qkkfJRiopTM3VK+XAapHwVps5 It2KB2EUF6Jp4V/0qKSNCbR4Mwoaer+gVSoqUcCNuWG+Mym+nTDvOdgn8YnHtby2Vo41 XOQfiNHMiQyH13a52f72nE3S/1mcGTbpyov8fL6B1ls1h/GpaasBlC7GcBN5H/gbRsyk GSCv4jnqb232zMNnhqMEFmJcvy8o5lZY4KI6k11J9prJQDLJwmK3tJyijbMmyaQCNizX rA4WZbNFwxAEyQO0zrk1N5N7+NDrDDkiKmoP6ONeQ+3Tw+P9Mo9Cb9htMyCH4jATfZ3L biew== 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=ZFUwI1vtg3iyh4tOJM66h9QmBwoKmVX49jLrkXblqJA=; fh=G6Sp6BgzL8tAftW0vdwNjzvw77GaLnYGmkm8Geqg+3g=; b=Y52kYiP00zoVVwnqmB+ervHmN1u5r7ne0Tj/YJ06jHZth8Ju2CLqgPdNfEDVILdRWO bs1FU1dIgtZAULg6UmFGl0P7ksy20UR4MvJ5223lpPYcLZuqgrM6rTaTe2hbPg+hbDIx rKCEy6U0iaLRBtrifmkDGnrbykAEqk2jJddqX0fUa2j/qVyRqfoLAu+nBzVKQLkMejLP fgEw1O2B6dMzhLPFTPaaUXfKDw6FxNma4NM/7pQX6tRxaTehjGZKCzV9WeEAsZbRGSYm nXUUhiwSjlvYtEKGm5wV/w+pm04QKTFMXdGVFzsWT2szcnP+MZqHgkQfc+RYnk9KIOP9 3B2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hcd8ItYO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id a20-20020a170902ee9400b001d0aeda9adbsi64010pld.300.2023.12.06.08.51.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 08:51:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hcd8ItYO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 2D9998080E2D; Wed, 6 Dec 2023 08:50:44 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379735AbjLFQu3 (ORCPT + 99 others); Wed, 6 Dec 2023 11:50:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379377AbjLFQu0 (ORCPT ); Wed, 6 Dec 2023 11:50:26 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3260D7F for ; Wed, 6 Dec 2023 08:50:31 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8CEA1C433C8; Wed, 6 Dec 2023 16:50:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701881431; bh=uOaUV/x5ijC7KJbVV3edKrjS8niRkEle6TaN8naPFz4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Hcd8ItYOoFEC/bFxRgyDSQg4eDgEi7NjBZZ1K/bZpZTpJ4dMCQ2Bavo/o4asTygME Lxfuz8yvWNOS5re1U2V/UJzmZWRccnkWGE8IjSaq+2/VdNt1LHJX2E9SQEisfDhIfs OtQuwh9qJk35wCLoovp6UQOpKs3aDg+AgBlHFoJTRCsnzH044NSgig0OOjQGe8Y+vG spsC7mWYl1gPMw3pRUkKTwAE+C8J4/oe7iDKYDrYpXIBTM7wUFWbPzLYj1N9iBK6UR J3kSMjADE2RKl1gufKnQATapZUN22fUEKf6jWHVOoRQb07o9twouWzUrV5mtQhE/Fd hgpQyfspEzoRQ== Date: Wed, 6 Dec 2023 17:50:25 +0100 From: Christian Brauner To: Jann Horn Cc: 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-gutmenschen-freie-5da710dfa4ab@brauner> References: <20231206-refinanzieren-werkhalle-22db5334f256@brauner> 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=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 06 Dec 2023 08:50:44 -0800 (PST) On Wed, Dec 06, 2023 at 03:38:50PM +0100, Jann Horn wrote: > On Wed, Dec 6, 2023 at 2:58 PM Christian Brauner 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? > > > > In your other mail you wrote: > > > > > I also think we have no guarantee here that the socket's ->file won't > > > go away due to a concurrent __sock_release(), which could cause us to > > > continue reading file credentials out of a file whose refcount has > > > already dropped to zero? > > > > Is this an independent worry or can the concurrent __sock_release() > > issue only happen due to a sock_orphan() having happened first? I think > > that it requires a sock_orphan() having happend, presumably because the > > socket gets marked SOCK_DEAD and can thus be released via > > __sock_release() asynchronously? > > > > If so then taking sk_callback_lock() in owner_mt() should fix this. > > (Otherwise we might need an additional get_active_file() on > > sk->sk_socker->file in owner_mt() in addition to the other fix.) > > My understanding is that it could only happen due to a sock_orphan() > having happened first, and so just sk_callback_lock() should probably > be a sufficient fix. (I'm not an expert on net subsystem locking rules > though.) Ok, so as I suspected. That's good.