Skip to contentSkip to conversation

ses: prevent from out of bounds accesses

PATCH Applied Tomas Henzl 2 participants 7 messages Feb 2, 2023 – Feb 22, 2023 SCSI

lore
Share & tools
lore
#0 lore raw

v2 cover ses: prevent from out of bounds accesses

First patch fixes a KASAN reported problem
Second patch fixes other possible places in ses_enclosure_data_process where the max_desc_len might access memory out of bounds. 3/4 does the same for desc_ptr in ses_enclosure_data_process. The last patch fixes another KASAN report in ses_intf_remove.

Changes:
v1: cc-ed [email protected]

Tomas Henzl (4):
ses: fix slab-out-of-bounds reported by KASAN in ses_enclosure_data_process
ses: fix possible addl_desc_ptr out-of-bounds accesses in ses_enclosure_data_process
ses: fix possible desc_ptr out-of-bounds accesses in ses_enclosure_data_process
ses: fix slab-out-of-bounds reported by KASAN in ses_intf_remove

 drivers/scsi/ses.c | 58 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 41 insertions(+), 17 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH v2 1/4 ses: fix slab-out-of-bounds reported by KASAN in ses_enclosure_data_process

A fix for:

BUG: KASAN: slab-out-of-bounds in ses_enclosure_data_process+0x949/0xe30 [ses]
Read of size 1 at addr ffff88a1b043a451 by task systemd-udevd/3271

Checking after (and before in next loop) addl_desc_ptr[1] is sufficient, we expect the size to be sanitized before first access to addl_desc_ptr[1]. Testing for one more byte shall protect partially the ses_process_descriptor.

---
 drivers/scsi/ses.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/scsi/ses.c +4 -2
index 0a1734f34587..1ee927ac8603 100644
@@ -603,9 +603,11 @@ static void ses_enclosure_data_process(struct enclosure_device *edev,
 			     /* these elements are optional */
 			     type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
 			     type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
-			     type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
+			     type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS)){
 				addl_desc_ptr += addl_desc_ptr[1] + 2;
-
+				if (addl_desc_ptr + 1 >= ses_dev->page10 + ses_dev->page10_len)
+					addl_desc_ptr = NULL;
+			}
 		}
 	}
 	kfree(buf);
Signature · 1 line
2.38.1
#2 lore raw

PATCH v2 2/4 ses: fix possible addl_desc_ptr out-of-bounds accesses in ses_enclosure_data_process

In reply to Tomas Henzl

Sanitize possible addl_desc_ptr out-of-bounds accesses in ses_enclosure_data_process.

---
 drivers/scsi/ses.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
drivers/scsi/ses.c +26 -9
index 1ee927ac8603..896fd4f6e93d 100644
@@ -433,8 +433,8 @@ int ses_match_host(struct enclosure_device *edev, void *data)
 }
 #endif  /*  0  */
 
-static void ses_process_descriptor(struct enclosure_component *ecomp,
-				   unsigned char *desc)
+static int ses_process_descriptor(struct enclosure_component *ecomp,
+				   unsigned char *desc, int max_desc_len)
 {
 	int eip = desc[0] & 0x10;
 	int invalid = desc[0] & 0x80;
@@ -445,22 +445,32 @@ static void ses_process_descriptor(struct enclosure_component *ecomp,
 	unsigned char *d;
 
 	if (invalid)
-		return;
+		return 0;
 
 	switch (proto) {
 	case SCSI_PROTOCOL_FCP:
 		if (eip) {
+			if (max_desc_len <= 7)
+				return 1;
 			d = desc + 4;
 			slot = d[3];
 		}
 		break;
 	case SCSI_PROTOCOL_SAS:
+
 		if (eip) {
+			if (max_desc_len <= 27)
+				return 1;
 			d = desc + 4;
 			slot = d[3];
 			d = desc + 8;
-		} else
+		} else {
+			if (max_desc_len <= 23)
+				return 1;
 			d = desc + 4;
+		}
+
+
 		/* only take the phy0 addr */
 		addr = (u64)d[12] << 56 |
 			(u64)d[13] << 48 |
@@ -477,6 +487,8 @@ static void ses_process_descriptor(struct enclosure_component *ecomp,
 	}
 	ecomp->slot = slot;
 	scomp->addr = addr;
+
+	return 0;
 }
 
 struct efd {
@@ -549,7 +561,7 @@ static void ses_enclosure_data_process(struct enclosure_device *edev,
 		/* skip past overall descriptor */
 		desc_ptr += len + 4;
 	}
-	if (ses_dev->page10)
+	if (ses_dev->page10 && ses_dev->page10_len > 9)
 		addl_desc_ptr = ses_dev->page10 + 8;
 	type_ptr = ses_dev->page1_types;
 	components = 0;
@@ -557,6 +569,7 @@ static void ses_enclosure_data_process(struct enclosure_device *edev,
 		for (j = 0; j < type_ptr[1]; j++) {
 			char *name = NULL;
 			struct enclosure_component *ecomp;
+			int max_desc_len;
 
 			if (desc_ptr) {
 				if (desc_ptr >= buf + page7_len) {
@@ -583,10 +596,14 @@ static void ses_enclosure_data_process(struct enclosure_device *edev,
 					ecomp = &edev->component[components++];
 
 				if (!IS_ERR(ecomp)) {
-					if (addl_desc_ptr)
-						ses_process_descriptor(
-							ecomp,
-							addl_desc_ptr);
+					if (addl_desc_ptr) {
+						max_desc_len = ses_dev->page10_len -
+						    (addl_desc_ptr - ses_dev->page10);
+						if (ses_process_descriptor(ecomp,
+						    addl_desc_ptr,
+						    max_desc_len))
+							addl_desc_ptr = NULL;
+					}
 					if (create)
 						enclosure_component_register(
 							ecomp);
Signature · 1 line
2.38.1
#3 lore raw

PATCH v2 3/4 ses: fix possible desc_ptr out-of-bounds accesses in ses_enclosure_data_process

In reply to Tomas Henzl

Sanitize possible desc_ptr out-of-bounds accesses
in ses_enclosure_data_process.

---
 drivers/scsi/ses.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/scsi/ses.c +9 -5
index 896fd4f6e93d..dbfe12f63c98 100644
@@ -572,15 +572,19 @@ static void ses_enclosure_data_process(struct enclosure_device *edev,
 			int max_desc_len;
 
 			if (desc_ptr) {
-				if (desc_ptr >= buf + page7_len) {
+				if (desc_ptr + 3 >= buf + page7_len) {
 					desc_ptr = NULL;
 				} else {
 					len = (desc_ptr[2] << 8) + desc_ptr[3];
 					desc_ptr += 4;
-					/* Add trailing zero - pushes into
-					 * reserved space */
-					desc_ptr[len] = '\0';
-					name = desc_ptr;
+					if (desc_ptr + len > buf + page7_len)
+						desc_ptr = NULL;
+					else {
+						/* Add trailing zero - pushes into
+						 * reserved space */
+						desc_ptr[len] = '\0';
+						name = desc_ptr;
+					}
 				}
 			}
 			if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
Signature · 1 line
2.38.1
#4 lore raw

PATCH v2 4/4 ses: fix slab-out-of-bounds reported by KASAN in ses_intf_remove

In reply to Tomas Henzl

A fix for:

BUG: KASAN: slab-out-of-bounds in ses_intf_remove+0x23f/0x270 [ses]
Read of size 8 at addr ffff88a10d32e5d8 by task rmmod/12013

When edev->components is zero, accessing edev->component[0] members is wrong.

---
 drivers/scsi/ses.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/scsi/ses.c +2 -1
index dbfe12f63c98..7a9eb54e8808 100644
@@ -850,7 +850,8 @@ static void ses_intf_remove_enclosure(struct scsi_device *sdev)
 	kfree(ses_dev->page2);
 	kfree(ses_dev);
 
-	kfree(edev->component[0].scratch);
+	if (edev->components)
+		kfree(edev->component[0].scratch);
 
 	put_device(&edev->edev);
 	enclosure_unregister(edev);
Signature · 1 line
2.38.1
#5 lore raw
In reply to Tomas Henzl

Tomas,

Quoted 5 lines

First patch fixes a KASAN reported problem Second patch fixes other possible places in ses_enclosure_data_process where the max_desc_len might access memory out of bounds. 3/4 does the same for desc_ptr in ses_enclosure_data_process. The last patch fixes another KASAN report in ses_intf_remove.

Thanks for working on this! With your series applied, in combination with a straggling patch from James, I can finally boot my SAS test setup without any KASAN warnings.

Applied to 6.3/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#6 lore raw
Quoted 9 lines · Martin K. Petersen

Tomas,

Quoted 5 lines

First patch fixes a KASAN reported problem Second patch fixes other possible places in ses_enclosure_data_process where the max_desc_len might access memory out of bounds. 3/4 does the same for desc_ptr in ses_enclosure_data_process. The last patch fixes another KASAN report in ses_intf_remove.

Quoted 3 lines

Thanks for working on this! With your series applied, in combination with a straggling patch from James, I can finally boot my SAS test setup without any KASAN warnings.

I'm glad it worked for you and it could have been added since I've noticed some previous approaches rejected.

Quoted 3 lines

Applied to 6.3/scsi-staging, thanks!