Skip to contentSkip to conversation

cxl/mbox: Fix Payload Length check for Get Log command

PATCH Superseded Robert Richter 2 participants 4 messages Jan 4, 2023 – Jan 13, 2023 CXL

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cxl/mbox: Fix Payload Length check for Get Log command

Commit 2aeaf663b85e introduced strict checking for variable length payload size validation. The payload length of received data must match the size of the requested data by the caller except for the case where the min_out value is set.

The Get Log command does not have a header with a length field set. The Log size is determined by the Get Supported Logs command (CXL 3.0, 8.2.9.5.1). However, the actual size can be smaller and the number of valid bytes in the payload output must be determined reading the Payload Length field (CXL 3.0, Table 8-36, Note 2).

Two issues arise: The command can successfully complete with a payload length of zero. And, the valid payload length must then also be consumed by the caller.

Change cxl_xfer_log() to pass the number of payload bytes back to the caller to determine the number of log entries. Implement the payload handling as a special case where mbox_cmd->size_out is consulted when cxl_internal_send_cmd() returns -EIO. A WARN_ONCE() is added to check that -EIO is only returned in case of an unexpected output size.

Logs can be bigger than the maximum payload length and multiple Get Log commands can be issued. If the received payload size is smaller than the maximum payload size we can assume all valid bytes have been fetched. Stop sending further Get Log commands then.

On that occasion, change debug messages to also report the opcodes of supported commands.

The variable payload commands GET_LSA and SET_LSA could be also affected by this strict check, but SET_LSA cannot be broken because SET_LSA does not return an output payload, and GET_LSA never expects short reads.

Fixes 2aeaf663b85e ("cxl/mbox: Add variable output size validation for internal commands")
---
 drivers/cxl/core/mbox.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/cxl/core/mbox.c +20 -8
index b03fba212799..e93df0d39022 100644
@@ -170,6 +170,8 @@ int cxl_internal_send_cmd(struct cxl_dev_state *cxlds,
 	out_size = mbox_cmd->size_out;
 	min_out = mbox_cmd->min_out;
 	rc = cxlds->mbox_send(cxlds, mbox_cmd);
+	if (WARN_ONCE(rc == -EIO, "Bad return code: -EIO"))
+		return -ENXIO;
 	if (rc)
 		return rc;
 
@@ -576,6 +578,17 @@ static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8
 		};
 
 		rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
+
+		/*
+		 * The output payload length that indicates the number
+		 * of valid bytes can be smaller than the Log buffer
+		 * size.
+		 */
+		if (rc == -EIO && mbox_cmd.size_out < xfer_size) {
+			offset += mbox_cmd.size_out;
+			break;
+		}
+
 		if (rc < 0)
 			return rc;
 
@@ -584,7 +597,7 @@ static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8
 		offset += xfer_size;
 	}
 
-	return 0;
+	return offset;
 }
 
 /**
@@ -608,13 +621,11 @@ static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel)
 		u16 opcode = le16_to_cpu(cel_entry[i].opcode);
 		struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
 
-		if (!cmd) {
-			dev_dbg(cxlds->dev,
-				"Opcode 0x%04x unsupported by driver", opcode);
-			continue;
-		}
+		if (cmd)
+			set_bit(cmd->info.id, cxlds->enabled_cmds);
 
-		set_bit(cmd->info.id, cxlds->enabled_cmds);
+		dev_dbg(cxlds->dev, "Opcode 0x%04x %ssupported by driver",
+			opcode, cmd ? "" : "un");
 	}
 }
 
@@ -695,11 +706,12 @@ int cxl_enumerate_cmds(struct cxl_dev_state *cxlds)
 		}
 
 		rc = cxl_xfer_log(cxlds, &uuid, size, log);
-		if (rc) {
+		if (rc < 0) {
 			kvfree(log);
 			goto out;
 		}
 
+		size = (u32)rc;
 		cxl_walk_cel(cxlds, size, log);
 		kvfree(log);
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 29 lines · On Wed, 4 Jan 2023 21:29:54 +0100 Robert Richter <[email protected]> wrote:

Commit 2aeaf663b85e introduced strict checking for variable length payload size validation. The payload length of received data must match the size of the requested data by the caller except for the case where the min_out value is set.

The Get Log command does not have a header with a length field set. The Log size is determined by the Get Supported Logs command (CXL 3.0, 8.2.9.5.1). However, the actual size can be smaller and the number of valid bytes in the payload output must be determined reading the Payload Length field (CXL 3.0, Table 8-36, Note 2).

Two issues arise: The command can successfully complete with a payload length of zero. And, the valid payload length must then also be consumed by the caller.

Change cxl_xfer_log() to pass the number of payload bytes back to the caller to determine the number of log entries. Implement the payload handling as a special case where mbox_cmd->size_out is consulted when cxl_internal_send_cmd() returns -EIO. A WARN_ONCE() is added to check that -EIO is only returned in case of an unexpected output size.

Logs can be bigger than the maximum payload length and multiple Get Log commands can be issued. If the received payload size is smaller than the maximum payload size we can assume all valid bytes have been fetched. Stop sending further Get Log commands then.

On that occasion, change debug messages to also report the opcodes of supported commands.

Quoted 7 lines

The variable payload commands GET_LSA and SET_LSA could be also affected by this strict check, but SET_LSA cannot be broken because SET_LSA does not return an output payload, and GET_LSA never expects short reads.

Fixes 2aeaf663b85e ("cxl/mbox: Add variable output size validation for internal commands")

Hi Robert, a few comments inline.

Thanks,

Jonathan

drivers/cxl/core/mbox.c int cxl_internal_send_cmd(struct cxl_dev_state *cxlds, · line 173

Quoted 13 lines
---
 drivers/cxl/core/mbox.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/cxl/core/mbox.c +1 -0
index b03fba212799..e93df0d39022 100644
@@ -170,6 +170,8 @@ int cxl_internal_send_cmd(struct cxl_dev_state *cxlds,
 	out_size = mbox_cmd->size_out;
 	min_out = mbox_cmd->min_out;
 	rc = cxlds->mbox_send(cxlds, mbox_cmd);
+	if (WARN_ONCE(rc == -EIO, "Bad return code: -EIO"))

This is unusual. Why the WARN_ONCE? How can an error code be bad? It may well panic. Fine to have a dev_err() or similar but this seems excessive.

Quoted 36 lines
drivers/cxl/core/mbox.c +13 -2
+		return -ENXIO;
 	if (rc)
 		return rc;
 
@@ -576,6 +578,17 @@ static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8
 		};
 
 		rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
+
+		/*
+		 * The output payload length that indicates the number
+		 * of valid bytes can be smaller than the Log buffer
+		 * size.
+		 */
+		if (rc == -EIO && mbox_cmd.size_out < xfer_size) {
+			offset += mbox_cmd.size_out;
+			break;
+		}
+
 		if (rc < 0)
 			return rc;
 
@@ -584,7 +597,7 @@ static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8
 		offset += xfer_size;
 	}
 
-	return 0;
+	return offset;
 }
 
 /**
@@ -608,13 +621,11 @@ static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel)
 		u16 opcode = le16_to_cpu(cel_entry[i].opcode);
 		struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
 
-		if (!cmd) {

drivers/cxl/core/mbox.c static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel) · line 628

Quoted 10 lines
drivers/cxl/core/mbox.c +4 -5
-			dev_dbg(cxlds->dev,
-				"Opcode 0x%04x unsupported by driver", opcode);
-			continue;
-		}
+		if (cmd)
+			set_bit(cmd->info.id, cxlds->enabled_cmds);
 
-		set_bit(cmd->info.id, cxlds->enabled_cmds);
+		dev_dbg(cxlds->dev, "Opcode 0x%04x %ssupported by driver",
+			opcode, cmd ? "" : "un");

Unrelated change so doesn't belong in this patch. I'd also split the
dev_dbg into two paths both to reduce modification and because people might grep for "unsupported by driver"

drivers/cxl/core/mbox.c int cxl_enumerate_cmds(struct cxl_dev_state *cxlds) · line 709

Quoted 9 lines
drivers/cxl/core/mbox.c +1 -1
 	}
 }
 
@@ -695,11 +706,12 @@ int cxl_enumerate_cmds(struct cxl_dev_state *cxlds)
 		}
 
 		rc = cxl_xfer_log(cxlds, &uuid, size, log);
-		if (rc) {
+		if (rc < 0) {

Feels like passing in size as a pointer that is then updated might be cleaner.

Quoted 8 lines
drivers/cxl/core/mbox.c +1 -0
 			kvfree(log);
 			goto out;
 		}
 
+		size = (u32)rc;
 		cxl_walk_cel(cxlds, size, log);
 		kvfree(log);
 
#2 lore raw
Quoted 32 lines · Jonathan Cameron
Quoted 29 lines · On Wed, 4 Jan 2023 21:29:54 +0100 Robert Richter <[email protected]> wrote:

Commit 2aeaf663b85e introduced strict checking for variable length payload size validation. The payload length of received data must match the size of the requested data by the caller except for the case where the min_out value is set.

The Get Log command does not have a header with a length field set. The Log size is determined by the Get Supported Logs command (CXL 3.0, 8.2.9.5.1). However, the actual size can be smaller and the number of valid bytes in the payload output must be determined reading the Payload Length field (CXL 3.0, Table 8-36, Note 2).

Two issues arise: The command can successfully complete with a payload length of zero. And, the valid payload length must then also be consumed by the caller.

Change cxl_xfer_log() to pass the number of payload bytes back to the caller to determine the number of log entries. Implement the payload handling as a special case where mbox_cmd->size_out is consulted when cxl_internal_send_cmd() returns -EIO. A WARN_ONCE() is added to check that -EIO is only returned in case of an unexpected output size.

Logs can be bigger than the maximum payload length and multiple Get Log commands can be issued. If the received payload size is smaller than the maximum payload size we can assume all valid bytes have been fetched. Stop sending further Get Log commands then.

On that occasion, change debug messages to also report the opcodes of supported commands.

Quoted 8 lines
Quoted 7 lines

The variable payload commands GET_LSA and SET_LSA could be also affected by this strict check, but SET_LSA cannot be broken because SET_LSA does not return an output payload, and GET_LSA never expects short reads.

Fixes 2aeaf663b85e ("cxl/mbox: Add variable output size validation for internal commands")

Hi Robert, a few comments inline.

Oh, just found your comments here after replying to the other mail thread.

Quoted 9 lines

Thanks,

Jonathan

Quoted 4 lines
---
 drivers/cxl/core/mbox.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
Quoted 13 lines

drivers/cxl/core/mbox.c int cxl_internal_send_cmd(struct cxl_dev_state *cxlds, · line 173

Quoted 9 lines
drivers/cxl/core/mbox.c +1 -0
index b03fba212799..e93df0d39022 100644
@@ -170,6 +170,8 @@ int cxl_internal_send_cmd(struct cxl_dev_state *cxlds,
 	out_size = mbox_cmd->size_out;
 	min_out = mbox_cmd->min_out;
 	rc = cxlds->mbox_send(cxlds, mbox_cmd);
+	if (WARN_ONCE(rc == -EIO, "Bad return code: -EIO"))

This is unusual. Why the WARN_ONCE? How can an error code be bad? It may well panic. Fine to have a dev_err() or similar but this seems excessive.

This function should only return -EIO if the size is unexpected. The mbox_send() function doesn't have a size information and should never return with -EIO. I think a comment is need here?

Quoted 45 lines
Quoted 45 lines
drivers/cxl/core/mbox.c +15 -6
+		return -ENXIO;
 	if (rc)
 		return rc;
 
@@ -576,6 +578,17 @@ static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8
 		};
 
 		rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
+
+		/*
+		 * The output payload length that indicates the number
+		 * of valid bytes can be smaller than the Log buffer
+		 * size.
+		 */
+		if (rc == -EIO && mbox_cmd.size_out < xfer_size) {
+			offset += mbox_cmd.size_out;
+			break;
+		}
+
 		if (rc < 0)
 			return rc;
 
@@ -584,7 +597,7 @@ static int cxl_xfer_log(struct cxl_dev_state *cxlds, uuid_t *uuid, u32 size, u8
 		offset += xfer_size;
 	}
 
-	return 0;
+	return offset;
 }
 
 /**
@@ -608,13 +621,11 @@ static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel)
 		u16 opcode = le16_to_cpu(cel_entry[i].opcode);
 		struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
 
-		if (!cmd) {
-			dev_dbg(cxlds->dev,
-				"Opcode 0x%04x unsupported by driver", opcode);
-			continue;
-		}
+		if (cmd)
+			set_bit(cmd->info.id, cxlds->enabled_cmds);
 
Quoted 7 lines

drivers/cxl/core/mbox.c static void cxl_walk_cel(struct cxl_dev_state *cxlds, size_t size, u8 *cel) · line 628

Quoted 3 lines
drivers/cxl/core/mbox.c +2 -1
-		set_bit(cmd->info.id, cxlds->enabled_cmds);
+		dev_dbg(cxlds->dev, "Opcode 0x%04x %ssupported by driver",
+			opcode, cmd ? "" : "un");

Unrelated change so doesn't belong in this patch. I'd also split the
dev_dbg into two paths both to reduce modification and because people might grep for "unsupported by driver"

I will send a separate patch for this and also rework this section.

Quoted 5 lines
Quoted 5 lines
drivers/cxl/core/mbox.c +0 -0
 	}
 }
 
Quoted 8 lines

drivers/cxl/core/mbox.c int cxl_enumerate_cmds(struct cxl_dev_state *cxlds) · line 709

Quoted 6 lines
drivers/cxl/core/mbox.c +1 -1
@@ -695,11 +706,12 @@ int cxl_enumerate_cmds(struct cxl_dev_state *cxlds)
 		}
 
 		rc = cxl_xfer_log(cxlds, &uuid, size, log);
-		if (rc) {
+		if (rc < 0) {

Feels like passing in size as a pointer that is then updated might be cleaner.

I can do that.

Thanks,

-Robert

Quoted 10 lines
Quoted 9 lines
drivers/cxl/core/mbox.c +1 -0
 			kvfree(log);
 			goto out;
 		}
 
+		size = (u32)rc;
 		cxl_walk_cel(cxlds, size, log);
 		kvfree(log);
 
#3 lore raw
Quoted 68 lines · On Fri, 13 Jan 2023 14:40:04 +0100 Robert Richter <[email protected]> wrote:
Quoted 32 lines · Jonathan Cameron
Quoted 29 lines · On Wed, 4 Jan 2023 21:29:54 +0100 Robert Richter <[email protected]> wrote:

Commit 2aeaf663b85e introduced strict checking for variable length payload size validation. The payload length of received data must match the size of the requested data by the caller except for the case where the min_out value is set.

The Get Log command does not have a header with a length field set. The Log size is determined by the Get Supported Logs command (CXL 3.0, 8.2.9.5.1). However, the actual size can be smaller and the number of valid bytes in the payload output must be determined reading the Payload Length field (CXL 3.0, Table 8-36, Note 2).

Two issues arise: The command can successfully complete with a payload length of zero. And, the valid payload length must then also be consumed by the caller.

Change cxl_xfer_log() to pass the number of payload bytes back to the caller to determine the number of log entries. Implement the payload handling as a special case where mbox_cmd->size_out is consulted when cxl_internal_send_cmd() returns -EIO. A WARN_ONCE() is added to check that -EIO is only returned in case of an unexpected output size.

Logs can be bigger than the maximum payload length and multiple Get Log commands can be issued. If the received payload size is smaller than the maximum payload size we can assume all valid bytes have been fetched. Stop sending further Get Log commands then.

On that occasion, change debug messages to also report the opcodes of supported commands.

Quoted 8 lines
Quoted 7 lines

The variable payload commands GET_LSA and SET_LSA could be also affected by this strict check, but SET_LSA cannot be broken because SET_LSA does not return an output payload, and GET_LSA never expects short reads.

Fixes 2aeaf663b85e ("cxl/mbox: Add variable output size validation for internal commands")

Hi Robert, a few comments inline.

Oh, just found your comments here after replying to the other mail thread.

Quoted 9 lines

Thanks,

Jonathan

Quoted 4 lines
---
 drivers/cxl/core/mbox.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
Quoted 13 lines

drivers/cxl/core/mbox.c int cxl_internal_send_cmd(struct cxl_dev_state *cxlds, · line 173

Quoted 9 lines
drivers/cxl/core/mbox.c +1 -0
index b03fba212799..e93df0d39022 100644
@@ -170,6 +170,8 @@ int cxl_internal_send_cmd(struct cxl_dev_state *cxlds,
 	out_size = mbox_cmd->size_out;
 	min_out = mbox_cmd->min_out;
 	rc = cxlds->mbox_send(cxlds, mbox_cmd);
+	if (WARN_ONCE(rc == -EIO, "Bad return code: -EIO"))  

This is unusual. Why the WARN_ONCE? How can an error code be bad? It may well panic. Fine to have a dev_err() or similar but this seems excessive.

Quoted 3 lines

This function should only return -EIO if the size is unexpected. The mbox_send() function doesn't have a size information and should never return with -EIO. I think a comment is need here?

Makes sense. A comment so we don't forget the reasoning would be great.

Thanks,

Jonathan