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

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