Blame SOURCES/CVE-2023-32233.patch

81743a
From 7972a497548bcdfc7f3b6f24a1fd5e4b0e008fff Mon Sep 17 00:00:00 2001
81743a
From: Ryan Sullivan <rysulliv@redhat.com>
81743a
Date: Thu, 15 Jun 2023 10:43:28 -0400
81743a
Subject: [KPATCH CVE-2023-32233] kpatch fixes for CVE-2023-32233
81743a
81743a
Kernels:
81743a
3.10.0-1160.81.1.el7
81743a
3.10.0-1160.83.1.el7
81743a
3.10.0-1160.88.1.el7
81743a
3.10.0-1160.90.1.el7
81743a
3.10.0-1160.92.1.el7
81743a
81743a
81743a
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-7/-/merge_requests/54
81743a
Approved-by: Yannick Cote (@ycote1)
81743a
Approved-by: Joe Lawrence (@joe.lawrence)
81743a
Changes since last build:
81743a
[x86_64]:
81743a
l2cap_core.o: changed function: l2cap_data_channel
81743a
l2cap_core.o: changed function: l2cap_rx_state_recv
81743a
nf_tables_api.o: changed function: __nf_tables_abort
81743a
nf_tables_api.o: changed function: nf_tables_delsetelem
81743a
nf_tables_api.o: changed function: nf_tables_newset
81743a
nf_tables_api.o: changed function: nft_delrule
81743a
nf_tables_api.o: changed function: nft_set_destroy
81743a
nf_tables_api.o: changed function: nft_validate_register_store
81743a
nf_tables_api.o: new function: nf_tables_activate_set
81743a
nf_tables_api.o: new function: nf_tables_deactivate_set
81743a
nf_tables_api.o: new function: nft_rule_expr_deactivate
81743a
nft_dynset.o: new function: klp_is_nft_dynset
81743a
nft_dynset.o: new function: nft_dynset_activate
81743a
nft_dynset.o: new function: nft_dynset_deactivate
81743a
nft_lookup.o: new function: klp_is_nft_lookup
81743a
nft_lookup.o: new function: nft_lookup_activate
81743a
nft_lookup.o: new function: nft_lookup_deactivate
81743a
81743a
[ppc64le]:
81743a
l2cap_core.o: changed function: l2cap_data_channel
81743a
l2cap_core.o: changed function: l2cap_rx_state_recv
81743a
nf_tables_api.o: changed function: __nf_tables_abort
81743a
nf_tables_api.o: changed function: nf_tables_bind_check_setelem
81743a
nf_tables_api.o: changed function: nf_tables_bind_set
81743a
nf_tables_api.o: changed function: nf_tables_commit
81743a
nf_tables_api.o: changed function: nf_tables_delrule
81743a
nf_tables_api.o: changed function: nf_tables_delsetelem
81743a
nf_tables_api.o: changed function: nf_tables_newset
81743a
nf_tables_api.o: changed function: nf_tables_unbind_set
81743a
nf_tables_api.o: changed function: nft_add_set_elem
81743a
nf_tables_api.o: changed function: nft_delrule_by_chain
81743a
nf_tables_api.o: changed function: nft_unregister_afinfo
81743a
nf_tables_api.o: changed function: nft_validate_register_store
81743a
nf_tables_api.o: new function: nf_tables_activate_set
81743a
nf_tables_api.o: new function: nf_tables_deactivate_set
81743a
nft_dynset.o: new function: klp_is_nft_dynset
81743a
nft_dynset.o: new function: nft_dynset_activate
81743a
nft_dynset.o: new function: nft_dynset_deactivate
81743a
nft_lookup.o: new function: klp_is_nft_lookup
81743a
nft_lookup.o: new function: nft_lookup_activate
81743a
nft_lookup.o: new function: nft_lookup_deactivate
81743a
81743a
---------------------------
81743a
81743a
Modifications:
81743a
- Removes prototype definitions of nf_tables_activate_set() and
81743a
nf_tables_deactivate_set() from nf_tables.h and moves them into the
81743a
affected files above when they are called
81743a
81743a
- Adds optimization attribute "-fno-optimize-sibling-calls" to the
81743a
nf_tables_deactivate_set()
81743a
81743a
- Removes definitions/edits of the policy and removed fields from the
81743a
nft_set struct instead using a shadow variable
81743a
(ID = KLP_CVE_2023_32233), created in nf_tables_newset() and
81743a
destroyed in nft_set_destroy()
81743a
81743a
- Removes .activate and .deactivate from nft_(dynset/lookup)_ops and
81743a
instead changes nft_rule_expr_(activate/deactivate) to directly call
81743a
activate and deactivate functions from within them
81743a
81743a
commit ffb7eb4b21c69f2b5e084c85b37eb033544e3fc9
81743a
Author: Florian Westphal <fwestpha@redhat.com>
81743a
Date:   Tue May 16 13:34:35 2023 +0200
81743a
81743a
    netfilter: nf_tables: deactivate anonymous set from preparation phase
81743a
81743a
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2196159
81743a
    Upstream Status: commit c1592a89942e9
81743a
    CVE: CVE-2023-32233
81743a
    Conflicts: everywhere
81743a
    Tested: nftables v0.8 shell/py tests
81743a
81743a
    A rule like
81743a
81743a
    ip saddr { 1.2.3.4, 5.6.7.8 } accept
81743a
81743a
    consists of multiple expressions and a set (the part in { }).
81743a
81743a
    This set only has a auto-assigned name "__set%d" hidden from
81743a
    userspace view, but its still internally referenceable this way.
81743a
81743a
    Such "anonymous" sets are owned by the rule that use it.
81743a
    Rule deletion auto-removes the set as well.
81743a
81743a
    Unfortunately userspace can craft a transaction that first
81743a
    deletes the rule and then attempts an operation on the anon
81743a
    set, such as deleting it or deleting an element from the set.
81743a
81743a
    Upstream patch uses existing delete/activate callbacks to disable
81743a
    the set in the new generation (the "preview" of the future ruleset).
81743a
    This makes such attempt at (re)using the set fail because the set
81743a
    won't be visible anymore.
81743a
81743a
    In RHEL7 we cannot mark the set as inactive in next generation
81743a
    because neither sets nor set elements have such a generation bit mask.
81743a
81743a
    This backport adds minimal bits from
81743a
    408070d6ee3490 "netfilter: nf_tables: add nft_set_is_anonymous() helper"
81743a
    bb7b40aecbf778 "netfilter: nf_tables: bogus EBUSY in chain deletions"
81743a
    cd5125d8f51882 "netfilter: nf_tables: split set destruction in deactivate and destroy phase"
81743a
81743a
    for the necessary infrastructure to mark anon sets as
81743a
    "to be removed" from transaction preparation phase.
81743a
81743a
    Introduce an explicit "removed" bit flag that is set once the nft_lookup
81743a
    or dynset expression referencing an anonymous set gets scheduled for
81743a
    removal in the transaction phase.
81743a
81743a
    This can then be detected in a subsequent DELSETEM attempt and
81743a
    an error can be returned.
81743a
81743a
    commit c1592a89942e9678f7d9c8030efa777c0d57edab
81743a
    Author: Pablo Neira Ayuso <pablo@netfilter.org>
81743a
    Date:   Tue May 2 10:25:24 2023 +0200
81743a
81743a
        netfilter: nf_tables: deactivate anonymous set from preparation phase
81743a
81743a
        Toggle deleted anonymous sets as inactive in the next generation, so
81743a
        users cannot perform any update on it. Clear the generation bitmask
81743a
        in case the transaction is aborted.
81743a
81743a
        The following KASAN splat shows a set element deletion for a bound
81743a
        anonymous set that has been already removed in the same transaction.
81743a
81743a
        [   64.921510] ==================================================================
81743a
        [   64.923123] BUG: KASAN: wild-memory-access in nf_tables_commit+0xa24/0x1490 [nf_tables]
81743a
        [   64.924745] Write of size 8 at addr dead000000000122 by task test/890
81743a
        [   64.927903] CPU: 3 PID: 890 Comm: test Not tainted 6.3.0+ #253
81743a
        [   64.931120] Call Trace:
81743a
        [   64.932699]  <TASK>
81743a
        [   64.934292]  dump_stack_lvl+0x33/0x50
81743a
        [   64.935908]  ? nf_tables_commit+0xa24/0x1490 [nf_tables]
81743a
        [   64.937551]  kasan_report+0xda/0x120
81743a
        [   64.939186]  ? nf_tables_commit+0xa24/0x1490 [nf_tables]
81743a
        [   64.940814]  nf_tables_commit+0xa24/0x1490 [nf_tables]
81743a
        [   64.942452]  ? __kasan_slab_alloc+0x2d/0x60
81743a
        [   64.944070]  ? nf_tables_setelem_notify+0x190/0x190 [nf_tables]
81743a
        [   64.945710]  ? kasan_set_track+0x21/0x30
81743a
        [   64.947323]  nfnetlink_rcv_batch+0x709/0xd90 [nfnetlink]
81743a
        [   64.948898]  ? nfnetlink_rcv_msg+0x480/0x480 [nfnetlink]
81743a
81743a
        Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
81743a
81743a
    Signed-off-by: Florian Westphal <fwestpha@redhat.com>
81743a
81743a
Signed-off-by: Ryan Sullivan <rysulliv@redhat.com>
81743a
---
81743a
 net/netfilter/nf_tables_api.c | 97 ++++++++++++++++++++++++++++++++++-
81743a
 net/netfilter/nft_dynset.c    | 26 ++++++++++
81743a
 net/netfilter/nft_lookup.c    | 26 ++++++++++
81743a
 3 files changed, 147 insertions(+), 2 deletions(-)
81743a
81743a
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
81743a
index 44738b987690..feff3b92a617 100644
81743a
--- a/net/netfilter/nf_tables_api.c
81743a
+++ b/net/netfilter/nf_tables_api.c
81743a
@@ -20,6 +20,9 @@
81743a
 #include <net/netfilter/nf_tables.h>
81743a
 #include <net/net_namespace.h>
81743a
 #include <net/sock.h>
81743a
+#include <linux/livepatch.h>
81743a
+
81743a
+#define KLP_CVE_2023_32233 	0x2022101200000111
81743a
 
81743a
 static LIST_HEAD(nf_tables_expressions);
81743a
 
81743a
@@ -260,6 +263,50 @@ static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
81743a
 	rule->genmask &= ~nft_genmask_next(net);
81743a
 }
81743a
 
81743a
+extern void nft_dynset_deactivate(const struct nft_ctx *ctx,
81743a
+				  const struct nft_expr *expr);
81743a
+extern void nft_dynset_activate(const struct nft_ctx *ctx,
81743a
+				const struct nft_expr *expr);
81743a
+extern int klp_is_nft_dynset(const struct nft_expr *expr);
81743a
+
81743a
+extern void nft_lookup_deactivate(const struct nft_ctx *ctx,
81743a
+				  const struct nft_expr *expr);
81743a
+extern void nft_lookup_activate(const struct nft_ctx *ctx,
81743a
+				const struct nft_expr *expr);
81743a
+extern int klp_is_nft_lookup(const struct nft_expr *expr);
81743a
+
81743a
+static void nft_rule_expr_activate(const struct nft_ctx *ctx,
81743a
+				   struct nft_rule *rule)
81743a
+{
81743a
+	struct nft_expr *expr;
81743a
+
81743a
+	expr = nft_expr_first(rule);
81743a
+	while (expr != nft_expr_last(rule) && expr->ops) {
81743a
+		if (klp_is_nft_dynset(expr))
81743a
+			nft_dynset_activate(ctx, expr);
81743a
+		else if (klp_is_nft_lookup(expr))
81743a
+			nft_lookup_activate(ctx, expr);
81743a
+
81743a
+		expr = nft_expr_next(expr);
81743a
+	}
81743a
+}
81743a
+
81743a
+static void nft_rule_expr_deactivate(const struct nft_ctx *ctx,
81743a
+				     struct nft_rule *rule)
81743a
+{
81743a
+	struct nft_expr *expr;
81743a
+
81743a
+	expr = nft_expr_first(rule);
81743a
+	while (expr != nft_expr_last(rule) && expr->ops) {
81743a
+		if (klp_is_nft_dynset(expr))
81743a
+			nft_dynset_deactivate(ctx, expr);
81743a
+		else if (klp_is_nft_lookup(expr))
81743a
+			nft_lookup_deactivate(ctx, expr);
81743a
+
81743a
+		expr = nft_expr_next(expr);
81743a
+	}
81743a
+}
81743a
+
81743a
 static int
81743a
 nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
81743a
 {
81743a
@@ -301,6 +348,7 @@ static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
81743a
 		nft_trans_destroy(trans);
81743a
 		return err;
81743a
 	}
81743a
+	nft_rule_expr_deactivate(ctx, rule);
81743a
 
81743a
 	return 0;
81743a
 }
81743a
@@ -2736,6 +2784,7 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk,
81743a
 	unsigned char *udata;
81743a
 	u16 udlen;
81743a
 	int err;
81743a
+	u16 *klp_removed;
81743a
 
81743a
 	if (nla[NFTA_SET_TABLE] == NULL ||
81743a
 	    nla[NFTA_SET_NAME] == NULL ||
81743a
@@ -2861,10 +2910,16 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk,
81743a
 	if (set == NULL)
81743a
 		goto err1;
81743a
 
81743a
+	klp_removed = klp_shadow_alloc(set, KLP_CVE_2023_32233,
81743a
+				      sizeof(*klp_removed), GFP_KERNEL,
81743a
+				      NULL, NULL);
81743a
+	if(!klp_removed)
81743a
+		goto err2;
81743a
+
81743a
 	nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
81743a
 	err = nf_tables_set_alloc_name(&ctx, set, name);
81743a
 	if (err < 0)
81743a
-		goto err2;
81743a
+		goto klp_err;
81743a
 
81743a
 	udata = NULL;
81743a
 	if (udlen) {
81743a
@@ -2889,7 +2944,7 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk,
81743a
 
81743a
 	err = ops->init(set, &desc, nla);
81743a
 	if (err < 0)
81743a
-		goto err2;
81743a
+		goto klp_err;
81743a
 
81743a
 	err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
81743a
 	if (err < 0)
81743a
@@ -2901,6 +2956,8 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk,
81743a
 
81743a
 err3:
81743a
 	ops->destroy(set);
81743a
+klp_err:
81743a
+	klp_shadow_free(set, KLP_CVE_2023_32233, NULL);
81743a
 err2:
81743a
 	kfree(set);
81743a
 err1:
81743a
@@ -2910,6 +2967,7 @@ err1:
81743a
 
81743a
 static void nft_set_destroy(struct nft_set *set)
81743a
 {
81743a
+	klp_shadow_free(set, KLP_CVE_2023_32233, NULL);
81743a
 	set->ops->destroy(set);
81743a
 	module_put(set->ops->owner);
81743a
 	kfree(set);
81743a
@@ -3013,6 +3071,34 @@ void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
81743a
 		nf_tables_set_destroy(ctx, set);
81743a
 }
81743a
 
81743a
+static inline bool nft_set_is_anonymous(const struct nft_set *set)
81743a
+{
81743a
+	return set->flags & NFT_SET_ANONYMOUS;
81743a
+}
81743a
+
81743a
+void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set)
81743a
+{
81743a
+	u16 *klp_removed;
81743a
+
81743a
+	if (nft_set_is_anonymous(set)) {
81743a
+		klp_removed = klp_shadow_get(set, KLP_CVE_2023_32233);
81743a
+		if(klp_removed)
81743a
+			*klp_removed = 0;
81743a
+	}
81743a
+}
81743a
+
81743a
+__attribute__((optimize("-fno-optimize-sibling-calls")))
81743a
+void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
81743a
+			      struct nft_set_binding *binding)
81743a
+{
81743a
+	u16 *klp_removed;
81743a
+	klp_removed = klp_shadow_get(set, KLP_CVE_2023_32233);
81743a
+	if (nft_set_is_anonymous(set)) {
81743a
+		if(klp_removed)
81743a
+			*klp_removed = 1;
81743a
+	}
81743a
+}
81743a
+
81743a
 const struct nft_set_ext_type nft_set_ext_types[] = {
81743a
 	[NFT_SET_EXT_KEY]		= {
81743a
 		.align	= __alignof__(u32),
81743a
@@ -3705,6 +3791,7 @@ static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
81743a
 	struct nft_set *set;
81743a
 	struct nft_ctx ctx;
81743a
 	int rem, err = 0;
81743a
+	u16 *klp_removed;
81743a
 
81743a
 	err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla);
81743a
 	if (err < 0)
81743a
@@ -3716,6 +3803,10 @@ static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
81743a
 	if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
81743a
 		return -EBUSY;
81743a
 
81743a
+	klp_removed = klp_shadow_get(set, KLP_CVE_2023_32233);
81743a
+	if (klp_removed && *klp_removed)
81743a
+		return -ENOENT;
81743a
+
81743a
 	if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) {
81743a
 		struct nft_set_dump_args args = {
81743a
 			.iter	= {
81743a
@@ -4152,11 +4243,13 @@ static int __nf_tables_abort(struct net *net)
81743a
 			break;
81743a
 		case NFT_MSG_NEWRULE:
81743a
 			trans->ctx.chain->use--;
81743a
+			nft_rule_expr_deactivate(&trans->ctx, nft_trans_rule(trans));
81743a
 			list_del_rcu(&nft_trans_rule(trans)->list);
81743a
 			break;
81743a
 		case NFT_MSG_DELRULE:
81743a
 			trans->ctx.chain->use++;
81743a
 			nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
81743a
+			nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
81743a
 			nft_trans_destroy(trans);
81743a
 			break;
81743a
 		case NFT_MSG_NEWSET:
81743a
diff --git a/net/netfilter/nft_dynset.c b/net/netfilter/nft_dynset.c
81743a
index 0cf187230050..4d6d3af26a5b 100644
81743a
--- a/net/netfilter/nft_dynset.c
81743a
+++ b/net/netfilter/nft_dynset.c
81743a
@@ -204,6 +204,32 @@ err1:
81743a
 	return err;
81743a
 }
81743a
 
81743a
+void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
81743a
+			      struct nft_set_binding *binding);
81743a
+
81743a
+void nft_dynset_deactivate(const struct nft_ctx *ctx,
81743a
+				  const struct nft_expr *expr)
81743a
+{
81743a
+	struct nft_dynset *priv = nft_expr_priv(expr);
81743a
+
81743a
+	nf_tables_deactivate_set(ctx, priv->set, &priv->binding);
81743a
+}
81743a
+
81743a
+void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set);
81743a
+
81743a
+void nft_dynset_activate(const struct nft_ctx *ctx,
81743a
+				const struct nft_expr *expr)
81743a
+{
81743a
+	struct nft_dynset *priv = nft_expr_priv(expr);
81743a
+
81743a
+	nf_tables_activate_set(ctx, priv->set);
81743a
+}
81743a
+
81743a
+int klp_is_nft_dynset(const struct nft_expr *expr)
81743a
+{
81743a
+	return expr->ops->type == &nft_dynset_type;
81743a
+}
81743a
+
81743a
 static void nft_dynset_destroy(const struct nft_ctx *ctx,
81743a
 			       const struct nft_expr *expr)
81743a
 {
81743a
diff --git a/net/netfilter/nft_lookup.c b/net/netfilter/nft_lookup.c
81743a
index c9ce7d60cd73..ab98a5cb7128 100644
81743a
--- a/net/netfilter/nft_lookup.c
81743a
+++ b/net/netfilter/nft_lookup.c
81743a
@@ -125,6 +125,32 @@ static int nft_lookup_init(const struct nft_ctx *ctx,
81743a
 	return 0;
81743a
 }
81743a
 
81743a
+void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
81743a
+			      struct nft_set_binding *binding);
81743a
+
81743a
+void nft_lookup_deactivate(const struct nft_ctx *ctx,
81743a
+				  const struct nft_expr *expr)
81743a
+{
81743a
+	struct nft_lookup *priv = nft_expr_priv(expr);
81743a
+
81743a
+	nf_tables_deactivate_set(ctx, priv->set, &priv->binding);
81743a
+}
81743a
+
81743a
+void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set);
81743a
+
81743a
+void nft_lookup_activate(const struct nft_ctx *ctx,
81743a
+				const struct nft_expr *expr)
81743a
+{
81743a
+	struct nft_lookup *priv = nft_expr_priv(expr);
81743a
+
81743a
+	nf_tables_activate_set(ctx, priv->set);
81743a
+}
81743a
+
81743a
+int klp_is_nft_lookup(const struct nft_expr *expr)
81743a
+{
81743a
+	return expr->ops->type == &nft_lookup_type;
81743a
+}
81743a
+
81743a
 static void nft_lookup_destroy(const struct nft_ctx *ctx,
81743a
 			       const struct nft_expr *expr)
81743a
 {
81743a
-- 
81743a
2.40.1
81743a
81743a