Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp582216rdb; Tue, 5 Dec 2023 13:40:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IFKQlXl0YQXTVr7pvoIIuOc2zzPlk0z+6aZkUsk49X3vuPquFW48aBLlSyMQTnoYsZ8BgUy X-Received: by 2002:a05:6a20:1611:b0:181:bc16:48dd with SMTP id l17-20020a056a20161100b00181bc1648ddmr3683454pzj.47.1701812434363; Tue, 05 Dec 2023 13:40:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701812434; cv=none; d=google.com; s=arc-20160816; b=Uef30v/aXxlwnXaJg/H8jlPA24P/UT2XFxEJ0MZrO0+NbqPPzCu+0iF187WNYAtfoz HpNuyFgoiFa8YbLCetPDyKzSbcu+5sdIBqA4gRqX9u4NjJdIwO3VHpcxruOdhcQfQkZC QG49tzYVsOjk13xR4q+mM+kuVF0YY2js6LoHD7/dVBFnyEtwuPYR3iTaDDPxeOP+xV9K DSOsLJ5htuxtXbFH+o2fk1w9DGvyAPdmzXEZO3jVmLZVjkGtRDQZTmr6HiMRMcQHUn6O ybTXpZBCRiQy7bnE88HLlGvaQ58Kp1ntYXPRatjQTV8MKNTSoMEIcgC6DRtFhbxxjnoO j+nA== 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=HwfadbfArAOrbi6WFDIy6INcelr36TFFIrCY2kyM2c4=; fh=cI+9URTgFpley0bzfT0QiKmI9whewfFNGUJ+V1kiTqI=; b=dyd3wKmYZwa6aPTFrJg8IGRwzcdQYXSilI/zA+qaziW1Y8USozXrjpu6ltlJCMObCa a7a9Mkksa2CnJuqArp7OcEnrcT7r0sdykjCPkJjlW9bGMj+RZiSfHkJfNyYB6vsbY0qv Fh2yqmyeCrA2L+9Qs+ASkSyryfPIAs3kKp7mwcBTqW0MZ+KyJV8yBz1vkRCL3xM7h7se UPtr0fcqeY1NCulP0vGQYHso/0I/2BOQDx2fum/spHUpQsWXASIdCEhy2YWA0p8JGhm2 EAa9ABBX8M036ANo044B/+VYfaKASKuHrcN8T9vHSd2lNxOs+9y0a9a4pXjzAex5+N97 MQfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nwl.cc header.s=mail2022 header.b=KsNMkEFo; 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 1-20020a631041000000b005bdbcdc9e3dsi10350752pgq.142.2023.12.05.13.40.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 13:40:34 -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; dkim=fail header.i=@nwl.cc header.s=mail2022 header.b=KsNMkEFo; 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 83CA78032A2E; Tue, 5 Dec 2023 13:40:31 -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 S1346025AbjLEVkQ (ORCPT + 99 others); Tue, 5 Dec 2023 16:40:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229591AbjLEVkP (ORCPT ); Tue, 5 Dec 2023 16:40:15 -0500 Received: from orbyte.nwl.cc (orbyte.nwl.cc [IPv6:2001:41d0:e:133a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F17761A5; Tue, 5 Dec 2023 13:40:19 -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=HwfadbfArAOrbi6WFDIy6INcelr36TFFIrCY2kyM2c4=; b=KsNMkEFoe4cCpPVKu9fmLkWvbx BQu1ttDZYNRVoM+Kt4dTeJ2Q2VOTg/u9VMWS/moeP4V481ksJ4PaN8j7d4DTqPBwoMPqhlcV/V/S5 rvxXh03r3El+HsPg2iPM2pMrdSdo86X4XeXH7Zq10ob83U8NF1qiI78+AG312kAr/4+Jw22BzXYBT QdgDfmK3BNQOLMJzSdlleXe1Vrd4JfkLOGurOkjzLKYhniDdC4AD2MPsODbPD2AQXJ191TY9yfhfo 0pHZ/bwR70EdGU6kwjOsJSNCOlF2MOZhWqz13R87mDFTMA/9MRPW+dQusySuXzHKIMtP6coOeXTX+ AI5lJuwA==; Received: from n0-1 by orbyte.nwl.cc with local (Exim 4.94.2) (envelope-from ) id 1rAd9L-0006hb-2f; Tue, 05 Dec 2023 22:40:15 +0100 Date: Tue, 5 Dec 2023 22:40:15 +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: multipart/mixed; boundary="AjyCI6p3WJlNZLnJ" 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 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]); Tue, 05 Dec 2023 13:40:31 -0800 (PST) --AjyCI6p3WJlNZLnJ Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit Hi, 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. Thanks, Phil --AjyCI6p3WJlNZLnJ Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-netfilter-xt_owner-Fix-for-unsafe-access-of-sk-sk_so.patch" From 3e28490e43b04d49e6e7f145a70fff7dd42c8cc5 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Tue, 5 Dec 2023 21:58:12 +0100 Subject: [nf PATCH] netfilter: xt_owner: Fix for unsafe access of sk->sk_socket A concurrently running sock_orphan() may NULL the sk_socket pointer in between check and deref. Follow other users (like nft_meta.c for instance) and acquire sk_callback_lock before dereferencing sk_socket. Reported-by: Jann Horn Signed-off-by: Phil Sutter --- net/netfilter/xt_owner.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/net/netfilter/xt_owner.c b/net/netfilter/xt_owner.c index e85ce69924ae..50332888c8d2 100644 --- a/net/netfilter/xt_owner.c +++ b/net/netfilter/xt_owner.c @@ -76,18 +76,23 @@ owner_mt(const struct sk_buff *skb, struct xt_action_param *par) */ return false; - filp = sk->sk_socket->file; - if (filp == NULL) + read_lock_bh(&sk->sk_callback_lock); + filp = sk->sk_socket ? sk->sk_socket->file : NULL; + if (filp == NULL) { + read_unlock_bh(&sk->sk_callback_lock); return ((info->match ^ info->invert) & (XT_OWNER_UID | XT_OWNER_GID)) == 0; + } if (info->match & XT_OWNER_UID) { kuid_t uid_min = make_kuid(net->user_ns, info->uid_min); kuid_t uid_max = make_kuid(net->user_ns, info->uid_max); if ((uid_gte(filp->f_cred->fsuid, uid_min) && uid_lte(filp->f_cred->fsuid, uid_max)) ^ - !(info->invert & XT_OWNER_UID)) + !(info->invert & XT_OWNER_UID)) { + read_unlock_bh(&sk->sk_callback_lock); return false; + } } if (info->match & XT_OWNER_GID) { @@ -112,10 +117,13 @@ owner_mt(const struct sk_buff *skb, struct xt_action_param *par) } } - if (match ^ !(info->invert & XT_OWNER_GID)) + if (match ^ !(info->invert & XT_OWNER_GID)) { + read_unlock_bh(&sk->sk_callback_lock); return false; + } } + read_unlock_bh(&sk->sk_callback_lock); return true; } -- 2.41.0 --AjyCI6p3WJlNZLnJ--