Blame SOURCES/CVE-2023-32233-full.patch

ce310d
From 432fd316587463b7ecb99dd24807776bea6f3c22 Mon Sep 17 00:00:00 2001
ce310d
From: Joe Lawrence <joe.lawrence@redhat.com>
ce310d
Date: Thu, 10 Aug 2023 15:16:18 -0400
ce310d
Subject: [KPATCH CVE-2023-32233] kpatch fixes for CVE-2023-32233
ce310d
ce310d
Kernels:
ce310d
3.10.0-1160.83.1.el7
ce310d
3.10.0-1160.88.1.el7
ce310d
3.10.0-1160.90.1.el7
ce310d
3.10.0-1160.92.1.el7
ce310d
ce310d
ce310d
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-7/-/merge_requests/58
ce310d
Approved-by: Yannick Cote (@ycote1)
ce310d
Changes since last build:
ce310d
[x86_64]:
ce310d
cls_u32.o: changed function: u32_set_parms.isra.21
ce310d
nf_tables_api.o: changed function: nf_tables_newsetelem
ce310d
nf_tables_api.o: changed function: nf_tables_set_lookup
ce310d
nf_tables_api.o: changed function: nf_tables_set_lookup_byid
ce310d
nft_byteorder.o: changed function: nft_byteorder_eval
ce310d
nft_dynset.o: changed function: nft_dynset_init
ce310d
nft_lookup.o: changed function: nft_lookup_init
ce310d
ce310d
[ppc64le]:
ce310d
cls_u32.o: changed function: u32_set_parms.isra.21
ce310d
nf_tables_api.o: changed function: nf_tables_delset
ce310d
nf_tables_api.o: changed function: nf_tables_dump_set
ce310d
nf_tables_api.o: changed function: nf_tables_getset
ce310d
nf_tables_api.o: changed function: nf_tables_getsetelem
ce310d
nf_tables_api.o: changed function: nf_tables_newsetelem
ce310d
nf_tables_api.o: changed function: nf_tables_set_lookup
ce310d
nf_tables_api.o: changed function: nf_tables_set_lookup_byid
ce310d
nft_byteorder.o: changed function: nft_byteorder_eval
ce310d
nft_dynset.o: changed function: nft_dynset_init
ce310d
nft_lookup.o: changed function: nft_lookup_init
ce310d
ce310d
---------------------------
ce310d
ce310d
Modifications:
ce310d
- Use KLP_CVE_2023_32233 shadow variable for set->removed
ce310d
- For ppc64le, add -fno-optimize-sibling-calls attribute for
ce310d
  nf_tables_api.c :: nf_tables_getsetelem()
ce310d
ce310d
commit cde71785485c5f12520ed90f93e3e2f78270a7b7
ce310d
Author: Florian Westphal <fwestpha@redhat.com>
ce310d
Date:   Thu Jul 13 16:10:39 2023 +0200
ce310d
ce310d
    netfilter: nf_tables: do not allow SET_ID to refer to another table
ce310d
ce310d
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2196159
ce310d
    Upstream Status: commit 470ee20e069a6
ce310d
ce310d
    Conflicts:
ce310d
    net/netfilter/nf_tables_api.c
ce310d
ce310d
    We lack commit 6ab3443e9e796 ("netfilter: nf_tables: pass ctx to nf_tables_expr_destroy()"),
ce310d
    which added the "nft_table" pointer to struct nft_set.
ce310d
    We can't easily pick this one up becaue it makes the kabi checker trip over
ce310d
    the nft_set layout change, and RH_KABI_EXTEND can't be used at the structures
ce310d
    end because nft_set last member is a VLA.
ce310d
ce310d
    Fortunately we can work around it by changing
ce310d
    "set->table" to "trans->ctx.table", we only need this check in the transaction phase.
ce310d
ce310d
    commit 470ee20e069a6d05ae549f7d0ef2bdbcee6a81b2
ce310d
    Author: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
ce310d
    Date:   Tue Aug 9 14:01:46 2022 -0300
ce310d
ce310d
        netfilter: nf_tables: do not allow SET_ID to refer to another table
ce310d
ce310d
        When doing lookups for sets on the same batch by using its ID, a set from a
ce310d
        different table can be used.
ce310d
ce310d
        Then, when the table is removed, a reference to the set may be kept after
ce310d
        the set is freed, leading to a potential use-after-free.
ce310d
ce310d
        When looking for sets by ID, use the table that was used for the lookup by
ce310d
        name, and only return sets belonging to that same table.
ce310d
ce310d
        This fixes CVE-2022-2586, also reported as ZDI-CAN-17470.
ce310d
ce310d
        Reported-by: Team Orca of Sea Security (@seasecresponse)
ce310d
        Fixes: 958bee14d071 ("netfilter: nf_tables: use new transaction infrastructure to handle sets")
ce310d
        Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
ce310d
        Cc: <stable@vger.kernel.org>
ce310d
        Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
ce310d
ce310d
    Signed-off-by: Florian Westphal <fwestpha@redhat.com>
ce310d
ce310d
commit 6a38a385344448ab2f4e68e833fcf9a7e3d62128
ce310d
Author: Florian Westphal <fwestpha@redhat.com>
ce310d
Date:   Thu Jul 13 16:11:38 2023 +0200
ce310d
ce310d
    netfilter: nf_tables: skip deactivated anonymous sets during lookups
ce310d
ce310d
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2196159
ce310d
    Upstream Status: RHEL7 only
ce310d
    CVE: CVE-2023-32233
ce310d
ce310d
    The fix for the above CVE was incomplete in *RHEL7*.
ce310d
    its not enough to check if the set is scheduled for removal when
ce310d
    an element is supposed to be deleted, this check needs to be done
ce310d
    for all other element operations too, e.g. when an element is
ce310d
    supposed to be *added* to a set.
ce310d
ce310d
    Move the check to the two functions that do the set lookup.
ce310d
    sets that are scheduled for removal/pending in the transaction
ce310d
    are no longer found if they have the "removed" bit set.
ce310d
ce310d
    Fixes: ffb7eb4b21c69 ("netfilter: nf_tables: deactivate anonymous set from preparation phase")
ce310d
    Reported-by: Phil Sutter <psutter@redhat.com>
ce310d
    Signed-off-by: Florian Westphal <fwestpha@redhat.com>
ce310d
ce310d
Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
ce310d
---
ce310d
 include/net/netfilter/nf_tables.h |  1 +
ce310d
 net/netfilter/nf_tables_api.c     | 40 +++++++++++++++++++++++--------
ce310d
 net/netfilter/nft_dynset.c        |  1 +
ce310d
 net/netfilter/nft_lookup.c        |  1 +
ce310d
 4 files changed, 33 insertions(+), 10 deletions(-)
ce310d
ce310d
diff --git a/include/net/netfilter/nf_tables.h b/include/net/netfilter/nf_tables.h
ce310d
index 2ea0683b5860..3e356aba44ea 100644
ce310d
--- a/include/net/netfilter/nf_tables.h
ce310d
+++ b/include/net/netfilter/nf_tables.h
ce310d
@@ -390,6 +390,7 @@ static inline struct nft_set *nft_set_container_of(const void *priv)
ce310d
 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
ce310d
 				     const struct nlattr *nla);
ce310d
 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
ce310d
+					  const struct nft_table *table,
ce310d
 					  const struct nlattr *nla);
ce310d
 
ce310d
 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
ce310d
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
ce310d
index feff3b92a617..ed8a787ac400 100644
ce310d
--- a/net/netfilter/nf_tables_api.c
ce310d
+++ b/net/netfilter/nf_tables_api.c
ce310d
@@ -2450,22 +2450,45 @@ struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
ce310d
 		return ERR_PTR(-EINVAL);
ce310d
 
ce310d
 	list_for_each_entry(set, &table->sets, list) {
ce310d
-		if (!nla_strcmp(nla, set->name))
ce310d
-			return set;
ce310d
+
ce310d
+		u16 *klp_removed;
ce310d
+
ce310d
+		klp_removed = klp_shadow_get(set, KLP_CVE_2023_32233);
ce310d
+		if (klp_removed) {
ce310d
+			if (!nla_strcmp(nla, set->name) && !(*klp_removed))
ce310d
+				return set;
ce310d
+		} else {
ce310d
+			if (!nla_strcmp(nla, set->name))
ce310d
+				return set;
ce310d
+		}
ce310d
 	}
ce310d
 	return ERR_PTR(-ENOENT);
ce310d
 }
ce310d
 
ce310d
 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
ce310d
+					  const struct nft_table *table,
ce310d
 					  const struct nlattr *nla)
ce310d
 {
ce310d
 	struct nft_trans *trans;
ce310d
 	u32 id = ntohl(nla_get_be32(nla));
ce310d
 
ce310d
 	list_for_each_entry(trans, &net->nft.commit_list, list) {
ce310d
-		if (trans->msg_type == NFT_MSG_NEWSET &&
ce310d
-		    id == nft_trans_set_id(trans))
ce310d
-			return nft_trans_set(trans);
ce310d
+		if (trans->msg_type == NFT_MSG_NEWSET) {
ce310d
+			struct nft_set *set = nft_trans_set(trans);
ce310d
+			u16 *klp_removed;
ce310d
+			klp_removed = klp_shadow_get(set, KLP_CVE_2023_32233);
ce310d
+
ce310d
+			if (klp_removed) {
ce310d
+				if (id == nft_trans_set_id(trans) &&
ce310d
+				    trans->ctx.table == table &&
ce310d
+				    !(*klp_removed))
ce310d
+					return set;
ce310d
+			} else {
ce310d
+				if (id == nft_trans_set_id(trans) &&
ce310d
+				    trans->ctx.table == table)
ce310d
+					return set;
ce310d
+			}
ce310d
+		}
ce310d
 	}
ce310d
 	return ERR_PTR(-ENOENT);
ce310d
 }
ce310d
@@ -3335,6 +3358,7 @@ nla_put_failure:
ce310d
 	return -ENOSPC;
ce310d
 }
ce310d
 
ce310d
+__attribute__((optimize("-fno-optimize-sibling-calls")))
ce310d
 static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
ce310d
 				const struct nlmsghdr *nlh,
ce310d
 				const struct nlattr * const nla[])
ce310d
@@ -3680,6 +3704,7 @@ static int nf_tables_newsetelem(struct net *net, struct sock *nlsk,
ce310d
 	if (IS_ERR(set)) {
ce310d
 		if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
ce310d
 			set = nf_tables_set_lookup_byid(net,
ce310d
+					ctx.table,
ce310d
 					nla[NFTA_SET_ELEM_LIST_SET_ID]);
ce310d
 		}
ce310d
 		if (IS_ERR(set))
ce310d
@@ -3791,7 +3816,6 @@ static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
ce310d
 	struct nft_set *set;
ce310d
 	struct nft_ctx ctx;
ce310d
 	int rem, err = 0;
ce310d
-	u16 *klp_removed;
ce310d
 
ce310d
 	err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla);
ce310d
 	if (err < 0)
ce310d
@@ -3803,10 +3827,6 @@ static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
ce310d
 	if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
ce310d
 		return -EBUSY;
ce310d
 
ce310d
-	klp_removed = klp_shadow_get(set, KLP_CVE_2023_32233);
ce310d
-	if (klp_removed && *klp_removed)
ce310d
-		return -ENOENT;
ce310d
-
ce310d
 	if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) {
ce310d
 		struct nft_set_dump_args args = {
ce310d
 			.iter	= {
ce310d
diff --git a/net/netfilter/nft_dynset.c b/net/netfilter/nft_dynset.c
ce310d
index 4d6d3af26a5b..211e5beab83f 100644
ce310d
--- a/net/netfilter/nft_dynset.c
ce310d
+++ b/net/netfilter/nft_dynset.c
ce310d
@@ -119,6 +119,7 @@ static int nft_dynset_init(const struct nft_ctx *ctx,
ce310d
 	if (IS_ERR(set)) {
ce310d
 		if (tb[NFTA_DYNSET_SET_ID])
ce310d
 			set = nf_tables_set_lookup_byid(ctx->net,
ce310d
+							ctx->table,
ce310d
 							tb[NFTA_DYNSET_SET_ID]);
ce310d
 		if (IS_ERR(set))
ce310d
 			return PTR_ERR(set);
ce310d
diff --git a/net/netfilter/nft_lookup.c b/net/netfilter/nft_lookup.c
ce310d
index ab98a5cb7128..63eff5caae5a 100644
ce310d
--- a/net/netfilter/nft_lookup.c
ce310d
+++ b/net/netfilter/nft_lookup.c
ce310d
@@ -74,6 +74,7 @@ static int nft_lookup_init(const struct nft_ctx *ctx,
ce310d
 	if (IS_ERR(set)) {
ce310d
 		if (tb[NFTA_LOOKUP_SET_ID]) {
ce310d
 			set = nf_tables_set_lookup_byid(ctx->net,
ce310d
+							ctx->table,
ce310d
 							tb[NFTA_LOOKUP_SET_ID]);
ce310d
 		}
ce310d
 		if (IS_ERR(set))
ce310d
-- 
ce310d
2.40.1
ce310d
ce310d