Skip to contentSkip to conversation

fsi: occ: Add check for OCC response checksum

PATCH Superseded Eddie James 3 participants 5 messages Apr 30, 2018 – May 1, 2018 FSI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fsi: occ: Add check for OCC response checksum

The OCC specification indicates that it is an error scenario if the response checksum doesn't match the sum of the bytes of the response. The driver needs to perform this calculation and check, and return an error if it's a mismatch.

---
 drivers/fsi/fsi-occ.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/fsi/fsi-occ.c +23 -0
index 45ae13c..16f18df 100644
@@ -427,6 +427,27 @@ static int occ_release(struct inode *inode, struct file *file)
 	.release = occ_release,
 };
 
+static int occ_check_sum(struct occ_response *resp, u16 data_length)
+{
+	unsigned int i;
+	u16 checksum = 0;
+	u16 checksum_resp = get_unaligned_be16(&resp->data[data_length]);
+
+	checksum += resp->seq_no;
+	checksum += resp->cmd_type;
+	checksum += resp->return_status;
+	checksum += (data_length >> 8) + (data_length & 0xFF);
+
+	for (i = 0; i < data_length; ++i) {
+		checksum += resp->data[i];
+	}
+
+	if (checksum != checksum_resp)
+		return -EBADMSG;
+
+	return 0;
+}
+
 static int occ_write_sbefifo(struct sbefifo_client *client, const char *buf,
 			     ssize_t len)
 {
@@ -692,6 +713,8 @@ static void occ_worker(struct work_struct *work)
 
 	xfr->resp_data_length = resp_data_length + 7;
 
+	rc = occ_check_sum(resp, resp_data_length);
+
 done:
 	mutex_unlock(&occ->occ_lock);
 
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 15 lines · Eddie James

The OCC specification indicates that it is an error scenario if the response checksum doesn't match the sum of the bytes of the response. The driver needs to perform this calculation and check, and return an error if it's a mismatch.

---
 drivers/fsi/fsi-occ.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/fsi/fsi-occ.c +0 -0
index 45ae13c..16f18df 100644

drivers/fsi/fsi-occ.c static int occ_release(struct inode *inode, struct · line 431

Quoted 5 lines
drivers/fsi/fsi-occ.c +1 -0
@@ -427,6 +427,27 @@ static int occ_release(struct inode *inode, struct 
 file *file)
 	.release = occ_release,
 };
 
+static int occ_check_sum(struct occ_response *resp, u16 data_length)

Why pass in data_length when it's been extracted from resp?

drivers/fsi/fsi-occ.c static int occ_release(struct inode *inode, struct file *file) · line 434

Quoted 4 lines
drivers/fsi/fsi-occ.c +4 -0
+{
+	unsigned int i;
+	u16 checksum = 0;
+	u16 checksum_resp = get_unaligned_be16(&resp->data[data_length]);

This looks like the definition of struct occ_response is broken:

struct occ_response {
	u8 seq_no;
	u8 cmd_type;
	u8 return_status;
	__be16 data_length;
	u8 data[OCC_RESP_DATA_BYTES];
	__be16 checksum;
} __packed;

The checksum member is misleading, as we're not always transferring 4089 bytes of data, but it is necessary to "avoid" an out-of-bounds memory access if we do. I think it's worth redefining data as u8 data[OCC_RESP_DATA_BYTES + 2] and dropping the checksum member.

Also can you add a comment explaining why you're doing an "out of bounds" access through the data member by indexing with data_length?

Quoted 20 lines
drivers/fsi/fsi-occ.c +16 -0
+
+	checksum += resp->seq_no;
+	checksum += resp->cmd_type;
+	checksum += resp->return_status;
+	checksum += (data_length >> 8) + (data_length & 0xFF);
+
+	for (i = 0; i < data_length; ++i) {
+		checksum += resp->data[i];
+	}
+
+	if (checksum != checksum_resp)
+		return -EBADMSG;
+
+	return 0;
+}
+
 static int occ_write_sbefifo(struct sbefifo_client *client, const char *buf,
 			     ssize_t len)

{

drivers/fsi/fsi-occ.c static void occ_worker(struct work_struct *work) · line 717

Quoted 5 lines
drivers/fsi/fsi-occ.c +2 -0
@@ -692,6 +713,8 @@ static void occ_worker(struct work_struct *work)
 
 	xfr->resp_data_length = resp_data_length + 7;
 
+	rc = occ_check_sum(resp, resp_data_length);
+

It's a pity there aren't two checksums: one for the fixed length data (covering the variable data length value) and one for the variable length data. We're pretty much reduced to inferring that the data length is fine if it doesn't exceed OCC_RESP_DATA_BYTES, but that doesn't mean that it isn't corrupt.

Cheers,

Andrew

Quoted 6 lines
drivers/fsi/fsi-occ.c +0 -0
 done:
 	mutex_unlock(&occ->occ_lock);
 
Signature · 1 line
1.8.3.1
#2 lore raw
Quoted 21 lines · Andrew Jeffery
Quoted 19 lines · Eddie James

The OCC specification indicates that it is an error scenario if the response checksum doesn't match the sum of the bytes of the response. The driver needs to perform this calculation and check, and return an error if it's a mismatch.

---
  drivers/fsi/fsi-occ.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
drivers/fsi/fsi-occ.c +0 -0
index 45ae13c..16f18df 100644
@@ -427,6 +427,27 @@ static int occ_release(struct inode *inode, struct
 file *file)
  	.release = occ_release,
  };
 
Quoted 2 lines

drivers/fsi/fsi-occ.c static int occ_release(struct inode *inode, struct file *file) · line 430

Quoted 1 line
drivers/fsi/fsi-occ.c +1 -0
+static int occ_check_sum(struct occ_response *resp, u16 data_length)

Why pass in data_length when it's been extracted from resp?

So that I don't have to convert it from BE again.

Quoted 16 lines

drivers/fsi/fsi-occ.c static int occ_release(struct inode *inode, struct file *file) · line 434

Quoted 5 lines
drivers/fsi/fsi-occ.c +4 -0
+{
+	unsigned int i;
+	u16 checksum = 0;
+	u16 checksum_resp = get_unaligned_be16(&resp->data[data_length]);

This looks like the definition of struct occ_response is broken:

struct occ_response {
	u8 seq_no;
	u8 cmd_type;
	u8 return_status;
	__be16 data_length;
	u8 data[OCC_RESP_DATA_BYTES];
	__be16 checksum;
} __packed;
Quoted 3 lines

The checksum member is misleading, as we're not always transferring 4089 bytes of data, but it is necessary to "avoid" an out-of-bounds memory access if we do. I think it's worth redefining data as u8 data[OCC_RESP_DATA_BYTES + 2] and dropping the checksum member.

Also can you add a comment explaining why you're doing an "out of bounds" access through the data member by indexing with data_length?

 Good ideas.

Quoted 22 lines
Quoted 22 lines
drivers/fsi/fsi-occ.c +16 -0
+
+	checksum += resp->seq_no;
+	checksum += resp->cmd_type;
+	checksum += resp->return_status;
+	checksum += (data_length >> 8) + (data_length & 0xFF);
+
+	for (i = 0; i < data_length; ++i) {
+		checksum += resp->data[i];
+	}
+
+	if (checksum != checksum_resp)
+		return -EBADMSG;
+
+	return 0;
+}
+
  static int occ_write_sbefifo(struct sbefifo_client *client, const char *buf,
  			     ssize_t len)
  {
drivers/fsi/fsi-occ.c +0 -0
@@ -692,6 +713,8 @@ static void occ_worker(struct work_struct *work)
 
Quoted 5 lines

drivers/fsi/fsi-occ.c static void occ_worker(struct work_struct *work) · line 717

Quoted 4 lines
drivers/fsi/fsi-occ.c +2 -0
  	xfr->resp_data_length = resp_data_length + 7;
 
+	rc = occ_check_sum(resp, resp_data_length);
+

It's a pity there aren't two checksums: one for the fixed length data (covering the variable data length value) and one for the variable length data. We're pretty much reduced to inferring that the data length is fine if it doesn't exceed OCC_RESP_DATA_BYTES, but that doesn't mean that it isn't corrupt.

Well it still works; if the data length is incorrect but less than the max, then the checksum won't match.

Thanks,
Eddie

Quoted 11 lines

Cheers,

Andrew

Quoted 6 lines
drivers/fsi/fsi-occ.c +0 -0
  done:
  	mutex_unlock(&occ->occ_lock);
 
Signature · 1 line
1.8.3.1
#3 lore raw
Quoted 73 lines · Eddie James
Quoted 21 lines · Andrew Jeffery
Quoted 19 lines · Eddie James

The OCC specification indicates that it is an error scenario if the response checksum doesn't match the sum of the bytes of the response. The driver needs to perform this calculation and check, and return an error if it's a mismatch.

---
  drivers/fsi/fsi-occ.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
drivers/fsi/fsi-occ.c +0 -0
index 45ae13c..16f18df 100644
@@ -427,6 +427,27 @@ static int occ_release(struct inode *inode, struct
 file *file)
  	.release = occ_release,
  };
 
Quoted 2 lines

drivers/fsi/fsi-occ.c static int occ_release(struct inode *inode, struct file *file) · line 430

Quoted 1 line
drivers/fsi/fsi-occ.c +1 -0
+static int occ_check_sum(struct occ_response *resp, u16 data_length)

Why pass in data_length when it's been extracted from resp?

So that I don't have to convert it from BE again.

Quoted 16 lines

drivers/fsi/fsi-occ.c static int occ_release(struct inode *inode, struct file *file) · line 434

Quoted 5 lines
drivers/fsi/fsi-occ.c +4 -0
+{
+	unsigned int i;
+	u16 checksum = 0;
+	u16 checksum_resp = get_unaligned_be16(&resp->data[data_length]);

This looks like the definition of struct occ_response is broken:

struct occ_response {
	u8 seq_no;
	u8 cmd_type;
	u8 return_status;
	__be16 data_length;
	u8 data[OCC_RESP_DATA_BYTES];
	__be16 checksum;
} __packed;
Quoted 3 lines

The checksum member is misleading, as we're not always transferring 4089 bytes of data, but it is necessary to "avoid" an out-of-bounds memory access if we do. I think it's worth redefining data as u8 data[OCC_RESP_DATA_BYTES + 2] and dropping the checksum member.

Also can you add a comment explaining why you're doing an "out of bounds" access through the data member by indexing with data_length?

 Good ideas.

Quoted 22 lines
Quoted 22 lines
drivers/fsi/fsi-occ.c +16 -0
+
+	checksum += resp->seq_no;
+	checksum += resp->cmd_type;
+	checksum += resp->return_status;
+	checksum += (data_length >> 8) + (data_length & 0xFF);
+
+	for (i = 0; i < data_length; ++i) {
+		checksum += resp->data[i];
+	}
+
+	if (checksum != checksum_resp)
+		return -EBADMSG;
+
+	return 0;
+}
+
  static int occ_write_sbefifo(struct sbefifo_client *client, const char *buf,
  			     ssize_t len)
  {
drivers/fsi/fsi-occ.c +0 -0
@@ -692,6 +713,8 @@ static void occ_worker(struct work_struct *work)
 
Quoted 8 lines
Quoted 5 lines

drivers/fsi/fsi-occ.c static void occ_worker(struct work_struct *work) · line 717

Quoted 4 lines
drivers/fsi/fsi-occ.c +2 -0
  	xfr->resp_data_length = resp_data_length + 7;
 
+	rc = occ_check_sum(resp, resp_data_length);
+

It's a pity there aren't two checksums: one for the fixed length data (covering the variable data length value) and one for the variable length data. We're pretty much reduced to inferring that the data length is fine if it doesn't exceed OCC_RESP_DATA_BYTES, but that doesn't mean that it isn't corrupt.

Well it still works; if the data length is incorrect but less than the max, then the checksum won't match.

Maybe: you're defining where your checksum lives in terms of the length, which is covered by the checksum but only if you've found the correct one, which you might not have if the length is corrupt. But it's pretty unlikely that it would pass, and a checksum is probabilistic anyway.

Quoted 16 lines

Thanks,
Eddie

Quoted 11 lines

Cheers,

Andrew

Quoted 6 lines
drivers/fsi/fsi-occ.c +0 -0
  done:
  	mutex_unlock(&occ->occ_lock);
 
Signature · 1 line
1.8.3.1
#4 lore raw
In reply to Eddie James
Quoted 15 lines · Eddie James

The OCC specification indicates that it is an error scenario if the response checksum doesn't match the sum of the bytes of the response. The driver needs to perform this calculation and check, and return an error if it's a mismatch.

---
 drivers/fsi/fsi-occ.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/fsi/fsi-occ.c +0 -0
index 45ae13c..16f18df 100644

drivers/fsi/fsi-occ.c static int occ_release(struct inode *inode, struct file *file) · line 430

Quoted 4 lines
drivers/fsi/fsi-occ.c +1 -0
@@ -427,6 +427,27 @@ static int occ_release(struct inode *inode, struct file *file)
 	.release = occ_release,
 };
 
+static int occ_check_sum(struct occ_response *resp, u16 data_length)

Minor nitpick: It looks like 'checksum' is the standard rather than 'check_sum':

[stewart@birb linux]$ ack check_sum|wc -l
133
[stewart@birb linux]$ ack checksum|wc -l
7284

Would a more accurate name be occ_resp_verify_checksum() ? By the name 'occ_check_sum' alone, I'd assume that it was returning the calculated checksum rather than verifying it.

Signature · 2 lines
Stewart Smith
OPAL Architect, IBM.