Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1108869rdb; Wed, 6 Dec 2023 08:48:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFoRX8ZWXDrY6cyXYwJ+vNNuOIFWVxSmM3xTyYg2WYk42+zAhi3RMpFy8r25HM80TIZE9Bc X-Received: by 2002:a05:6a20:2444:b0:18f:97c:b9ed with SMTP id t4-20020a056a20244400b0018f097cb9edmr958411pzc.71.1701881329086; Wed, 06 Dec 2023 08:48:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701881329; cv=none; d=google.com; s=arc-20160816; b=J/PQPYjlpUhvdZUk1y2PJOZ98+tCmIl9rI/gdFW569dvpH7+8G1FhOKO9D2g0tvOfC 8QyY/EjXDAzNB9J74e7XkXGkAp0Bwp+TOo8PwLDByVYuFByXBphxy7A06sffrbuEmIs4 FEj2oaqwv0BloZJHxGWr1gLh2vxm5Ks3NuuJH+58fdCrZ2o9i+bJr9kfv54Ax79JRiAM z7jtwKWnuEHzCSAxUFUYUXKog7M2f2tYpZMXeBTSf1v91tOsmeIVTFg7K/yv4xl+X4zM MJ3LUsUkytip0r/wvCEcLEHlBEXHQ0UpjXwBvuW+xP/m8+nVc4MiS5L9DwDUeCSEVm+/ or/g== 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; bh=K6b1+k1zzatQApCQkbTRn770yAyEdGxwkESjvCBnUJI=; fh=Dmmo6+kaLGx1x21nkrGhEg7sXjr1XCH6eyCTpELDpM8=; b=FLqoQp7hO01c+co9Nf7L7Qh64/URvDYUtCaBNjcQu+WZtLk/UVxml1LgzizEZDJYV+ NmGPWehmgmTZuK17+9iKSByqumLVu3UTxBbupES8c7yeaXFsaDdOtSds8VGhcG204avu ecAmyEPNrts8V7f3YNzxW3a3qz9t2SOPdvwArusOM7JPw1a49sVypuZsmEhozs8AXaJg NbB02AiP/FuH9k/r/JBW4avKFjaxUTyVQ5LlIo+TjzsFj0v9MKYdPUbPima0wTx6LNP+ Je/kfaGTiLzq4blmCBxg+cnn4ng0P6m/WyVRb1pAi68Vn1IZGCeA4ejodgcAm5go6LKy KhUQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id be15-20020a056a001f0f00b006cbd3f3a2fcsi213945pfb.77.2023.12.06.08.48.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 08:48:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id 6362D8028A7F; Wed, 6 Dec 2023 08:48:46 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379377AbjLFQsc (ORCPT + 99 others); Wed, 6 Dec 2023 11:48:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378443AbjLFQsb (ORCPT ); Wed, 6 Dec 2023 11:48:31 -0500 Received: from ganesha.gnumonks.org (ganesha.gnumonks.org [IPv6:2001:780:45:1d:225:90ff:fe52:c662]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0E7ED4B; Wed, 6 Dec 2023 08:48:37 -0800 (PST) Received: from [78.30.43.141] (port=41630 helo=gnumonks.org) by ganesha.gnumonks.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rAv4b-003yIw-SR; Wed, 06 Dec 2023 17:48:35 +0100 Date: Wed, 6 Dec 2023 17:48:32 +0100 From: Pablo Neira Ayuso To: Jann Horn Cc: Phil Sutter , 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: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Score: -1.9 (-) X-Spam-Status: No, score=-0.8 required=5.0 tests=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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Wed, 06 Dec 2023 08:48:46 -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. The lack of READ_ONCE() on sk->sk_socket also got me thinking here, given this sk_set_socket(sk, NULL) in sock_orphan is done under the lock. I am taking Phil's patch, I think it is leaving things in better shape.