Blame SOURCES/CVE-2022-4744.patch

497ac3
From 951ae7dd395dd1407cfc6d7f2f59163850ec0362 Mon Sep 17 00:00:00 2001
497ac3
From: Yannick Cote <ycote@redhat.com>
497ac3
Date: Tue, 14 Mar 2023 21:40:48 -0400
497ac3
Subject: [KPATCH CVE-2022-4744] kpatch fixes for CVE-2022-4744
497ac3
497ac3
Kernels:
497ac3
5.14.0-162.6.1.el9_1
497ac3
5.14.0-162.12.1.el9_1
497ac3
5.14.0-162.18.1.el9_1
497ac3
497ac3
497ac3
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-9/-/merge_requests/28
497ac3
Approved-by: Joe Lawrence (@joe.lawrence)
497ac3
Changes since last build:
497ac3
[x86_64]:
497ac3
control.o: changed function: snd_ctl_elem_read
497ac3
control.o: changed function: snd_ctl_ioctl
497ac3
dev.o: changed function: register_netdevice
497ac3
tun.o: changed function: tun_free_netdev
497ac3
tun.o: changed function: tun_set_iff.constprop.0
497ac3
tun.o: new function: kpp_cve_2022_4744_tun_net_init
497ac3
497ac3
[ppc64le]:
497ac3
dev.o: changed function: register_netdevice
497ac3
tun.o: changed function: tun_free_netdev
497ac3
tun.o: changed function: tun_set_iff.constprop.0
497ac3
tun.o: new function: kpp_cve_2022_4744_tun_net_init
497ac3
497ac3
---------------------------
497ac3
497ac3
Modifications:
497ac3
- add shadow variables for tun->ifr, tun->file, ndo_init
497ac3
- call init from register_netdevice() when shadow variables are detected
497ac3
- renamed new tun_net_init() -> kpp_cve_2022_4744_tun_net_init()
497ac3
- code to allocate, maintain and remove shadow variables
497ac3
497ac3
commit 8ab79b18abf2f3a2cf33903794ff0de7cec105fc
497ac3
Author: Jon Maloy <jmaloy@redhat.com>
497ac3
Date:   Wed Mar 8 11:35:45 2023 -0500
497ac3
497ac3
    tun: avoid double free in tun_free_netdev
497ac3
497ac3
    Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2156372
497ac3
    Upstream: Merged
497ac3
    CVE: CVE-2022-4744
497ac3
497ac3
    commit 158b515f703e75e7d68289bf4d98c664e1d632df
497ac3
    Author: George Kennedy <george.kennedy@oracle.com>
497ac3
    Date:   Thu Dec 16 13:25:32 2021 -0500
497ac3
497ac3
        tun: avoid double free in tun_free_netdev
497ac3
497ac3
        Avoid double free in tun_free_netdev() by moving the
497ac3
        dev->tstats and tun->security allocs to a new ndo_init routine
497ac3
        (tun_net_init()) that will be called by register_netdevice().
497ac3
        ndo_init is paired with the desctructor (tun_free_netdev()),
497ac3
        so if there's an error in register_netdevice() the destructor
497ac3
        will handle the frees.
497ac3
497ac3
        BUG: KASAN: double-free or invalid-free in selinux_tun_dev_free_security+0x1a/0x20 security/selinux/hooks.c:5605
497ac3
497ac3
        CPU: 0 PID: 25750 Comm: syz-executor416 Not tainted 5.16.0-rc2-syzk #1
497ac3
        Hardware name: Red Hat KVM, BIOS
497ac3
        Call Trace:
497ac3
        <TASK>
497ac3
        __dump_stack lib/dump_stack.c:88 [inline]
497ac3
        dump_stack_lvl+0x89/0xb5 lib/dump_stack.c:106
497ac3
        print_address_description.constprop.9+0x28/0x160 mm/kasan/report.c:247
497ac3
        kasan_report_invalid_free+0x55/0x80 mm/kasan/report.c:372
497ac3
        ____kasan_slab_free mm/kasan/common.c:346 [inline]
497ac3
        __kasan_slab_free+0x107/0x120 mm/kasan/common.c:374
497ac3
        kasan_slab_free include/linux/kasan.h:235 [inline]
497ac3
        slab_free_hook mm/slub.c:1723 [inline]
497ac3
        slab_free_freelist_hook mm/slub.c:1749 [inline]
497ac3
        slab_free mm/slub.c:3513 [inline]
497ac3
        kfree+0xac/0x2d0 mm/slub.c:4561
497ac3
        selinux_tun_dev_free_security+0x1a/0x20 security/selinux/hooks.c:5605
497ac3
        security_tun_dev_free_security+0x4f/0x90 security/security.c:2342
497ac3
        tun_free_netdev+0xe6/0x150 drivers/net/tun.c:2215
497ac3
        netdev_run_todo+0x4df/0x840 net/core/dev.c:10627
497ac3
        rtnl_unlock+0x13/0x20 net/core/rtnetlink.c:112
497ac3
        __tun_chr_ioctl+0x80c/0x2870 drivers/net/tun.c:3302
497ac3
        tun_chr_ioctl+0x2f/0x40 drivers/net/tun.c:3311
497ac3
        vfs_ioctl fs/ioctl.c:51 [inline]
497ac3
        __do_sys_ioctl fs/ioctl.c:874 [inline]
497ac3
        __se_sys_ioctl fs/ioctl.c:860 [inline]
497ac3
        __x64_sys_ioctl+0x19d/0x220 fs/ioctl.c:860
497ac3
        do_syscall_x64 arch/x86/entry/common.c:50 [inline]
497ac3
        do_syscall_64+0x3a/0x80 arch/x86/entry/common.c:80
497ac3
        entry_SYSCALL_64_after_hwframe+0x44/0xae
497ac3
497ac3
        Reported-by: syzkaller <syzkaller@googlegroups.com>
497ac3
        Signed-off-by: George Kennedy <george.kennedy@oracle.com>
497ac3
        Suggested-by: Jakub Kicinski <kuba@kernel.org>
497ac3
        Link: https://lore.kernel.org/r/1639679132-19884-1-git-send-email-george.kennedy@oracle.com
497ac3
        Signed-off-by: Jakub Kicinski <kuba@kernel.org>
497ac3
497ac3
    Signed-off-by: Jon Maloy <jmaloy@redhat.com>
497ac3
497ac3
Signed-off-by: Yannick Cote <ycote@redhat.com>
497ac3
---
497ac3
 drivers/net/tun.c | 135 ++++++++++++++++++++++++++++------------------
497ac3
 net/core/dev.c    |  31 ++++++++++-
497ac3
 2 files changed, 112 insertions(+), 54 deletions(-)
497ac3
497ac3
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
497ac3
index a3aec566fb4b..2a2fee5e3c10 100644
497ac3
--- a/drivers/net/tun.c
497ac3
+++ b/drivers/net/tun.c
497ac3
@@ -216,6 +216,9 @@ struct veth {
497ac3
 	__be16 h_vlan_TCI;
497ac3
 };
497ac3
 
497ac3
+static void tun_flow_init(struct tun_struct *tun);
497ac3
+static void tun_flow_uninit(struct tun_struct *tun);
497ac3
+
497ac3
 static int tun_napi_receive(struct napi_struct *napi, int budget)
497ac3
 {
497ac3
 	struct tun_file *tfile = container_of(napi, struct tun_file, napi);
497ac3
@@ -953,6 +956,67 @@ static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
497ac3
 
497ac3
 static const struct ethtool_ops tun_ethtool_ops;
497ac3
 
497ac3
+/* CVE-2022-4744 - kpatch gathered definitions */
497ac3
+#ifndef __GENKSYMS__
497ac3
+/* Note: avoid symvers churn for tun_get_tx_ring and tun_get_socket */
497ac3
+# include <linux/livepatch.h>
497ac3
+# define KLP_CVE_2022_4744  0x2022474400000001
497ac3
+struct klp_cve_2022_4774_t {
497ac3
+	struct ifreq tun_ifr;
497ac3
+	struct file tun_file;
497ac3
+	int (*ndo_init)(struct net_device *netdev);
497ac3
+};
497ac3
+#endif
497ac3
+int kpp_cve_2022_4744_tun_net_init(struct net_device *dev)
497ac3
+{
497ac3
+	struct tun_struct *tun = netdev_priv(dev);
497ac3
+	int err;
497ac3
+	struct klp_cve_2022_4774_t *klp_sv;
497ac3
+
497ac3
+	klp_sv = klp_shadow_get(tun, KLP_CVE_2022_4744);
497ac3
+	if (!klp_sv)
497ac3
+		return -EINVAL;
497ac3
+
497ac3
+	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
497ac3
+	if (!dev->tstats)
497ac3
+		return -ENOMEM;
497ac3
+
497ac3
+	spin_lock_init(&tun->lock);
497ac3
+
497ac3
+	err = security_tun_dev_alloc_security(&tun->security);
497ac3
+	if (err < 0) {
497ac3
+		free_percpu(dev->tstats);
497ac3
+		return err;
497ac3
+	}
497ac3
+
497ac3
+	tun_flow_init(tun);
497ac3
+
497ac3
+	dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
497ac3
+			   TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
497ac3
+			   NETIF_F_HW_VLAN_STAG_TX;
497ac3
+	dev->features = dev->hw_features | NETIF_F_LLTX;
497ac3
+	dev->vlan_features = dev->features &
497ac3
+			     ~(NETIF_F_HW_VLAN_CTAG_TX |
497ac3
+			       NETIF_F_HW_VLAN_STAG_TX);
497ac3
+	klp_sv = klp_shadow_get(tun, KLP_CVE_2022_4744);
497ac3
+	if (klp_sv) {
497ac3
+		tun->flags = (tun->flags & ~TUN_FEATURES) |
497ac3
+			     (klp_sv->tun_ifr.ifr_flags & TUN_FEATURES);
497ac3
+
497ac3
+		INIT_LIST_HEAD(&tun->disabled);
497ac3
+		err = tun_attach(tun, &klp_sv->tun_file, false,
497ac3
+			 klp_sv->tun_ifr.ifr_flags & IFF_NAPI,
497ac3
+			 klp_sv->tun_ifr.ifr_flags & IFF_NAPI_FRAGS, false);
497ac3
+	}
497ac3
+	if (err < 0) {
497ac3
+		tun_flow_uninit(tun);
497ac3
+		security_tun_dev_free_security(tun->security);
497ac3
+		free_percpu(dev->tstats);
497ac3
+		return err;
497ac3
+	}
497ac3
+	return 0;
497ac3
+}
497ac3
+
497ac3
 /* Net device detach from fd. */
497ac3
 static void tun_net_uninit(struct net_device *dev)
497ac3
 {
497ac3
@@ -2206,15 +2270,11 @@ static void tun_free_netdev(struct net_device *dev)
497ac3
 	BUG_ON(!(list_empty(&tun->disabled)));
497ac3
 
497ac3
 	free_percpu(dev->tstats);
497ac3
-	/* We clear tstats so that tun_set_iff() can tell if
497ac3
-	 * tun_free_netdev() has been called from register_netdevice().
497ac3
-	 */
497ac3
-	dev->tstats = NULL;
497ac3
-
497ac3
 	tun_flow_uninit(tun);
497ac3
 	security_tun_dev_free_security(tun->security);
497ac3
 	__tun_set_ebpf(tun, &tun->steering_prog, NULL);
497ac3
 	__tun_set_ebpf(tun, &tun->filter_prog, NULL);
497ac3
+	klp_shadow_free(tun, KLP_CVE_2022_4744, NULL);
497ac3
 }
497ac3
 
497ac3
 static void tun_setup(struct net_device *dev)
497ac3
@@ -2716,41 +2776,30 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
497ac3
 		tun->rx_batched = 0;
497ac3
 		RCU_INIT_POINTER(tun->steering_prog, NULL);
497ac3
 
497ac3
-		dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
497ac3
-		if (!dev->tstats) {
497ac3
-			err = -ENOMEM;
497ac3
-			goto err_free_dev;
497ac3
-		}
497ac3
+/* kpatch */
497ac3
+{
497ac3
+		struct klp_cve_2022_4774_t *klp_sv;
497ac3
 
497ac3
-		spin_lock_init(&tun->lock);
497ac3
+		klp_sv = klp_shadow_alloc(tun, KLP_CVE_2022_4744,
497ac3
+					  sizeof(*klp_sv), GFP_KERNEL, NULL, NULL);
497ac3
+		if (!klp_sv) {
497ac3
+			free_netdev(dev);
497ac3
+			return -ENOMEM;
497ac3
+		}
497ac3
 
497ac3
-		err = security_tun_dev_alloc_security(&tun->security);
497ac3
-		if (err < 0)
497ac3
-			goto err_free_stat;
497ac3
+		memcpy(&klp_sv->tun_ifr, ifr, sizeof(*ifr));
497ac3
+		memcpy(&klp_sv->tun_file, file, sizeof(*file));
497ac3
+		klp_sv->ndo_init = kpp_cve_2022_4744_tun_net_init;
497ac3
+}
497ac3
 
497ac3
 		tun_net_init(dev);
497ac3
-		tun_flow_init(tun);
497ac3
-
497ac3
-		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
497ac3
-				   TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
497ac3
-				   NETIF_F_HW_VLAN_STAG_TX;
497ac3
-		dev->features = dev->hw_features | NETIF_F_LLTX;
497ac3
-		dev->vlan_features = dev->features &
497ac3
-				     ~(NETIF_F_HW_VLAN_CTAG_TX |
497ac3
-				       NETIF_F_HW_VLAN_STAG_TX);
497ac3
-
497ac3
-		tun->flags = (tun->flags & ~TUN_FEATURES) |
497ac3
-			      (ifr->ifr_flags & TUN_FEATURES);
497ac3
-
497ac3
-		INIT_LIST_HEAD(&tun->disabled);
497ac3
-		err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI,
497ac3
-				 ifr->ifr_flags & IFF_NAPI_FRAGS, false);
497ac3
-		if (err < 0)
497ac3
-			goto err_free_flow;
497ac3
 
497ac3
 		err = register_netdevice(tun->dev);
497ac3
-		if (err < 0)
497ac3
-			goto err_detach;
497ac3
+		if (err < 0) {
497ac3
+			klp_shadow_free(tun, KLP_CVE_2022_4744, NULL);
497ac3
+			free_netdev(dev);
497ac3
+			return err;
497ac3
+		}
497ac3
 		/* free_netdev() won't check refcnt, to avoid race
497ac3
 		 * with dev_put() we need publish tun after registration.
497ac3
 		 */
497ac3
@@ -2767,24 +2816,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
497ac3
 
497ac3
 	strcpy(ifr->ifr_name, tun->dev->name);
497ac3
 	return 0;
497ac3
-
497ac3
-err_detach:
497ac3
-	tun_detach_all(dev);
497ac3
-	/* We are here because register_netdevice() has failed.
497ac3
-	 * If register_netdevice() already called tun_free_netdev()
497ac3
-	 * while dealing with the error, dev->stats has been cleared.
497ac3
-	 */
497ac3
-	if (!dev->tstats)
497ac3
-		goto err_free_dev;
497ac3
-
497ac3
-err_free_flow:
497ac3
-	tun_flow_uninit(tun);
497ac3
-	security_tun_dev_free_security(tun->security);
497ac3
-err_free_stat:
497ac3
-	free_percpu(dev->tstats);
497ac3
-err_free_dev:
497ac3
-	free_netdev(dev);
497ac3
-	return err;
497ac3
 }
497ac3
 
497ac3
 static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr)
497ac3
diff --git a/net/core/dev.c b/net/core/dev.c
497ac3
index 89ff6b1e7735..d5f07da178dc 100644
497ac3
--- a/net/core/dev.c
497ac3
+++ b/net/core/dev.c
497ac3
@@ -9544,6 +9544,15 @@ EXPORT_SYMBOL(netif_tx_stop_all_queues);
497ac3
  *	will not get the same name.
497ac3
  */
497ac3
 
497ac3
+/* CVE-2022-4744 - kpatch gathered definitions */
497ac3
+#include <linux/livepatch.h>
497ac3
+#define KLP_CVE_2022_4744  0x2022474400000001
497ac3
+struct klp_cve_2022_4774_t {
497ac3
+	struct ifreq tun_ifr;
497ac3
+	struct file tun_file;
497ac3
+	int (*ndo_init)(struct net_device *netdev);
497ac3
+};
497ac3
+
497ac3
 int register_netdevice(struct net_device *dev)
497ac3
 {
497ac3
 	int ret;
497ac3
@@ -9576,15 +9585,33 @@ int register_netdevice(struct net_device *dev)
497ac3
 	if (!dev->name_node)
497ac3
 		goto out;
497ac3
 
497ac3
+/* kpatch */
497ac3
+{
497ac3
 	/* Init, if this function is available */
497ac3
-	if (dev->netdev_ops->ndo_init) {
497ac3
-		ret = dev->netdev_ops->ndo_init(dev);
497ac3
+	struct klp_cve_2022_4774_t *klp_sv;
497ac3
+	int (*ndo_init)(struct net_device *netdev) = NULL;
497ac3
+
497ac3
+	/* Does device has shadow variable ndo_init? */
497ac3
+	if (netdev_priv(dev)) {
497ac3
+		klp_sv = klp_shadow_get(netdev_priv(dev), KLP_CVE_2022_4744);
497ac3
+		if (klp_sv && klp_sv->ndo_init)
497ac3
+			ndo_init = klp_sv->ndo_init;
497ac3
+	}
497ac3
+
497ac3
+	/* How about typical netdev_ops->ndo_init */
497ac3
+	if (!ndo_init && dev->netdev_ops->ndo_init)
497ac3
+		ndo_init = dev->netdev_ops->ndo_init;
497ac3
+
497ac3
+	/* Run ndo_init callback if found */
497ac3
+	if (ndo_init) {
497ac3
+		ret = ndo_init(dev);
497ac3
 		if (ret) {
497ac3
 			if (ret > 0)
497ac3
 				ret = -EIO;
497ac3
 			goto err_free_name;
497ac3
 		}
497ac3
 	}
497ac3
+}
497ac3
 
497ac3
 	if (((dev->hw_features | dev->features) &
497ac3
 	     NETIF_F_HW_VLAN_CTAG_FILTER) &&
497ac3
-- 
497ac3
2.39.2
497ac3
497ac3