Blame SOURCES/CVE-2022-4744.patch

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