Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp989479rdb; Wed, 6 Dec 2023 05:58:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IGIOJHXsavdqJRF4Fr00NI2JylbLbzal4EnQ2HNyJg5aqk2ssGuswjRvdZB9N9+QHGrZjEO X-Received: by 2002:a17:902:e548:b0:1d0:ba36:eea3 with SMTP id n8-20020a170902e54800b001d0ba36eea3mr659607plf.7.1701871129195; Wed, 06 Dec 2023 05:58:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701871129; cv=none; d=google.com; s=arc-20160816; b=BxYIcMbbA6qpTBStF2rpen2MaRg2jmvnKBiUwCKJLDydMxj2RDItJY6rTxCaIJqJ2+ set4SlsQF582WcEwHxmkuMNaGQKBvitAluba/14tPyW3RSnVlfXAb0vSIsGRkOWz5jC9 a0BBNimK6pWLn21/E/02PMnPq1mRTHBP6Jzu6UtXTYr5QUaxxV3hJx4XeKsjATajSDes Vt7rEpJi+NqI+El9g7WB58+xJOxxzjN3uxAwnag5D4545WxhLf0fqYpB3miDer8IKzYb EyClZv1Yiq5TP58rSOKbCZ3RDnd3KGworkJt+AoOJthkyaxBd8hYLb4Ird6sJPntomzm xggg== 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=yXlmXSJDMPXdd7nUh5N43yox/TZm8Zu68+oFptDOGw8=; fh=G6Sp6BgzL8tAftW0vdwNjzvw77GaLnYGmkm8Geqg+3g=; b=E8S9tSYfTNZvGepZoGMUFLboNvueu0gJpcidxLgARb6U5SkiGc/Jynp/klhapcQfmS tk56TcB0CKRsWx9U6RQQaUDnhyebSTxwNiDaUexJYLuRdyRSdkIgIWV4/EVpjUsTTOma G/BdMGM/kjxbp2o96GV23eqCBkDsTAO0q02F/XlmaWBO9gQb8K6LXdASz+caa8E5WWjo HnMqmtY8LcttDd7X1gZ21edjRrdeoIN7kFiUl9+EmPW4+azuEl1yLjfEatxlgw0+BejB YBOcUe5uFHjkvb9s4B8NorBLsZ3leWF9/Fe3jMthzz5aICD/znXJYUCr88Ym0FT+oe7a UKIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NuZBxUDR; 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 q18-20020a17090311d200b001cfabbeeda5si11822248plh.291.2023.12.06.05.58.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 05:58:49 -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=NuZBxUDR; 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 D7DC18029C48; Wed, 6 Dec 2023 05:58:47 -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 S1378633AbjLFN6i (ORCPT + 99 others); Wed, 6 Dec 2023 08:58:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378713AbjLFN6g (ORCPT ); Wed, 6 Dec 2023 08:58:36 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 634E9A4 for ; Wed, 6 Dec 2023 05:58:42 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B72FC433C7; Wed, 6 Dec 2023 13:58:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701871122; bh=igbPsqBqoC6Yx2+ikp9QT1gV4CGkTMXJP7iKBjWdZ3o=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=NuZBxUDRhHT/RUByFl8OswuDT+o0oA7/OX5J4+UiSHxe2O6k9gLNxFAZtfXxO0uiq R71x2H6o0RrTHC0eVqeJNL+K3VlpWwLf+zMNsf7i/JmNRHQNoFX9id6su73i+rqncg dOrrWdyjbFbeY2LB+cHc0SE6yWdnoE+X80I68lpt+R3HI7km/6fSMLn1B5Lk6legQa KVqqlpEk3X/wEyIG7ytGOPaaga7jCka2AWPIi3Ato9IfoM5t9OTFnrxbhg/x2xx/yc nYdgYtarnKjk4VxveyJNg5pFi7OgQXomKaf5r/dUe3R1i0KcQCpWYe8NPGLK4e//bU EA6jxYRGTg4+Q== Date: Wed, 6 Dec 2023 14:58:36 +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-refinanzieren-werkhalle-22db5334f256@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 05:58:48 -0800 (PST) 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.)