Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3311923pxf; Mon, 15 Mar 2021 06:56:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwiwvu9ccwyG8IpHRH9/snGeo0/n/N1Fz6IGrteVAbEJuPsXOgZSZ0qjp72azBLOy3T3Wdn X-Received: by 2002:a17:906:5450:: with SMTP id d16mr13202088ejp.274.1615816607429; Mon, 15 Mar 2021 06:56:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615816607; cv=none; d=google.com; s=arc-20160816; b=N9igX5TOMLbyHlk4YoGKeUz1OfE6O56jRCnGWDNsW0Ez4TKzmEllSV9vJHK1z2v4oa A86tIVnxNt1K6w9sa7mQRf6ao2eTTw4/k+oO60cYUG7mB6duX09vGABoB9q6+4/gP989 mPONPoIMdZSdi7OlYfUqVMf8AKzRqFYnR53T1XzFj2Uvm91GdxGDWBbi0wfAhuO9a/2G 4gXSxy+yJTdHX8UgkVvjj2ZgrsDKxBffcFsdSrqEBY3AufFJqxTForV/70saOPYrIbUz 0sO9RDMyHDRowuQR/vE/KIQGu5D1I2km3mw5LW3egqn0+tOqaVHII7KDfJZR/h+mj3fX eOgg== 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=/K13tsj/ZvowsrdSsTlg3OXMGtflxKya7s5TtOOpDj8=; b=OT2q6TWArd3xoJ8TFRFWOOhjIfUU8Z2nyU/6xnCNSf20vK7zqIH1TgZrWFZO4+WAP1 N+rvKo03Q2ldSTJyextLUg6ERCSXh1453z/AkSsAjRYIjYHQ+R7ZULZrZUcGYxnojoAn 90GG75EquPKZvr+wqYevrpJpxwcaHhKWT4deRA7QgbiyltcvhLv8hpX4Hp87pq6vylpY a6qgyxF1+xhPfZvA+BZqCbpPJRBkvP13OGiZc7cKuVzplBVqYV5Zpl6CprjhfODjN0wj gSIiGesAYGjLSeO294D3CNL4MnTlgFzlBTixtaTeDujfsYxoZNUTNnOORkBwiJ6tSPFU vWhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="gcsXKN/L"; 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 q17si11478340eju.412.2021.03.15.06.56.24; Mon, 15 Mar 2021 06:56:47 -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="gcsXKN/L"; 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 S230123AbhCONxP (ORCPT + 99 others); Mon, 15 Mar 2021 09:53:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:55396 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229917AbhCONwt (ORCPT ); Mon, 15 Mar 2021 09:52:49 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D713C64D9E; Mon, 15 Mar 2021 13:52:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816369; bh=RtPS6I2sPdTd21blcm4L0KYzvV5RBFSeZhr/AUca1kk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gcsXKN/L3uZa0k+D188rYKrhJ7O/kKjEW+gllFQ9qUVVoWrZ5ZnD9v1V0yxCal4B3 +zQUujPSr0goLfhep00AKDNXNkv0rH4TuKT8hONWlOFAcl1mGJgqJi7ERH8eY5yI3G 2wHrPx2sFMN3CetaGbigtcMDxJc0SPWNpFqdXHi0= 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 4.4 07/75] netfilter: x_tables: gpf inside xt_find_revision() Date: Mon, 15 Mar 2021 14:51:21 +0100 Message-Id: <20210315135208.505146721@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135208.252034256@linuxfoundation.org> References: <20210315135208.252034256@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 @@ -271,6 +271,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) @@ -279,6 +280,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); @@ -291,6 +293,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) @@ -299,6 +302,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); @@ -312,12 +316,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) {