051a90
From a1732e68bce148255785e67eb035520729274d86 Mon Sep 17 00:00:00 2001
4857c9
From: Chrostoper Ertl <chertl@microsoft.com>
4857c9
Date: Thu, 28 Nov 2019 16:33:59 +0000
4857c9
Subject: [PATCH] Fixes for CVE-2020-5208
4857c9
4857c9
see https://github.com/ipmitool/ipmitool/security/advisories/GHSA-g659-9qxw-p7cp
4857c9
4857c9
This patch is combination of following commits:
4857c9
4857c9
pick e824c23316ae50beb7f7488f2055ac65e8b341f2 fru: Fix buffer overflow vulnerabilities
4857c9
pick 840fb1cbb4fb365cb9797300e3374d4faefcdb10 fru: Fix buffer overflow in ipmi_spd_print_fru
4857c9
pick 41d7026946fafbd4d1ec0bcaca3ea30a6e8eed22 session: Fix buffer overflow in ipmi_get_session_info
051a90
pick 9452be87181a6e83cfcc768b3ed8321763db50e4 channel: Fix buffer overflow
4857c9
pick d45572d71e70840e0d4c50bf48218492b79c1a10 lanp: Fix buffer overflows in get_lan_param_select
4857c9
pick 7ccea283dd62a05a320c1921e3d8d71a87772637 fru, sdr: Fix id_string buffer overflows
4857c9
4857c9
[vdolezal@redhat.com]: fixed memleak of `spd_data` in
4857c9
    lib/dimm_spd.c:ipmi_spd_print_fru()
4857c9
---
4857c9
 lib/dimm_spd.c     | 11 ++++++++++-
051a90
 lib/ipmi_channel.c |  5 ++++-
4857c9
 lib/ipmi_fru.c     | 35 ++++++++++++++++++++++++++++++++---
4857c9
 lib/ipmi_lanp.c    | 14 +++++++-------
4857c9
 lib/ipmi_sdr.c     | 40 ++++++++++++++++++++++++----------------
4857c9
 lib/ipmi_session.c | 12 ++++++++----
051a90
 6 files changed, 85 insertions(+), 32 deletions(-)
4857c9
4857c9
diff --git a/lib/dimm_spd.c b/lib/dimm_spd.c
4857c9
index 41e30db..ebcc94c 100644
4857c9
--- a/lib/dimm_spd.c
4857c9
+++ b/lib/dimm_spd.c
4857c9
@@ -1621,7 +1621,7 @@ ipmi_spd_print_fru(struct ipmi_intf * intf, uint8_t id)
4857c9
 	struct ipmi_rq req;
4857c9
 	struct fru_info fru;
4857c9
 	uint8_t *spd_data, msg_data[4];
4857c9
-	int len, offset;
4857c9
+	uint32_t len, offset;
4857c9
 
4857c9
 	msg_data[0] = id;
4857c9
 
4857c9
@@ -1697,6 +1697,15 @@ ipmi_spd_print_fru(struct ipmi_intf * intf, uint8_t id)
4857c9
 		}
4857c9
 
4857c9
 		len = rsp->data[0];
4857c9
+		if(rsp->data_len < 1
4857c9
+		   || len > rsp->data_len - 1
4857c9
+		   || len > fru.size - offset)
4857c9
+		{
4857c9
+			printf(" Not enough buffer size");
4857c9
+			free(spd_data);
4857c9
+			spd_data = NULL;
4857c9
+			return -1;
4857c9
+		}
4857c9
 		memcpy(&spd_data[offset], rsp->data + 1, len);
4857c9
 		offset += len;
4857c9
 	} while (offset < fru.size);
051a90
diff --git a/lib/ipmi_channel.c b/lib/ipmi_channel.c
051a90
index 3ae3104..80ba522 100644
051a90
--- a/lib/ipmi_channel.c
051a90
+++ b/lib/ipmi_channel.c
051a90
@@ -447,7 +447,10 @@ ipmi_get_channel_cipher_suites(struct ipmi_intf *intf, const char *payload_type,
051a90
 			lprintf(LOG_ERR, "Unable to Get Channel Cipher Suites");
051a90
 			return -1;
051a90
 		}
051a90
-		if (rsp->ccode || rsp->data_len < 1) {
051a90
+		if (rsp->ccode
051a90
+		    || rsp->data_len < 1
051a90
+		    || rsp->data_len > sizeof(uint8_t) + MAX_CIPHER_SUITE_DATA_LEN)
051a90
+		{
051a90
 			lprintf(LOG_ERR, "Get Channel Cipher Suites failed: %s",
051a90
 					val2str(rsp->ccode, completion_code_vals));
051a90
 			return -1;
4857c9
diff --git a/lib/ipmi_fru.c b/lib/ipmi_fru.c
4857c9
index cf00eff..98bc984 100644
4857c9
--- a/lib/ipmi_fru.c
4857c9
+++ b/lib/ipmi_fru.c
4857c9
@@ -615,7 +615,10 @@ int
4857c9
 read_fru_area(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
4857c9
 			uint32_t offset, uint32_t length, uint8_t *frubuf)
4857c9
 {
4857c9
-	uint32_t off = offset, tmp, finish;
4857c9
+	uint32_t off = offset;
4857c9
+	uint32_t tmp;
4857c9
+	uint32_t finish;
4857c9
+	uint32_t size_left_in_buffer;
4857c9
 	struct ipmi_rs * rsp;
4857c9
 	struct ipmi_rq req;
4857c9
 	uint8_t msg_data[4];
4857c9
@@ -628,10 +631,12 @@ read_fru_area(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
4857c9
 
4857c9
 	finish = offset + length;
4857c9
 	if (finish > fru->size) {
4857c9
+		memset(frubuf + fru->size, 0, length - fru->size);
4857c9
 		finish = fru->size;
4857c9
 		lprintf(LOG_NOTICE, "Read FRU Area length %d too large, "
4857c9
 			"Adjusting to %d",
4857c9
 			offset + length, finish - offset);
4857c9
+		length = finish - offset;
4857c9
 	}
4857c9
 
4857c9
 	memset(&req, 0, sizeof(req));
4857c9
@@ -667,6 +672,7 @@ read_fru_area(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
4857c9
 		}
4857c9
 	}
4857c9
 
4857c9
+	size_left_in_buffer = length;
4857c9
 	do {
4857c9
 		tmp = fru->access ? off >> 1 : off;
4857c9
 		msg_data[0] = id;
4857c9
@@ -707,9 +713,18 @@ read_fru_area(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
4857c9
 		}
4857c9
 
4857c9
 		tmp = fru->access ? rsp->data[0] << 1 : rsp->data[0];
4857c9
+		if(rsp->data_len < 1
4857c9
+		   || tmp > rsp->data_len - 1
4857c9
+		   || tmp > size_left_in_buffer)
4857c9
+		{
4857c9
+			printf(" Not enough buffer size");
4857c9
+			return -1;
4857c9
+		}
4857c9
+
4857c9
 		memcpy(frubuf, rsp->data + 1, tmp);
4857c9
 		off += tmp;
4857c9
 		frubuf += tmp;
4857c9
+		size_left_in_buffer -= tmp;
4857c9
 		/* sometimes the size returned in the Info command
4857c9
 		* is too large.  return 0 so higher level function
4857c9
 		* still attempts to parse what was returned */
4857c9
@@ -742,7 +757,9 @@ read_fru_area_section(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
4857c9
 			uint32_t offset, uint32_t length, uint8_t *frubuf)
4857c9
 {
4857c9
 	static uint32_t fru_data_rqst_size = 20;
4857c9
-	uint32_t off = offset, tmp, finish;
4857c9
+	uint32_t off = offset;
4857c9
+	uint32_t tmp, finish;
4857c9
+	uint32_t size_left_in_buffer;
4857c9
 	struct ipmi_rs * rsp;
4857c9
 	struct ipmi_rq req;
4857c9
 	uint8_t msg_data[4];
4857c9
@@ -755,10 +772,12 @@ read_fru_area_section(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
4857c9
 
4857c9
 	finish = offset + length;
4857c9
 	if (finish > fru->size) {
4857c9
+		memset(frubuf + fru->size, 0, length - fru->size);
4857c9
 		finish = fru->size;
4857c9
 		lprintf(LOG_NOTICE, "Read FRU Area length %d too large, "
4857c9
 			"Adjusting to %d",
4857c9
 			offset + length, finish - offset);
4857c9
+		length = finish - offset;
4857c9
 	}
4857c9
 
4857c9
 	memset(&req, 0, sizeof(req));
4857c9
@@ -773,6 +792,8 @@ read_fru_area_section(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
4857c9
 	if (fru->access && fru_data_rqst_size > 16)
4857c9
 #endif
4857c9
 		fru_data_rqst_size = 16;
4857c9
+
4857c9
+	size_left_in_buffer = length;
4857c9
 	do {
4857c9
 		tmp = fru->access ? off >> 1 : off;
4857c9
 		msg_data[0] = id;
4857c9
@@ -804,8 +825,16 @@ read_fru_area_section(struct ipmi_intf * intf, struct fru_info *fru, uint8_t id,
4857c9
 		}
4857c9
 
4857c9
 		tmp = fru->access ? rsp->data[0] << 1 : rsp->data[0];
4857c9
+		if(rsp->data_len < 1
4857c9
+		   || tmp > rsp->data_len - 1
4857c9
+		   || tmp > size_left_in_buffer)
4857c9
+		{
4857c9
+			printf(" Not enough buffer size");
4857c9
+			return -1;
4857c9
+		}
4857c9
 		memcpy((frubuf + off)-offset, rsp->data + 1, tmp);
4857c9
 		off += tmp;
4857c9
+		size_left_in_buffer -= tmp;
4857c9
 
4857c9
 		/* sometimes the size returned in the Info command
4857c9
 		* is too large.  return 0 so higher level function
4857c9
@@ -3033,7 +3062,7 @@ ipmi_fru_print(struct ipmi_intf * intf, struct sdr_record_fru_locator * fru)
4857c9
 		return 0;
4857c9
 
4857c9
 	memset(desc, 0, sizeof(desc));
4857c9
-	memcpy(desc, fru->id_string, fru->id_code & 0x01f);
4857c9
+	memcpy(desc, fru->id_string, __min(fru->id_code & 0x01f, sizeof(desc)));
4857c9
 	desc[fru->id_code & 0x01f] = 0;
4857c9
 	printf("FRU Device Description : %s (ID %d)\n", desc, fru->device_id);
4857c9
 
4857c9
diff --git a/lib/ipmi_lanp.c b/lib/ipmi_lanp.c
4857c9
index 65d881b..022c7f1 100644
4857c9
--- a/lib/ipmi_lanp.c
4857c9
+++ b/lib/ipmi_lanp.c
4857c9
@@ -1809,7 +1809,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert,
4857c9
 		if (p == NULL) {
4857c9
 			return (-1);
4857c9
 		}
4857c9
-		memcpy(data, p->data, p->data_len);
4857c9
+		memcpy(data, p->data, __min(p->data_len, sizeof(data)));
4857c9
 		/* set new ipaddr */
4857c9
 		memcpy(data+3, temp, 4);
4857c9
 		printf("Setting LAN Alert %d IP Address to %d.%d.%d.%d\n", alert,
4857c9
@@ -1824,7 +1824,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert,
4857c9
 		if (p == NULL) {
4857c9
 			return (-1);
4857c9
 		}
4857c9
-		memcpy(data, p->data, p->data_len);
4857c9
+		memcpy(data, p->data, __min(p->data_len, sizeof(data)));
4857c9
 		/* set new macaddr */
4857c9
 		memcpy(data+7, temp, 6);
4857c9
 		printf("Setting LAN Alert %d MAC Address to "
4857c9
@@ -1838,7 +1838,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert,
4857c9
 		if (p == NULL) {
4857c9
 			return (-1);
4857c9
 		}
4857c9
-		memcpy(data, p->data, p->data_len);
4857c9
+		memcpy(data, p->data, __min(p->data_len, sizeof(data)));
4857c9
 
4857c9
 		if (strncasecmp(argv[1], "def", 3) == 0 ||
4857c9
 		    strncasecmp(argv[1], "default", 7) == 0) {
4857c9
@@ -1864,7 +1864,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert,
4857c9
 		if (p == NULL) {
4857c9
 			return (-1);
4857c9
 		}
4857c9
-		memcpy(data, p->data, p->data_len);
4857c9
+		memcpy(data, p->data, __min(p->data_len, sizeof(data)));
4857c9
 
4857c9
 		if (strncasecmp(argv[1], "on", 2) == 0 ||
4857c9
 		    strncasecmp(argv[1], "yes", 3) == 0) {
4857c9
@@ -1889,7 +1889,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert,
4857c9
 		if (p == NULL) {
4857c9
 			return (-1);
4857c9
 		}
4857c9
-		memcpy(data, p->data, p->data_len);
4857c9
+		memcpy(data, p->data, __min(p->data_len, sizeof(data)));
4857c9
 
4857c9
 		if (strncasecmp(argv[1], "pet", 3) == 0) {
4857c9
 			printf("Setting LAN Alert %d destination to PET Trap\n", alert);
4857c9
@@ -1917,7 +1917,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert,
4857c9
 		if (p == NULL) {
4857c9
 			return (-1);
4857c9
 		}
4857c9
-		memcpy(data, p->data, p->data_len);
4857c9
+		memcpy(data, p->data, __min(p->data_len, sizeof(data)));
4857c9
 
4857c9
 		if (str2uchar(argv[1], &data[2]) != 0) {
4857c9
 			lprintf(LOG_ERR, "Invalid time: %s", argv[1]);
4857c9
@@ -1933,7 +1933,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert,
4857c9
 		if (p == NULL) {
4857c9
 			return (-1);
4857c9
 		}
4857c9
-		memcpy(data, p->data, p->data_len);
4857c9
+		memcpy(data, p->data, __min(p->data_len, sizeof(data)));
4857c9
 
4857c9
 		if (str2uchar(argv[1], &data[3]) != 0) {
4857c9
 			lprintf(LOG_ERR, "Invalid retry: %s", argv[1]);
4857c9
diff --git a/lib/ipmi_sdr.c b/lib/ipmi_sdr.c
4857c9
index fd2c02d..01d81f7 100644
4857c9
--- a/lib/ipmi_sdr.c
4857c9
+++ b/lib/ipmi_sdr.c
4857c9
@@ -2086,7 +2086,7 @@ ipmi_sdr_print_sensor_eventonly(struct ipmi_intf *intf,
4857c9
 		return -1;
4857c9
 
4857c9
 	memset(desc, 0, sizeof (desc));
4857c9
-	snprintf(desc, (sensor->id_code & 0x1f) + 1, "%s", sensor->id_string);
4857c9
+	snprintf(desc, sizeof(desc), "%.*s", (sensor->id_code & 0x1f) + 1, sensor->id_string);
4857c9
 
4857c9
 	if (verbose) {
4857c9
 		printf("Sensor ID              : %s (0x%x)\n",
4857c9
@@ -2137,7 +2137,7 @@ ipmi_sdr_print_sensor_mc_locator(struct ipmi_intf *intf,
4857c9
 		return -1;
4857c9
 
4857c9
 	memset(desc, 0, sizeof (desc));
4857c9
-	snprintf(desc, (mc->id_code & 0x1f) + 1, "%s", mc->id_string);
4857c9
+	snprintf(desc, sizeof(desc), "%.*s", (mc->id_code & 0x1f) + 1, mc->id_string);
4857c9
 
4857c9
 	if (verbose == 0) {
4857c9
 		if (csv_output)
4857c9
@@ -2230,7 +2230,7 @@ ipmi_sdr_print_sensor_generic_locator(struct ipmi_intf *intf,
4857c9
 	char desc[17];
4857c9
 
4857c9
 	memset(desc, 0, sizeof (desc));
4857c9
-	snprintf(desc, (dev->id_code & 0x1f) + 1, "%s", dev->id_string);
4857c9
+	snprintf(desc, sizeof(desc), "%.*s", (dev->id_code & 0x1f) + 1, dev->id_string);
4857c9
 
4857c9
 	if (!verbose) {
4857c9
 		if (csv_output)
4857c9
@@ -2287,7 +2287,7 @@ ipmi_sdr_print_sensor_fru_locator(struct ipmi_intf *intf,
4857c9
 	char desc[17];
4857c9
 
4857c9
 	memset(desc, 0, sizeof (desc));
4857c9
-	snprintf(desc, (fru->id_code & 0x1f) + 1, "%s", fru->id_string);
4857c9
+	snprintf(desc, sizeof(desc), "%.*s", (fru->id_code & 0x1f) + 1, fru->id_string);
4857c9
 
4857c9
 	if (!verbose) {
4857c9
 		if (csv_output)
4857c9
@@ -2491,35 +2491,43 @@ ipmi_sdr_print_name_from_rawentry(struct ipmi_intf *intf, uint16_t id,
4857c9
 
4857c9
    int rc =0;
4857c9
    char desc[17];
4857c9
+   const char *id_string;
4857c9
+   uint8_t id_code;
4857c9
    memset(desc, ' ', sizeof (desc));
4857c9
 
4857c9
    switch ( type) {
4857c9
       case SDR_RECORD_TYPE_FULL_SENSOR:
4857c9
       record.full = (struct sdr_record_full_sensor *) raw;
4857c9
-      snprintf(desc, (record.full->id_code & 0x1f) +1, "%s",
4857c9
-               (const char *)record.full->id_string);
4857c9
+      id_code = record.full->id_code;
4857c9
+      id_string = record.full->id_string;
4857c9
       break;
4857c9
+
4857c9
       case SDR_RECORD_TYPE_COMPACT_SENSOR:
4857c9
       record.compact = (struct sdr_record_compact_sensor *) raw	;
4857c9
-      snprintf(desc, (record.compact->id_code & 0x1f)  +1, "%s",
4857c9
-               (const char *)record.compact->id_string);
4857c9
+      id_code = record.compact->id_code;
4857c9
+      id_string = record.compact->id_string;
4857c9
       break;
4857c9
+
4857c9
       case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
4857c9
       record.eventonly  = (struct sdr_record_eventonly_sensor *) raw ;
4857c9
-      snprintf(desc, (record.eventonly->id_code & 0x1f)  +1, "%s",
4857c9
-               (const char *)record.eventonly->id_string);
4857c9
-      break;            
4857c9
+      id_code = record.eventonly->id_code;
4857c9
+      id_string = record.eventonly->id_string;
4857c9
+      break;
4857c9
+
4857c9
       case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
4857c9
       record.mcloc  = (struct sdr_record_mc_locator *) raw ;
4857c9
-      snprintf(desc, (record.mcloc->id_code & 0x1f)  +1, "%s",
4857c9
-               (const char *)record.mcloc->id_string);		
4857c9
+      id_code = record.mcloc->id_code;
4857c9
+      id_string = record.mcloc->id_string;
4857c9
       break;
4857c9
+
4857c9
       default:
4857c9
       rc = -1;
4857c9
-      break;
4857c9
-   }   
4857c9
+   }
4857c9
+   if (!rc) {
4857c9
+       snprintf(desc, sizeof(desc), "%.*s", (id_code & 0x1f) + 1, id_string);
4857c9
+   }
4857c9
 
4857c9
-      lprintf(LOG_INFO, "ID: 0x%04x , NAME: %-16s", id, desc);
4857c9
+   lprintf(LOG_INFO, "ID: 0x%04x , NAME: %-16s", id, desc);
4857c9
    return rc;
4857c9
 }
4857c9
 
4857c9
diff --git a/lib/ipmi_session.c b/lib/ipmi_session.c
4857c9
index 141f0f4..b9af1fd 100644
4857c9
--- a/lib/ipmi_session.c
4857c9
+++ b/lib/ipmi_session.c
4857c9
@@ -309,8 +309,10 @@ ipmi_get_session_info(struct ipmi_intf         * intf,
4857c9
 		}
4857c9
 		else
4857c9
 		{
4857c9
-			memcpy(&session_info,  rsp->data, rsp->data_len);
4857c9
-			print_session_info(&session_info, rsp->data_len);
4857c9
+			memcpy(&session_info,  rsp->data,
4857c9
+			       __min(rsp->data_len, sizeof(session_info)));
4857c9
+			print_session_info(&session_info,
4857c9
+			                   __min(rsp->data_len, sizeof(session_info)));
4857c9
 		}
4857c9
 		break;
4857c9
 		
4857c9
@@ -341,8 +343,10 @@ ipmi_get_session_info(struct ipmi_intf         * intf,
4857c9
 				break;
4857c9
 			}
4857c9
 
4857c9
-			memcpy(&session_info,  rsp->data, rsp->data_len);
4857c9
-			print_session_info(&session_info, rsp->data_len);
4857c9
+			memcpy(&session_info,  rsp->data,
4857c9
+			       __min(rsp->data_len, sizeof(session_info)));
4857c9
+			print_session_info(&session_info,
4857c9
+			                   __min(rsp->data_len, sizeof(session_info)));
4857c9
 			
4857c9
 		} while (i <= session_info.session_slot_count);
4857c9
 		break;
4857c9
-- 
4857c9
2.20.1
4857c9