Blame SOURCES/target-scsi-Fix-XCOPY-NAA-identifier-lookup.patch

2460d0
From 575d2ac72342beea5c3d63fc655d5d173b01283a Mon Sep 17 00:00:00 2001
2460d0
From: Artem Savkov <asavkov@redhat.com>
2460d0
Date: Tue, 2 Feb 2021 11:12:30 +0100
2460d0
Subject: [RHEL7.9 KPATCH v2] [target] scsi: Fix XCOPY NAA identifier lookup
2460d0
2460d0
Kernels:
2460d0
3.10.0-1160.el7
2460d0
3.10.0-1160.2.1.el7
2460d0
3.10.0-1160.2.2.el7
2460d0
3.10.0-1160.6.1.el7
2460d0
3.10.0-1160.11.1.el7
2460d0
2460d0
Changes since last build:
2460d0
[x86_64]:
2460d0
target_core_xcopy.o: changed function: target_do_xcopy
2460d0
target_core_xcopy.o: changed function: target_parse_xcopy_cmd
2460d0
target_core_xcopy.o: changed function: target_xcopy_do_work
2460d0
target_core_xcopy.o: changed function: target_xcopy_locate_se_dev_e4
2460d0
target_core_xcopy.o: changed function: xcopy_pt_undepend_remotedev
2460d0
2460d0
[ppc64le]:
2460d0
target_core_xcopy.o: changed function: target_do_receive_copy_results
2460d0
target_core_xcopy.o: changed function: target_do_xcopy
2460d0
target_core_xcopy.o: changed function: target_parse_xcopy_cmd
2460d0
target_core_xcopy.o: changed function: target_xcopy_do_work
2460d0
target_core_xcopy.o: new function: target_xcopy_locate_se_dev_e4
2460d0
target_core_xcopy.o: new function: xcopy_pt_undepend_remotedev
2460d0
2460d0
---------------------------
2460d0
2460d0
Modifications: shadow var instead of structure fixup
2460d0
2460d0
commit 173ce8ce7c0c334c3406b4826dca6732f101dd2e
2460d0
Author: Maurizio Lombardi <mlombard@redhat.com>
2460d0
Date:   Mon Jan 18 16:24:30 2021 -0500
2460d0
2460d0
    [target] scsi: Fix XCOPY NAA identifier lookup
2460d0
2460d0
    Message-id: <20210118162431.74459-1-mlombard@redhat.com>
2460d0
    Patchwork-id: 10208
2460d0
    Patchwork-instance: patchwork-private
2460d0
    O-Subject: [kernel team] [CVE-2020-28374 RHEL7.9.z e-stor PATCH] scsi: target: Fix XCOPY NAA identifier lookup
2460d0
    Bugzilla: 1900469
2460d0
    CVE: CVE-2020-28374
2460d0
    RH-Acked-by: Tomas Henzl <thenzl@redhat.com>
2460d0
    RH-Acked-by: Ewan D. Milne <emilne@redhat.com>
2460d0
2460d0
    From: David Disseldorp <ddiss@suse.de>
2460d0
2460d0
    BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1900469
2460d0
    Brew: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=34294843
2460d0
    Upstream: cherry-picked from the mainline tree
2460d0
2460d0
    When attempting to match EXTENDED COPY CSCD descriptors with corresponding
2460d0
    se_devices, target_xcopy_locate_se_dev_e4() currently iterates over LIO's
2460d0
    global devices list which includes all configured backstores.
2460d0
2460d0
    This change ensures that only initiator-accessible backstores are
2460d0
    considered during CSCD descriptor lookup, according to the session's
2460d0
    se_node_acl LUN list.
2460d0
2460d0
    To avoid LUN removal race conditions, device pinning is changed from being
2460d0
    configfs based to instead using the se_node_acl lun_ref.
2460d0
2460d0
    Reference: CVE-2020-28374
2460d0
    Fixes: cbf031f425fd ("target: Add support for EXTENDED_COPY copy offload emulation")
2460d0
    Reviewed-by: Lee Duncan <lduncan@suse.com>
2460d0
    Signed-off-by: David Disseldorp <ddiss@suse.de>
2460d0
    Signed-off-by: Mike Christie <michael.christie@oracle.com>
2460d0
    Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2460d0
    (cherry picked from commit 2896c93811e39d63a4d9b63ccf12a8fbc226e5e4)
2460d0
    Signed-off-by: Maurizio Lombardi <mlombard@redhat.com>
2460d0
    Signed-off-by: Augusto Caringi <acaringi@redhat.com>
2460d0
2460d0
Signed-off-by: Artem Savkov <asavkov@redhat.com>
2460d0
Acked-by: Joe Lawrence <joe.lawrence@redhat.com>
2460d0
Acked-by: Josh Poimboeuf <jpoimboe@redhat.com>
2460d0
---
2460d0
 drivers/target/target_core_xcopy.c | 143 +++++++++++++++++++----------
2460d0
 1 file changed, 95 insertions(+), 48 deletions(-)
2460d0
2460d0
diff --git a/drivers/target/target_core_xcopy.c b/drivers/target/target_core_xcopy.c
2460d0
index 252de556dbb3..4d023e403845 100644
2460d0
--- a/drivers/target/target_core_xcopy.c
2460d0
+++ b/drivers/target/target_core_xcopy.c
2460d0
@@ -38,6 +38,8 @@
2460d0
 #include "target_core_ua.h"
2460d0
 #include "target_core_xcopy.h"
2460d0
 
2460d0
+#define KLP_SHADOW_REMOTE_LUN_REF 0x2020283740000000
2460d0
+
2460d0
 static struct workqueue_struct *xcopy_wq = NULL;
2460d0
 
2460d0
 static sense_reason_t target_parse_xcopy_cmd(struct xcopy_op *xop);
2460d0
@@ -55,60 +57,83 @@ static int target_xcopy_gen_naa_ieee(struct se_device *dev, unsigned char *buf)
2460d0
 	return 0;
2460d0
 }
2460d0
 
2460d0
-struct xcopy_dev_search_info {
2460d0
-	const unsigned char *dev_wwn;
2460d0
-	struct se_device *found_dev;
2460d0
-};
2460d0
-
2460d0
+/**
2460d0
+ * target_xcopy_locate_se_dev_e4_iter - compare XCOPY NAA device identifiers
2460d0
+ *
2460d0
+ * @se_dev: device being considered for match
2460d0
+ * @dev_wwn: XCOPY requested NAA dev_wwn
2460d0
+ * @return: 1 on match, 0 on no-match
2460d0
+ */
2460d0
 static int target_xcopy_locate_se_dev_e4_iter(struct se_device *se_dev,
2460d0
-					      void *data)
2460d0
+					      const unsigned char *dev_wwn)
2460d0
 {
2460d0
-	struct xcopy_dev_search_info *info = data;
2460d0
 	unsigned char tmp_dev_wwn[XCOPY_NAA_IEEE_REGEX_LEN];
2460d0
 	int rc;
2460d0
 
2460d0
-	if (!se_dev->dev_attrib.emulate_3pc)
2460d0
+	if (!se_dev->dev_attrib.emulate_3pc) {
2460d0
+		pr_debug("XCOPY: emulate_3pc disabled on se_dev %p\n", se_dev);
2460d0
 		return 0;
2460d0
+	}
2460d0
 
2460d0
 	memset(&tmp_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
2460d0
 	target_xcopy_gen_naa_ieee(se_dev, &tmp_dev_wwn[0]);
2460d0
 
2460d0
-	rc = memcmp(&tmp_dev_wwn[0], info->dev_wwn, XCOPY_NAA_IEEE_REGEX_LEN);
2460d0
-	if (rc != 0)
2460d0
-		return 0;
2460d0
-
2460d0
-	info->found_dev = se_dev;
2460d0
-	pr_debug("XCOPY 0xe4: located se_dev: %p\n", se_dev);
2460d0
-
2460d0
-	rc = target_depend_item(&se_dev->dev_group.cg_item);
2460d0
+	rc = memcmp(&tmp_dev_wwn[0], dev_wwn, XCOPY_NAA_IEEE_REGEX_LEN);
2460d0
 	if (rc != 0) {
2460d0
-		pr_err("configfs_depend_item attempt failed: %d for se_dev: %p\n",
2460d0
-		       rc, se_dev);
2460d0
-		return rc;
2460d0
+		pr_debug("XCOPY: skip non-matching: %*ph\n",
2460d0
+			 XCOPY_NAA_IEEE_REGEX_LEN, tmp_dev_wwn);
2460d0
+		return 0;
2460d0
 	}
2460d0
+	pr_debug("XCOPY 0xe4: located se_dev: %p\n", se_dev);
2460d0
 
2460d0
-	pr_debug("Called configfs_depend_item for se_dev: %p se_dev->se_dev_group: %p\n",
2460d0
-		 se_dev, &se_dev->dev_group);
2460d0
 	return 1;
2460d0
 }
2460d0
 
2460d0
-static int target_xcopy_locate_se_dev_e4(const unsigned char *dev_wwn,
2460d0
-					struct se_device **found_dev)
2460d0
+static int target_xcopy_locate_se_dev_e4(struct se_session *sess,
2460d0
+					const unsigned char *dev_wwn,
2460d0
+					struct se_device **_found_dev,
2460d0
+					struct percpu_ref **_found_lun_ref)
2460d0
 {
2460d0
-	struct xcopy_dev_search_info info;
2460d0
-	int ret;
2460d0
-
2460d0
-	memset(&info, 0, sizeof(info));
2460d0
-	info.dev_wwn = dev_wwn;
2460d0
-
2460d0
-	ret = target_for_each_device(target_xcopy_locate_se_dev_e4_iter, &info;;
2460d0
-	if (ret == 1) {
2460d0
-		*found_dev = info.found_dev;
2460d0
-		return 0;
2460d0
-	} else {
2460d0
-		pr_debug_ratelimited("Unable to locate 0xe4 descriptor for EXTENDED_COPY\n");
2460d0
-		return -EINVAL;
2460d0
+	struct se_dev_entry *deve;
2460d0
+	struct se_node_acl *nacl;
2460d0
+	struct se_lun *this_lun = NULL;
2460d0
+	struct se_device *found_dev = NULL;
2460d0
+
2460d0
+	/* cmd with NULL sess indicates no associated $FABRIC_MOD */
2460d0
+	if (!sess)
2460d0
+		goto err_out;
2460d0
+
2460d0
+	pr_debug("XCOPY 0xe4: searching for: %*ph\n",
2460d0
+		 XCOPY_NAA_IEEE_REGEX_LEN, dev_wwn);
2460d0
+
2460d0
+	nacl = sess->se_node_acl;
2460d0
+	rcu_read_lock();
2460d0
+	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
2460d0
+		struct se_device *this_dev;
2460d0
+		int rc;
2460d0
+
2460d0
+		this_lun = rcu_dereference(deve->se_lun);
2460d0
+		this_dev = rcu_dereference_raw(this_lun->lun_se_dev);
2460d0
+
2460d0
+		rc = target_xcopy_locate_se_dev_e4_iter(this_dev, dev_wwn);
2460d0
+		if (rc) {
2460d0
+			if (percpu_ref_tryget_live(&this_lun->lun_ref))
2460d0
+				found_dev = this_dev;
2460d0
+			break;
2460d0
+		}
2460d0
 	}
2460d0
+	rcu_read_unlock();
2460d0
+	if (found_dev == NULL)
2460d0
+		goto err_out;
2460d0
+
2460d0
+	pr_debug("lun_ref held for se_dev: %p se_dev->se_dev_group: %p\n",
2460d0
+		 found_dev, &found_dev->dev_group);
2460d0
+	*_found_dev = found_dev;
2460d0
+	*_found_lun_ref = &this_lun->lun_ref;
2460d0
+	return 0;
2460d0
+err_out:
2460d0
+	pr_debug_ratelimited("Unable to locate 0xe4 descriptor for EXTENDED_COPY\n");
2460d0
+	return -EINVAL;
2460d0
 }
2460d0
 
2460d0
 static int target_xcopy_parse_tiddesc_e4(struct se_cmd *se_cmd, struct xcopy_op *xop,
2460d0
@@ -197,6 +222,8 @@ static int target_xcopy_parse_tiddesc_e4(struct se_cmd *se_cmd, struct xcopy_op
2460d0
 	return 0;
2460d0
 }
2460d0
 
2460d0
+#include "kpatch-macros.h"
2460d0
+
2460d0
 static int target_xcopy_parse_target_descriptors(struct se_cmd *se_cmd,
2460d0
 				struct xcopy_op *xop, unsigned char *p,
2460d0
 				unsigned short tdll, sense_reason_t *sense_ret)
2460d0
@@ -206,6 +233,7 @@ static int target_xcopy_parse_target_descriptors(struct se_cmd *se_cmd,
2460d0
 	int offset = tdll % XCOPY_TARGET_DESC_LEN, rc;
2460d0
 	unsigned short cscd_index = 0;
2460d0
 	unsigned short start = 0;
2460d0
+	struct percpu_ref **remote_lun_ref;
2460d0
 
2460d0
 	*sense_ret = TCM_INVALID_PARAMETER_LIST;
2460d0
 
2460d0
@@ -253,14 +281,24 @@ static int target_xcopy_parse_target_descriptors(struct se_cmd *se_cmd,
2460d0
 		}
2460d0
 	}
2460d0
 
2460d0
+	remote_lun_ref = klp_shadow_get_or_alloc(xop, KLP_SHADOW_REMOTE_LUN_REF,
2460d0
+			sizeof(struct percpu_ref*), GFP_KERNEL, NULL, NULL);
2460d0
+
2460d0
+	if (!remote_lun_ref)
2460d0
+		goto out;
2460d0
+
2460d0
 	switch (xop->op_origin) {
2460d0
 	case XCOL_SOURCE_RECV_OP:
2460d0
-		rc = target_xcopy_locate_se_dev_e4(xop->dst_tid_wwn,
2460d0
-						&xop->dst_dev);
2460d0
+		rc = target_xcopy_locate_se_dev_e4(se_cmd->se_sess,
2460d0
+						xop->dst_tid_wwn,
2460d0
+						&xop->dst_dev,
2460d0
+						remote_lun_ref);
2460d0
 		break;
2460d0
 	case XCOL_DEST_RECV_OP:
2460d0
-		rc = target_xcopy_locate_se_dev_e4(xop->src_tid_wwn,
2460d0
-						&xop->src_dev);
2460d0
+		rc = target_xcopy_locate_se_dev_e4(se_cmd->se_sess,
2460d0
+						xop->src_tid_wwn,
2460d0
+						&xop->src_dev,
2460d0
+						remote_lun_ref);
2460d0
 		break;
2460d0
 	default:
2460d0
 		pr_err("XCOPY CSCD descriptor IDs not found in CSCD list - "
2460d0
@@ -406,18 +444,16 @@ static int xcopy_pt_get_cmd_state(struct se_cmd *se_cmd)
2460d0
 
2460d0
 static void xcopy_pt_undepend_remotedev(struct xcopy_op *xop)
2460d0
 {
2460d0
-	struct se_device *remote_dev;
2460d0
+	struct percpu_ref **remote_lun_ref = klp_shadow_get(xop,
2460d0
+						KLP_SHADOW_REMOTE_LUN_REF);
2460d0
 
2460d0
 	if (xop->op_origin == XCOL_SOURCE_RECV_OP)
2460d0
-		remote_dev = xop->dst_dev;
2460d0
+		pr_debug("putting dst lun_ref for %p\n", xop->dst_dev);
2460d0
 	else
2460d0
-		remote_dev = xop->src_dev;
2460d0
+		pr_debug("putting src lun_ref for %p\n", xop->src_dev);
2460d0
 
2460d0
-	pr_debug("Calling configfs_undepend_item for"
2460d0
-		  " remote_dev: %p remote_dev->dev_group: %p\n",
2460d0
-		  remote_dev, &remote_dev->dev_group.cg_item);
2460d0
-
2460d0
-	target_undepend_item(&remote_dev->dev_group.cg_item);
2460d0
+	if (remote_lun_ref)
2460d0
+		percpu_ref_put(*remote_lun_ref);
2460d0
 }
2460d0
 
2460d0
 static void xcopy_pt_release_cmd(struct se_cmd *se_cmd)
2460d0
@@ -857,6 +893,7 @@ static void target_xcopy_do_work(struct work_struct *work)
2460d0
 	}
2460d0
 
2460d0
 	xcopy_pt_undepend_remotedev(xop);
2460d0
+	klp_shadow_free(xop, KLP_SHADOW_REMOTE_LUN_REF, NULL);
2460d0
 	kfree(xop);
2460d0
 
2460d0
 	pr_debug("target_xcopy_do_work: Final src_lba: %llu, dst_lba: %llu\n",
2460d0
@@ -872,6 +909,7 @@ out:
2460d0
 	xcopy_pt_undepend_remotedev(xop);
2460d0
 
2460d0
 err_free:
2460d0
+	klp_shadow_free(xop, KLP_SHADOW_REMOTE_LUN_REF, NULL);
2460d0
 	kfree(xop);
2460d0
 	/*
2460d0
 	 * Don't override an error scsi status if it has already been set
2460d0
@@ -981,6 +1019,7 @@ sense_reason_t target_do_xcopy(struct se_cmd *se_cmd)
2460d0
 	struct se_device *dev = se_cmd->se_dev;
2460d0
 	struct xcopy_op *xop;
2460d0
 	unsigned int sa;
2460d0
+	struct percpu_ref **remote_lun_ref;
2460d0
 
2460d0
 	if (!dev->dev_attrib.emulate_3pc) {
2460d0
 		pr_err("EXTENDED_COPY operation explicitly disabled\n");
2460d0
@@ -1006,6 +1045,12 @@ sense_reason_t target_do_xcopy(struct se_cmd *se_cmd)
2460d0
 	xop = kzalloc(sizeof(struct xcopy_op), GFP_KERNEL);
2460d0
 	if (!xop)
2460d0
 		goto err;
2460d0
+
2460d0
+	remote_lun_ref = klp_shadow_alloc(xop, KLP_SHADOW_REMOTE_LUN_REF,
2460d0
+			sizeof(struct percpu_ref*), GFP_KERNEL, NULL, NULL);
2460d0
+	if (!remote_lun_ref)
2460d0
+		goto xop_free;
2460d0
+
2460d0
 	xop->xop_se_cmd = se_cmd;
2460d0
 	INIT_WORK(&xop->xop_work, target_xcopy_do_work);
2460d0
 	if (WARN_ON_ONCE(!queue_work(xcopy_wq, &xop->xop_work)))
2460d0
@@ -1013,6 +1058,8 @@ sense_reason_t target_do_xcopy(struct se_cmd *se_cmd)
2460d0
 	return TCM_NO_SENSE;
2460d0
 
2460d0
 free:
2460d0
+	klp_shadow_free(xop, KLP_SHADOW_REMOTE_LUN_REF, NULL);
2460d0
+xop_free:
2460d0
 	kfree(xop);
2460d0
 
2460d0
 err:
2460d0
-- 
2460d0
2.26.2
2460d0