Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3321892pxf; Mon, 15 Mar 2021 07:06:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwS+AN9R7s1Npfw2aFaWWX5brzXS45UTsbpOIdU54U6M2FkRbWhXmIlcgkDuvLPyTVgNt7E X-Received: by 2002:a17:907:629e:: with SMTP id nd30mr13685595ejc.407.1615817219382; Mon, 15 Mar 2021 07:06:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615817219; cv=none; d=google.com; s=arc-20160816; b=qTAGAtWEErR/DguXSazhMsJJ8Up6LTT2fwHcUb2ISulULY9MqeLblsgbN5s6Pxbg7B 6kEUs6+1N+gpyM/zRjV8qOgIMeisr+T+3bC5sG9YtO1hUbuJfbMck9IbzS6ZkKi5cYkk phNocscLZp/mhT+0y7+H8kMMtk9GdhawkuMYjiYFgOHO7Aolz30VgmaGOMCBVwCwE6XY oXBfewlYOQbjo23cYMBDroXj4CpzyeoaTItIMs2rwZra+dwKPw3tMrHlysKSIQ3uqWqI TK6ftZ5bS7qGBwWfyICVdu8oPdkXl3xBrxMTXJ4Ekn2JuTf0h1gRJcGoTFf1hz2/OZo2 Yl5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=t/HGwlpwaD852OYy05mcfS2x0Ldvcnxk73lFWJkBmVU=; b=aZVZhp1JyFmJfyhpQtmer2fIMdLREkPjGCdEBPw3U9uQK/jdZt7ixlU9PIPlSGFoz8 4UQaPaP8wRcYHo42ffi+TMkA+ckCMaXXydB5rm6IPKY2gFS6Q6ul8cu0uTgkDE0rkuKe gUoyqJBV7n9AeFJQ+FWbiMlntgiYkWXjmkuDZsbg8yWwvY2g3Vb4Ow7UPwR/0Dr6PPk+ 5xKLBHiegbxa8mVOTvNkQVMqcupEDuyh/xEVlZ2P4fdlDC+GQYebNA9rn4kBJtdUKHBR 4htA9mm1g+h4yS4VRmDgrVJD5vZeWBItt3gJUs3EIdvHT96yEI1qJoCs5batYo95nh79 9sgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=D0EKi0Ej; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mp34si950207ejc.140.2021.03.15.07.06.36; Mon, 15 Mar 2021 07:06:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=D0EKi0Ej; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234206AbhCOODE (ORCPT + 99 others); Mon, 15 Mar 2021 10:03:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:34280 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230455AbhCON5G (ORCPT ); Mon, 15 Mar 2021 09:57:06 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2382D64F0B; Mon, 15 Mar 2021 13:57:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816622; bh=nsYvCEi2xoNHe+CzywZRoMdYn6xHq91vEpzjR/JdKQk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=D0EKi0EjF9ekbWWDuH4YUZNTK3fBvyjKw2W2XdJcXrE7jDywyFAHkI+6g7vHTxTwC 7uK/t+BNiNryKR52FS2KZFRnzqDa3ZClkvK1ZqLH/NMit8GN3NYZ0tgH6udKXQJF4P C3oqyiy/jWsFS5O980epis94wEWLow+rgBncvzeE= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Vasily Averin , Florian Westphal , Pablo Neira Ayuso Subject: [PATCH 5.11 026/306] netfilter: x_tables: gpf inside xt_find_revision() Date: Mon, 15 Mar 2021 14:51:29 +0100 Message-Id: <20210315135508.510397578@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135507.611436477@linuxfoundation.org> References: <20210315135507.611436477@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Kroah-Hartman From: Vasily Averin commit 8e24edddad152b998b37a7f583175137ed2e04a5 upstream. nested target/match_revfn() calls work with xt[NFPROTO_UNSPEC] lists without taking xt[NFPROTO_UNSPEC].mutex. This can race with module unload and cause host to crash: general protection fault: 0000 [#1] Modules linked in: ... [last unloaded: xt_cluster] CPU: 0 PID: 542455 Comm: iptables RIP: 0010:[] [] strcmp+0x18/0x40 RDX: 0000000000000003 RSI: ffff9a5a5d9abe10 RDI: dead000000000111 R13: ffff9a5a5d9abe10 R14: ffff9a5a5d9abd8c R15: dead000000000100 (VvS: %R15 -- &xt_match, %RDI -- &xt_match.name, xt_cluster unregister match in xt[NFPROTO_UNSPEC].match list) Call Trace: [] match_revfn+0x54/0xc0 [] match_revfn+0xaf/0xc0 [] xt_find_revision+0x6e/0xf0 [] do_ipt_get_ctl+0x100/0x420 [ip_tables] [] nf_getsockopt+0x4f/0x70 [] ip_getsockopt+0xde/0x100 [] raw_getsockopt+0x25/0x50 [] sock_common_getsockopt+0x1a/0x20 [] SyS_getsockopt+0x7d/0xf0 [] system_call_fastpath+0x25/0x2a Fixes: 656caff20e1 ("netfilter 04/09: x_tables: fix match/target revision lookup") Signed-off-by: Vasily Averin Reviewed-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Greg Kroah-Hartman --- net/netfilter/x_tables.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/net/netfilter/x_tables.c +++ b/net/netfilter/x_tables.c @@ -330,6 +330,7 @@ static int match_revfn(u8 af, const char const struct xt_match *m; int have_rev = 0; + mutex_lock(&xt[af].mutex); list_for_each_entry(m, &xt[af].match, list) { if (strcmp(m->name, name) == 0) { if (m->revision > *bestp) @@ -338,6 +339,7 @@ static int match_revfn(u8 af, const char have_rev = 1; } } + mutex_unlock(&xt[af].mutex); if (af != NFPROTO_UNSPEC && !have_rev) return match_revfn(NFPROTO_UNSPEC, name, revision, bestp); @@ -350,6 +352,7 @@ static int target_revfn(u8 af, const cha const struct xt_target *t; int have_rev = 0; + mutex_lock(&xt[af].mutex); list_for_each_entry(t, &xt[af].target, list) { if (strcmp(t->name, name) == 0) { if (t->revision > *bestp) @@ -358,6 +361,7 @@ static int target_revfn(u8 af, const cha have_rev = 1; } } + mutex_unlock(&xt[af].mutex); if (af != NFPROTO_UNSPEC && !have_rev) return target_revfn(NFPROTO_UNSPEC, name, revision, bestp); @@ -371,12 +375,10 @@ int xt_find_revision(u8 af, const char * { int have_rev, best = -1; - mutex_lock(&xt[af].mutex); if (target == 1) have_rev = target_revfn(af, name, revision, &best); else have_rev = match_revfn(af, name, revision, &best); - mutex_unlock(&xt[af].mutex); /* Nothing at all? Return 0 to try loading module. */ if (best == -1) {