Skip to contentSkip to conversation

libmpathpersist allocation size fixes

PATCH Superseded [email protected] 2 participants 12 messages Mar 6, 2020 – Mar 15, 2020 Device mapper

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover libmpathpersist allocation size fixes

From: Martin Wilck <[email protected]>

Hi Christophe,

We recently found a problem with mpathpersist using an allocation length of > 8192 bytes for PERSISTENT RESERVE IN/OUT commands. This patch set tries to fix it. The last patch would change the ABI for external programs, it's optional.

(Christophe, to avoid misunderstanding: This set is not meant to be included
in the upcoming merge).

Reviews and comments welcome.

Regards,
Martin

Martin Wilck (4):
libmpathpersist: limit PRIN allocation length to 8192 bytes
libmpathpersist: format_transportids(): avoid PROUT overflow
libmpathpersist: mpath_format_readfullstatus(): use real buffer size
libmpathpersist: ABI change: limit data-in/out size to 8192 bytes

 libmpathpersist/mpath_persist.h  | 15 ++++++++----
 libmpathpersist/mpath_pr_ioctl.c | 40 +++++++++++++++++++++++++++-----
 2 files changed, 45 insertions(+), 10 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 libmpathpersist: limit PRIN allocation length to 8192 bytes

From: Martin Wilck <[email protected]>

Some targets (notably the qemu-pr-helper) don't support PERSISTENT RESERVE IN commands with more than 8192 bytes allocation length. While I have found no explicit requirement in the SCSI specs that the allocation lengh may not exceed 8k, an 8k limit is also enforced by sg_persist(8), and actually by mpathpersist itself for the --allocation-length option, but not for the auto-determined length.

Fix that.

---
 libmpathpersist/mpath_pr_ioctl.c | 2 ++
 1 file changed, 2 insertions(+)
libmpathpersist/mpath_pr_ioctl.c +2 -0
index 74b26b0c..1a28cba7 100644
@@ -543,5 +543,7 @@ int get_prin_length(int rq_servact)
 			mx_resp_len = 0;
 			break;
 	}
+	if (mx_resp_len > MPATH_MAX_PARAM_LEN)
+		mx_resp_len = MPATH_MAX_PARAM_LEN;
 	return mx_resp_len;
 }
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] libmpathpersist: limit PRIN allocation length to 8192 bytes

Quoted 11 lines · On Sat, Mar 07, 2020 at 12:06:02AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Some targets (notably the qemu-pr-helper) don't support PERSISTENT RESERVE IN commands with more than 8192 bytes allocation length. While I have found no explicit requirement in the SCSI specs that the allocation lengh may not exceed 8k, an 8k limit is also enforced by sg_persist(8), and actually by mpathpersist itself for the --allocation-length option, but not for the auto-determined length.

Fix that.

Quoted 19 lines
---
 libmpathpersist/mpath_pr_ioctl.c | 2 ++
 1 file changed, 2 insertions(+)
libmpathpersist/mpath_pr_ioctl.c +2 -0
index 74b26b0c..1a28cba7 100644
@@ -543,5 +543,7 @@ int get_prin_length(int rq_servact)
 			mx_resp_len = 0;
 			break;
 	}
+	if (mx_resp_len > MPATH_MAX_PARAM_LEN)
+		mx_resp_len = MPATH_MAX_PARAM_LEN;
 	return mx_resp_len;
 }
Signature · 1 line
2.25.1
#3 lore raw

PATCH 2/4 libmpathpersist: format_transportids(): avoid PROUT overflow

In reply to [email protected]

From: Martin Wilck <[email protected]>

This limits the PERSISTENT RESERVE OUT data size to max. 8192 bytes.

---
 libmpathpersist/mpath_pr_ioctl.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
libmpathpersist/mpath_pr_ioctl.c +24 -2
index 1a28cba7..8bf16c0d 100644
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <stddef.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -138,38 +139,59 @@ retry :
 	return status;
 }
 
+#define check_overflow(ofs, n, start, label)				\
+	do {								\
+		if ((ofs) + 4 + (n) +					\
+		    offsetof(struct prout_param_descriptor, private_buffer) \
+		    > MPATH_MAX_PARAM_LEN)				\
+		{							\
+			(ofs) = (start);				\
+			goto label;					\
+		}							\
+	} while(0)
+
 uint32_t  format_transportids(struct prout_param_descriptor *paramp)
 {
 	unsigned int i = 0, len;
 	uint32_t buff_offset = 4;
-	memset(paramp->private_buffer, 0, MPATH_MAX_PARAM_LEN);
+	memset(paramp->private_buffer, 0, sizeof(paramp->private_buffer));
 	for (i=0; i < paramp->num_transportid; i++ )
 	{
+		uint32_t start_offset = buff_offset;
+
+		check_overflow(buff_offset, 1, start_offset, end_loop);
 		paramp->private_buffer[buff_offset] = (uint8_t)((paramp->trnptid_list[i]->format_code & 0xff)|
 							(paramp->trnptid_list[i]->protocol_id & 0xff));
 		buff_offset += 1;
 		switch(paramp->trnptid_list[i]->protocol_id)
 		{
 			case MPATH_PROTOCOL_ID_FC:
+				check_overflow(buff_offset, 7 + 8 + 8,
+					       start_offset, end_loop);
 				buff_offset += 7;
 				memcpy(&paramp->private_buffer[buff_offset], &paramp->trnptid_list[i]->n_port_name, 8);
 				buff_offset +=8 ;
 				buff_offset +=8 ;
 				break;
 			case MPATH_PROTOCOL_ID_SAS:
+				check_overflow(buff_offset, 3 + 12,
+					       start_offset, end_loop);
 				buff_offset += 3;
 				memcpy(&paramp->private_buffer[buff_offset], &paramp->trnptid_list[i]->sas_address, 8);
 				buff_offset += 12;
 				break;
 			case MPATH_PROTOCOL_ID_ISCSI:
-				buff_offset += 1;
 				len = (paramp->trnptid_list[i]->iscsi_name[1] & 0xff)+2;
+				check_overflow(buff_offset, 1 + len,
+					       start_offset, end_loop);
+				buff_offset += 1;
 				memcpy(&paramp->private_buffer[buff_offset], &paramp->trnptid_list[i]->iscsi_name,len);
 				buff_offset += len ;
 				break;
 		}
 
 	}
+end_loop:
 	buff_offset -= 4;
 	paramp->private_buffer[0] = (unsigned char)((buff_offset >> 24) & 0xff);
 	paramp->private_buffer[1] = (unsigned char)((buff_offset >> 16) & 0xff);
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH 2/4] libmpathpersist: format_transportids(): avoid PROUT overflow

Quoted 21 lines · On Sat, Mar 07, 2020 at 12:06:03AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

This limits the PERSISTENT RESERVE OUT data size to max. 8192 bytes.

---
 libmpathpersist/mpath_pr_ioctl.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
libmpathpersist/mpath_pr_ioctl.c +1 -0
index 1a28cba7..8bf16c0d 100644
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <stddef.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>

libmpathpersist/mpath_pr_ioctl.c retry : · line 144

Quoted 6 lines
libmpathpersist/mpath_pr_ioctl.c +3 -0
@@ -138,38 +139,59 @@ retry :
 	return status;
 }
 
+#define check_overflow(ofs, n, start, label)				\
+	do {								\
+		if ((ofs) + 4 + (n) +					\

I don't get where the 4 comes from here, since buff_offset starts at 4. Maybe I'm just missing something.

-Ben

Quoted 57 lines
libmpathpersist/mpath_pr_ioctl.c +20 -2
+		    offsetof(struct prout_param_descriptor, private_buffer) \
+		    > MPATH_MAX_PARAM_LEN)				\
+		{							\
+			(ofs) = (start);				\
+			goto label;					\
+		}							\
+	} while(0)
+
 uint32_t  format_transportids(struct prout_param_descriptor *paramp)
 {
 	unsigned int i = 0, len;
 	uint32_t buff_offset = 4;
-	memset(paramp->private_buffer, 0, MPATH_MAX_PARAM_LEN);
+	memset(paramp->private_buffer, 0, sizeof(paramp->private_buffer));
 	for (i=0; i < paramp->num_transportid; i++ )
 	{
+		uint32_t start_offset = buff_offset;
+
+		check_overflow(buff_offset, 1, start_offset, end_loop);
 		paramp->private_buffer[buff_offset] = (uint8_t)((paramp->trnptid_list[i]->format_code & 0xff)|
 							(paramp->trnptid_list[i]->protocol_id & 0xff));
 		buff_offset += 1;
 		switch(paramp->trnptid_list[i]->protocol_id)
 		{
 			case MPATH_PROTOCOL_ID_FC:
+				check_overflow(buff_offset, 7 + 8 + 8,
+					       start_offset, end_loop);
 				buff_offset += 7;
 				memcpy(&paramp->private_buffer[buff_offset], &paramp->trnptid_list[i]->n_port_name, 8);
 				buff_offset +=8 ;
 				buff_offset +=8 ;
 				break;
 			case MPATH_PROTOCOL_ID_SAS:
+				check_overflow(buff_offset, 3 + 12,
+					       start_offset, end_loop);
 				buff_offset += 3;
 				memcpy(&paramp->private_buffer[buff_offset], &paramp->trnptid_list[i]->sas_address, 8);
 				buff_offset += 12;
 				break;
 			case MPATH_PROTOCOL_ID_ISCSI:
-				buff_offset += 1;
 				len = (paramp->trnptid_list[i]->iscsi_name[1] & 0xff)+2;
+				check_overflow(buff_offset, 1 + len,
+					       start_offset, end_loop);
+				buff_offset += 1;
 				memcpy(&paramp->private_buffer[buff_offset], &paramp->trnptid_list[i]->iscsi_name,len);
 				buff_offset += len ;
 				break;
 		}
 
 	}
+end_loop:
 	buff_offset -= 4;
 	paramp->private_buffer[0] = (unsigned char)((buff_offset >> 24) & 0xff);
 	paramp->private_buffer[1] = (unsigned char)((buff_offset >> 16) & 0xff);
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH 2/4] libmpathpersist: format_transportids(): avoid PROUT overflow

Quoted 27 lines · Benjamin Marzinski
Quoted 26 lines · On Sat, Mar 07, 2020 at 12:06:03AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

This limits the PERSISTENT RESERVE OUT data size to max. 8192 bytes.

---
 libmpathpersist/mpath_pr_ioctl.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
libmpathpersist/mpath_pr_ioctl.c +1 -0
index 1a28cba7..8bf16c0d 100644
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <stddef.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -138,38 +139,59 @@ retry :
 	return status;
 }
 
Quoted 10 lines

libmpathpersist/mpath_pr_ioctl.c retry : · line 147

Quoted 6 lines
libmpathpersist/mpath_pr_ioctl.c +3 -0
+#define check_overflow(ofs, n, start, label)			
 	\
+	do {								
 \
+		if ((ofs) + 4 + (n) +					
 \

I don't get where the 4 comes from here, since buff_offset starts at 4. Maybe I'm just missing something.

No. It's wrong, buff_offset includes these 4 bytes inded. Thanks for spotting it. I'll resubmit the series.

Regards,
Martin

#6 lore raw

PATCH 3/4 libmpathpersist: mpath_format_readfullstatus(): use real buffer size

In reply to [email protected]

From: Martin Wilck <[email protected]>

This changes no semantics, but it will allow changing the size of prin_readfd.private_buffer in a follow-up patch.

---
 libmpathpersist/mpath_pr_ioctl.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
libmpathpersist/mpath_pr_ioctl.c +8 -4
index 8bf16c0d..8d8ca76f 100644
@@ -233,6 +233,8 @@ static void mpath_format_readfullstatus(struct prin_resp *pr_buff)
 	uint32_t additional_length, k, tid_len_len = 0;
 	char tempbuff[MPATH_MAX_PARAM_LEN];
 	struct prin_fulldescr fdesc;
+	static const int pbuf_size =
+		sizeof(pr_buff->prin_descriptor.prin_readfd.private_buffer);
 
 	convert_be32_to_cpu(&pr_buff->prin_descriptor.prin_readfd.prgeneration);
 	convert_be32_to_cpu(&pr_buff->prin_descriptor.prin_readfd.number_of_descriptor);
@@ -244,16 +246,18 @@ static void mpath_format_readfullstatus(struct prin_resp *pr_buff)
 	}
 
 	additional_length = pr_buff->prin_descriptor.prin_readfd.number_of_descriptor;
-	if (additional_length > MPATH_MAX_PARAM_LEN) {
+	if (additional_length > pbuf_size) {
 		condlog(3, "PRIN length %u exceeds max length %d", additional_length,
-			MPATH_MAX_PARAM_LEN);
+			pbuf_size);
 		return;
 	}
 
 	memset(&fdesc, 0, sizeof(struct prin_fulldescr));
 
-	memcpy( tempbuff, pr_buff->prin_descriptor.prin_readfd.private_buffer,MPATH_MAX_PARAM_LEN );
-	memset(&pr_buff->prin_descriptor.prin_readfd.private_buffer, 0, MPATH_MAX_PARAM_LEN);
+	memcpy( tempbuff, pr_buff->prin_descriptor.prin_readfd.private_buffer,
+		pbuf_size);
+	memset(&pr_buff->prin_descriptor.prin_readfd.private_buffer, 0,
+	       pbuf_size);
 
 	p =(unsigned char *)tempbuff;
 	ppbuff = (char *)pr_buff->prin_descriptor.prin_readfd.private_buffer;
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 3/4
#7 lore raw

Re: [PATCH 3/4] libmpathpersist: mpath_format_readfullstatus(): use real buffer size

Quoted 5 lines · On Sat, Mar 07, 2020 at 12:06:04AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

This changes no semantics, but it will allow changing the size of prin_readfd.private_buffer in a follow-up patch.

Quoted 43 lines
---
 libmpathpersist/mpath_pr_ioctl.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
libmpathpersist/mpath_pr_ioctl.c +8 -4
index 8bf16c0d..8d8ca76f 100644
@@ -233,6 +233,8 @@ static void mpath_format_readfullstatus(struct prin_resp *pr_buff)
 	uint32_t additional_length, k, tid_len_len = 0;
 	char tempbuff[MPATH_MAX_PARAM_LEN];
 	struct prin_fulldescr fdesc;
+	static const int pbuf_size =
+		sizeof(pr_buff->prin_descriptor.prin_readfd.private_buffer);
 
 	convert_be32_to_cpu(&pr_buff->prin_descriptor.prin_readfd.prgeneration);
 	convert_be32_to_cpu(&pr_buff->prin_descriptor.prin_readfd.number_of_descriptor);
@@ -244,16 +246,18 @@ static void mpath_format_readfullstatus(struct prin_resp *pr_buff)
 	}
 
 	additional_length = pr_buff->prin_descriptor.prin_readfd.number_of_descriptor;
-	if (additional_length > MPATH_MAX_PARAM_LEN) {
+	if (additional_length > pbuf_size) {
 		condlog(3, "PRIN length %u exceeds max length %d", additional_length,
-			MPATH_MAX_PARAM_LEN);
+			pbuf_size);
 		return;
 	}
 
 	memset(&fdesc, 0, sizeof(struct prin_fulldescr));
 
-	memcpy( tempbuff, pr_buff->prin_descriptor.prin_readfd.private_buffer,MPATH_MAX_PARAM_LEN );
-	memset(&pr_buff->prin_descriptor.prin_readfd.private_buffer, 0, MPATH_MAX_PARAM_LEN);
+	memcpy( tempbuff, pr_buff->prin_descriptor.prin_readfd.private_buffer,
+		pbuf_size);
+	memset(&pr_buff->prin_descriptor.prin_readfd.private_buffer, 0,
+	       pbuf_size);
 
 	p =(unsigned char *)tempbuff;
 	ppbuff = (char *)pr_buff->prin_descriptor.prin_readfd.private_buffer;
Signature · 1 line
2.25.1
#8 lore raw

PATCH 4/4 libmpathpersist: ABI change: limit data-in/out size to 8192 bytes

In reply to [email protected]

From: Martin Wilck <[email protected]>

Make sure that data structures used for PERSISTENT RESERVE IN/OUT fit into 8k buffers.

This patch breaks the libmpathpersist ABI.

---
 libmpathpersist/mpath_persist.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
libmpathpersist/mpath_persist.h +11 -4
index 7cf4faf9..bdf4069d 100644
@@ -92,7 +92,7 @@ struct prin_readdescr
 	uint32_t prgeneration;
 	uint32_t additional_length;	/* The value should be either 0 or divisible by 8.
 					   0 indicates no registered reservation key. */
-	uint8_t	 key_list[MPATH_MAX_PARAM_LEN];
+	uint8_t	 key_list[MPATH_MAX_PARAM_LEN - 2 * sizeof(uint32_t)];
 };
 
 struct prin_resvdescr
@@ -141,7 +141,9 @@ struct print_fulldescr_list
 {
 	uint32_t prgeneration;
 	uint32_t number_of_descriptor;
-	uint8_t private_buffer[MPATH_MAX_PARAM_LEN]; /*Private buffer for list storage*/
+	/* Data-in; Private buffer for list storage */
+	uint8_t private_buffer[MPATH_MAX_PARAM_LEN - 2 * sizeof(uint32_t)];
+	/* array of pointers into private_buffer */
 	struct prin_fulldescr *descriptors[];
 };
 
@@ -163,8 +165,13 @@ struct prout_param_descriptor {		/* PROUT parameter descriptor */
 	uint8_t	 sa_flags;
 	uint8_t _reserved;
 	uint16_t _obsolete1;
-	uint8_t  private_buffer[MPATH_MAX_PARAM_LEN]; /*private buffer for list storage*/
-	uint32_t num_transportid;	/* Number of Transport ID listed in trnptid_list[]*/
+	/*private buffer for list storage; data-out */
+	/* 24: offsetof (struct prout_param_descriptor, private_buffer) */
+	uint8_t  private_buffer[MPATH_MAX_PARAM_LEN - 24];
+	/* Internal use below here */
+	/* Number of Transport ID listed in trnptid_list[] */
+	uint32_t num_transportid;
+	/* pointers into private_buffer */
 	struct transportid *trnptid_list[];
 };
 
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 4/4
#9 lore raw

Re: [PATCH 4/4] libmpathpersist: ABI change: limit data-in/out size to 8192 bytes

Quoted 7 lines · On Sat, Mar 07, 2020 at 12:06:05AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Make sure that data structures used for PERSISTENT RESERVE IN/OUT fit into 8k buffers.

This patch breaks the libmpathpersist ABI.

I'm not super worried about this. I don't really see a way for users to hurt themselves because of this change, without making some pretty odd assumptions. Am I missing something?

Quoted 47 lines
---
 libmpathpersist/mpath_persist.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
libmpathpersist/mpath_persist.h +11 -4
index 7cf4faf9..bdf4069d 100644
@@ -92,7 +92,7 @@ struct prin_readdescr
 	uint32_t prgeneration;
 	uint32_t additional_length;	/* The value should be either 0 or divisible by 8.
 					   0 indicates no registered reservation key. */
-	uint8_t	 key_list[MPATH_MAX_PARAM_LEN];
+	uint8_t	 key_list[MPATH_MAX_PARAM_LEN - 2 * sizeof(uint32_t)];
 };
 
 struct prin_resvdescr
@@ -141,7 +141,9 @@ struct print_fulldescr_list
 {
 	uint32_t prgeneration;
 	uint32_t number_of_descriptor;
-	uint8_t private_buffer[MPATH_MAX_PARAM_LEN]; /*Private buffer for list storage*/
+	/* Data-in; Private buffer for list storage */
+	uint8_t private_buffer[MPATH_MAX_PARAM_LEN - 2 * sizeof(uint32_t)];
+	/* array of pointers into private_buffer */
 	struct prin_fulldescr *descriptors[];
 };
 
@@ -163,8 +165,13 @@ struct prout_param_descriptor {		/* PROUT parameter descriptor */
 	uint8_t	 sa_flags;
 	uint8_t _reserved;
 	uint16_t _obsolete1;
-	uint8_t  private_buffer[MPATH_MAX_PARAM_LEN]; /*private buffer for list storage*/
-	uint32_t num_transportid;	/* Number of Transport ID listed in trnptid_list[]*/
+	/*private buffer for list storage; data-out */
+	/* 24: offsetof (struct prout_param_descriptor, private_buffer) */
+	uint8_t  private_buffer[MPATH_MAX_PARAM_LEN - 24];
+	/* Internal use below here */
+	/* Number of Transport ID listed in trnptid_list[] */
+	uint32_t num_transportid;
+	/* pointers into private_buffer */
 	struct transportid *trnptid_list[];
 };
 
Signature · 1 line
2.25.1
#10 lore raw

Re: [PATCH 4/4] libmpathpersist: ABI change: limit data-in/out size to 8192 bytes

Quoted 6 lines · Benjamin Marzinski
Quoted 5 lines · On Sat, Mar 07, 2020 at 12:06:05AM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Make sure that data structures used for PERSISTENT RESERVE IN/OUT fit into 8k buffers.

Quoted 8 lines
Quoted 2 lines

This patch breaks the libmpathpersist ABI.

I'm not super worried about this. I don't really see a way for users to
hurt themselves because of this change, without making some pretty odd
assumptions. Am I missing something?

I found this:

struct prout_param_descriptor {	
	uint8_t	 key[8];
	uint8_t	 sa_key[8];
[...]
	uint8_t  private_buffer[MPATH_MAX_PARAM_LEN - 24];
	uint32_t num_transportid;
	struct transportid *trnptid_list[];
};

A program written against the old API might assume that the length of private_buffer was MPATH_MAX_PARAM_LEN, and call e.g. memset(private_buffer, 0, MPATH_MAX_PARAM_LEN), overwriting num_transportid.

The same could happen if a program compiled against the new API was linked against the old. In that case, the overwrite would happen in format_transportids().

Martin

#11 lore raw

Re: [PATCH 4/4] libmpathpersist: ABI change: limit data-in/out size to 8192 bytes

Quoted 5 lines · Martin Wilck

A program written against the old API might assume that the length of private_buffer was MPATH_MAX_PARAM_LEN, and call e.g. memset(private_buffer, 0, MPATH_MAX_PARAM_LEN), overwriting num_transportid.

And programs compiled against old headers would obviously use the wrong offsets for num_transportids and trnptid_list.

Similar for struct print_fulldescr_list.descriptors.

So I this does indeed break the ABI. Perhaps we shouldn't apply this without implementing clean so numbering.

Regards
Martin