Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3321936pxf; Mon, 15 Mar 2021 07:07:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzVorMzb3RbM27pEB/onE6vYQWSA7HEE/mmI8wrja8fKrWsgGPt9FKMeIiAB4wcX77GnzCd X-Received: by 2002:a17:906:4801:: with SMTP id w1mr2151127ejq.475.1615817222117; Mon, 15 Mar 2021 07:07:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615817222; cv=none; d=google.com; s=arc-20160816; b=QJt4167kN9y17FwFGfnIqwS8eGy+J5LawnrRzzjRn2Liqyp++xpvTKmu/hSAUButtp eA5aXcyOY1FAnthyzkEPkR0BVyLdBb9oOAnz8kBBmlqPcNrYKCpWuONTz6zUY5sXFgn4 6Bhqbqdgf1lEUIcltUSK734i93+RE0LQ81EzQhLg7g3xBGdrmvbMntcPf2q5aTHm3FFC LMrpMvlItYhy/fvHx3BWbo0mMHjJVtacoMOTXpCoBYkSxbgnry0hElL5rnk+HtM1pbUO Di03lF0IszkITUGv/FceP0qtpKx08BzRbbA+BwgXR0Abg9THfYRgHWbzRE7V+b7DXiCL CLDQ== 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=w1zUuVb+BRhEDGYTCJEL2ogYrmjJp4k1lzuoPK3bT22KyM4H41QRElwGYjcrpBrrBH Ef9oXYCRItQ4hOanZ7q8ooI2ZdcTCdxpDSyc3PNIidpQhSM17ZugOyAPDovNlUFu9E99 3NrXf2F5U8tp5c+1kbd43XymiqaBPO+M2ejWhIq4k7DGEA+Zec47cx+Kry4Ojxx/0LGM 0JabI71lulAMcOVbo8q7Jtbc/tP59jp9uoz/ZmwnFvwkX1e4rREHxLz2HJN+wdb+aF1G vhGzgdg2sbyTFLLzuD0qxuw/HR1tDPdA1keUqvvFnzKJ+DAKkxhQ55CIb9GHf8kNbMa0 PXdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=v93QLcVb; 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 ce13si11137495ejc.750.2021.03.15.07.06.38; Mon, 15 Mar 2021 07:07:02 -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=v93QLcVb; 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 S234245AbhCOODG (ORCPT + 99 others); Mon, 15 Mar 2021 10:03:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:34198 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231139AbhCON5G (ORCPT ); Mon, 15 Mar 2021 09:57:06 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8989D64EF0; Mon, 15 Mar 2021 13:57:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816623; bh=nsYvCEi2xoNHe+CzywZRoMdYn6xHq91vEpzjR/JdKQk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=v93QLcVbXGkn4O2UFj/up4WcxfdnKnvS/O4dh+2g3PCAgCdRAvkaVX7khgFSDDbCV qLCG2A8wVjTgtGZJIev6kx9ieHRlqb/K9SguUcU1rgoy7WEsW+2pErVO2i04Z76cuZ OUHrFNJQULehgjKhmiEuWVab2dYIW1nKcQXlI6JE= 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.10 019/290] netfilter: x_tables: gpf inside xt_find_revision() Date: Mon, 15 Mar 2021 14:51:52 +0100 Message-Id: <20210315135542.583007327@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135541.921894249@linuxfoundation.org> References: <20210315135541.921894249@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) {