Blame SOURCES/CVE-2022-1966.patch

8a5dcc
From bdeb7f1c4651240043b0b8a2a5432fc9760cfadf Mon Sep 17 00:00:00 2001
8a5dcc
From: Joe Lawrence <joe.lawrence@redhat.com>
8a5dcc
Date: Wed, 15 Jun 2022 16:10:31 -0400
8a5dcc
Subject: [KPATCH CVE-2022-1966] kpatch fixes for CVE-2022-1966
8a5dcc
8a5dcc
Kernels:
8a5dcc
3.10.0-1160.36.2.el7
8a5dcc
3.10.0-1160.41.1.el7
8a5dcc
3.10.0-1160.42.2.el7
8a5dcc
3.10.0-1160.45.1.el7
8a5dcc
3.10.0-1160.49.1.el7
8a5dcc
3.10.0-1160.53.1.el7
8a5dcc
3.10.0-1160.59.1.el7
8a5dcc
3.10.0-1160.62.1.el7
8a5dcc
3.10.0-1160.66.1.el7
8a5dcc
8a5dcc
Changes since last build:
8a5dcc
arches: x86_64 ppc64le
8a5dcc
nf_tables_api.o: changed function: nft_expr_init
8a5dcc
nft_dynset.o: changed function: nft_dynset_init
8a5dcc
---------------------------
8a5dcc
8a5dcc
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-7/-/merge_requests/42
8a5dcc
Approved-by: Yannick Cote (@ycote1)
8a5dcc
Modifications: none
8a5dcc
8a5dcc
commit c511e60bebd0546f8ec47a3c1691ab01d262b8e4
8a5dcc
Author: Phil Sutter <psutter@redhat.com>
8a5dcc
Date:   Fri Jun 3 16:54:42 2022 +0200
8a5dcc
8a5dcc
    netfilter: nf_tables: fix memory leak if expr init fails
8a5dcc
8a5dcc
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2093000
8a5dcc
    Upstream Status: commit 6cafaf4764a32
8a5dcc
8a5dcc
    commit 6cafaf4764a32597c2195aa5411b87728e1fde8a
8a5dcc
    Author: Liping Zhang <liping.zhang@spreadtrum.com>
8a5dcc
    Date:   Mon Jun 20 21:11:45 2016 +0800
8a5dcc
8a5dcc
        netfilter: nf_tables: fix memory leak if expr init fails
8a5dcc
8a5dcc
        If expr init fails then we need to free it.
8a5dcc
8a5dcc
        So when the user add a nft rule as follows:
8a5dcc
8a5dcc
          # nft add rule filter input tcp dport 22 flow table ssh \
8a5dcc
            { ip saddr limit rate 0/second }
8a5dcc
8a5dcc
        memory leak will happen.
8a5dcc
8a5dcc
        Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
8a5dcc
        Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8a5dcc
8a5dcc
    Signed-off-by: Phil Sutter <psutter@redhat.com>
8a5dcc
8a5dcc
commit 4a4cc18bcf8f43c93dbf39cb52308dfaea4ec346
8a5dcc
Author: Phil Sutter <psutter@redhat.com>
8a5dcc
Date:   Fri Jun 3 16:54:43 2022 +0200
8a5dcc
8a5dcc
    netfilter: nf_tables: disallow non-stateful expression in sets earlier
8a5dcc
8a5dcc
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2093000
8a5dcc
    Upstream Status: net.git commit 520778042ccca
8a5dcc
    CVE: CVE-2022-1966
8a5dcc
    Conflicts:
8a5dcc
     * RHEL7 does not have nft_set_elem_expr_alloc(), remove
8a5dcc
       NFT_EXPR_STATEFUL check from nft_dynset_init() instead
8a5dcc
     * Context change in nft_expr_init() as RHEL7 does not have .release_ops
8a5dcc
     * Adjusted new NFT_EXPR_STATEFUL check as upstream renamed 'info' into
8a5dcc
       'expr_info'
8a5dcc
8a5dcc
    commit 520778042ccca019f3ffa136dd0ca565c486cedd
8a5dcc
    Author: Pablo Neira Ayuso <pablo@netfilter.org>
8a5dcc
    Date:   Wed May 25 10:36:38 2022 +0200
8a5dcc
8a5dcc
        netfilter: nf_tables: disallow non-stateful expression in sets earlier
8a5dcc
8a5dcc
        Since 3e135cd499bf ("netfilter: nft_dynset: dynamic stateful expression
8a5dcc
        instantiation"), it is possible to attach stateful expressions to set
8a5dcc
        elements.
8a5dcc
8a5dcc
        cd5125d8f518 ("netfilter: nf_tables: split set destruction in deactivate
8a5dcc
        and destroy phase") introduces conditional destruction on the object to
8a5dcc
        accomodate transaction semantics.
8a5dcc
8a5dcc
        nft_expr_init() calls expr->ops->init() first, then check for
8a5dcc
        NFT_STATEFUL_EXPR, this stills allows to initialize a non-stateful
8a5dcc
        lookup expressions which points to a set, which might lead to UAF since
8a5dcc
        the set is not properly detached from the set->binding for this case.
8a5dcc
        Anyway, this combination is non-sense from nf_tables perspective.
8a5dcc
8a5dcc
        This patch fixes this problem by checking for NFT_STATEFUL_EXPR before
8a5dcc
        expr->ops->init() is called.
8a5dcc
8a5dcc
        The reporter provides a KASAN splat and a poc reproducer (similar to
8a5dcc
        those autogenerated by syzbot to report use-after-free errors). It is
8a5dcc
        unknown to me if they are using syzbot or if they use similar automated
8a5dcc
        tool to locate the bug that they are reporting.
8a5dcc
8a5dcc
        For the record, this is the KASAN splat.
8a5dcc
8a5dcc
        [   85.431824] ==================================================================
8a5dcc
        [   85.432901] BUG: KASAN: use-after-free in nf_tables_bind_set+0x81b/0xa20
8a5dcc
        [   85.433825] Write of size 8 at addr ffff8880286f0e98 by task poc/776
8a5dcc
        [   85.434756]
8a5dcc
        [   85.434999] CPU: 1 PID: 776 Comm: poc Tainted: G        W         5.18.0+ #2
8a5dcc
        [   85.436023] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014
8a5dcc
8a5dcc
        Fixes: 0b2d8a7b638b ("netfilter: nf_tables: add helper functions for expression handling")
8a5dcc
        Reported-and-tested-by: Aaron Adams <edg-e@nccgroup.com>
8a5dcc
        Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8a5dcc
8a5dcc
    Signed-off-by: Phil Sutter <psutter@redhat.com>
8a5dcc
8a5dcc
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
8a5dcc
---
8a5dcc
 net/netfilter/nf_tables_api.c | 16 +++++++++++-----
8a5dcc
 net/netfilter/nft_dynset.c    |  4 ----
8a5dcc
 2 files changed, 11 insertions(+), 9 deletions(-)
8a5dcc
8a5dcc
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
8a5dcc
index 0f46d90715a3..44738b987690 100644
8a5dcc
--- a/net/netfilter/nf_tables_api.c
8a5dcc
+++ b/net/netfilter/nf_tables_api.c
8a5dcc
@@ -1739,21 +1739,27 @@ struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
8a5dcc
 
8a5dcc
 	err = nf_tables_expr_parse(ctx, nla, &info;;
8a5dcc
 	if (err < 0)
8a5dcc
-		goto err1;
8a5dcc
+		goto err_expr_parse;
8a5dcc
+
8a5dcc
+	err = -EOPNOTSUPP;
8a5dcc
+	if (!(info.ops->type->flags & NFT_EXPR_STATEFUL))
8a5dcc
+		goto err_expr_stateful;
8a5dcc
 
8a5dcc
 	err = -ENOMEM;
8a5dcc
 	expr = kzalloc(info.ops->size, GFP_KERNEL);
8a5dcc
 	if (expr == NULL)
8a5dcc
-		goto err2;
8a5dcc
+		goto err_expr_stateful;
8a5dcc
 
8a5dcc
 	err = nf_tables_newexpr(ctx, &info, expr);
8a5dcc
 	if (err < 0)
8a5dcc
-		goto err2;
8a5dcc
+		goto err_expr_new;
8a5dcc
 
8a5dcc
 	return expr;
8a5dcc
-err2:
8a5dcc
+err_expr_new:
8a5dcc
+	kfree(expr);
8a5dcc
+err_expr_stateful:
8a5dcc
 	module_put(info.ops->type->owner);
8a5dcc
-err1:
8a5dcc
+err_expr_parse:
8a5dcc
 	return ERR_PTR(err);
8a5dcc
 }
8a5dcc
 
8a5dcc
diff --git a/net/netfilter/nft_dynset.c b/net/netfilter/nft_dynset.c
8a5dcc
index 3f9f8e82716e..0cf187230050 100644
8a5dcc
--- a/net/netfilter/nft_dynset.c
8a5dcc
+++ b/net/netfilter/nft_dynset.c
8a5dcc
@@ -174,10 +174,6 @@ static int nft_dynset_init(const struct nft_ctx *ctx,
8a5dcc
 		priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
8a5dcc
 		if (IS_ERR(priv->expr))
8a5dcc
 			return PTR_ERR(priv->expr);
8a5dcc
-
8a5dcc
-		err = -EOPNOTSUPP;
8a5dcc
-		if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL))
8a5dcc
-			goto err1;
8a5dcc
 	} else if (set->flags & NFT_SET_EVAL)
8a5dcc
 		return -EINVAL;
8a5dcc
 
8a5dcc
-- 
8a5dcc
2.34.3
8a5dcc
8a5dcc