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