1From bf3ded3a474d85da99eb717acdcd8ff4f89f9879 Mon Sep 17 00:00:00 2001
2From: Chrostoper Ertl <chertl@microsoft.com>
3Date: Thu, 28 Nov 2019 17:13:45 +0000
4Subject: [PATCH] fru, sdr: Fix id_string buffer overflows
5
6Final part of the fixes for CVE-2020-5208, see
7https://github.com/ipmitool/ipmitool/security/advisories/GHSA-g659-9qxw-p7cp
8
99 variants of stack buffer overflow when parsing `id_string` field of
10SDR records returned from `CMD_GET_SDR` command.
11
12SDR record structs have an `id_code` field, and an `id_string` `char`
13array.
14
15The length of `id_string` is calculated as `(id_code & 0x1f) + 1`,
16which can be larger than expected 16 characters (if `id_code = 0xff`,
17then length will be `(0xff & 0x1f) + 1 = 32`).
18
19In numerous places, this can cause stack buffer overflow when copying
20into fixed buffer of size `17` bytes from this calculated length.
21
22[Retrieve from:
23https://github.com/ipmitool/ipmitool/commit/7ccea283dd62a05a320c1921e3d8d71a87772637]
24Signed-off-by: Heiko Thiery <heiko.thiery@gmail.com>
25---
26 lib/ipmi_fru.c |  2 +-
27 lib/ipmi_sdr.c | 40 ++++++++++++++++++++++++----------------
28 2 files changed, 25 insertions(+), 17 deletions(-)
29
30diff --git a/lib/ipmi_fru.c b/lib/ipmi_fru.c
31index af99aa9..98bc984 100644
32--- a/lib/ipmi_fru.c
33+++ b/lib/ipmi_fru.c
34@@ -3062,7 +3062,7 @@ ipmi_fru_print(struct ipmi_intf * intf, struct sdr_record_fru_locator * fru)
35 		return 0;
36
37 	memset(desc, 0, sizeof(desc));
38-	memcpy(desc, fru->id_string, fru->id_code & 0x01f);
39+	memcpy(desc, fru->id_string, __min(fru->id_code & 0x01f, sizeof(desc)));
40 	desc[fru->id_code & 0x01f] = 0;
41 	printf("FRU Device Description : %s (ID %d)\n", desc, fru->device_id);
42
43diff --git a/lib/ipmi_sdr.c b/lib/ipmi_sdr.c
44index 2a9cbe3..62aac08 100644
45--- a/lib/ipmi_sdr.c
46+++ b/lib/ipmi_sdr.c
47@@ -2084,7 +2084,7 @@ ipmi_sdr_print_sensor_eventonly(struct ipmi_intf *intf,
48 		return -1;
49
50 	memset(desc, 0, sizeof (desc));
51-	snprintf(desc, (sensor->id_code & 0x1f) + 1, "%s", sensor->id_string);
52+	snprintf(desc, sizeof(desc), "%.*s", (sensor->id_code & 0x1f) + 1, sensor->id_string);
53
54 	if (verbose) {
55 		printf("Sensor ID              : %s (0x%x)\n",
56@@ -2135,7 +2135,7 @@ ipmi_sdr_print_sensor_mc_locator(struct ipmi_intf *intf,
57 		return -1;
58
59 	memset(desc, 0, sizeof (desc));
60-	snprintf(desc, (mc->id_code & 0x1f) + 1, "%s", mc->id_string);
61+	snprintf(desc, sizeof(desc), "%.*s", (mc->id_code & 0x1f) + 1, mc->id_string);
62
63 	if (verbose == 0) {
64 		if (csv_output)
65@@ -2228,7 +2228,7 @@ ipmi_sdr_print_sensor_generic_locator(struct ipmi_intf *intf,
66 	char desc[17];
67
68 	memset(desc, 0, sizeof (desc));
69-	snprintf(desc, (dev->id_code & 0x1f) + 1, "%s", dev->id_string);
70+	snprintf(desc, sizeof(desc), "%.*s", (dev->id_code & 0x1f) + 1, dev->id_string);
71
72 	if (!verbose) {
73 		if (csv_output)
74@@ -2285,7 +2285,7 @@ ipmi_sdr_print_sensor_fru_locator(struct ipmi_intf *intf,
75 	char desc[17];
76
77 	memset(desc, 0, sizeof (desc));
78-	snprintf(desc, (fru->id_code & 0x1f) + 1, "%s", fru->id_string);
79+	snprintf(desc, sizeof(desc), "%.*s", (fru->id_code & 0x1f) + 1, fru->id_string);
80
81 	if (!verbose) {
82 		if (csv_output)
83@@ -2489,35 +2489,43 @@ ipmi_sdr_print_name_from_rawentry(struct ipmi_intf *intf, uint16_t id,
84
85    int rc =0;
86    char desc[17];
87+   const char *id_string;
88+   uint8_t id_code;
89    memset(desc, ' ', sizeof (desc));
90
91    switch ( type) {
92       case SDR_RECORD_TYPE_FULL_SENSOR:
93       record.full = (struct sdr_record_full_sensor *) raw;
94-      snprintf(desc, (record.full->id_code & 0x1f) +1, "%s",
95-               (const char *)record.full->id_string);
96+      id_code = record.full->id_code;
97+      id_string = record.full->id_string;
98       break;
99+
100       case SDR_RECORD_TYPE_COMPACT_SENSOR:
101       record.compact = (struct sdr_record_compact_sensor *) raw	;
102-      snprintf(desc, (record.compact->id_code & 0x1f)  +1, "%s",
103-               (const char *)record.compact->id_string);
104+      id_code = record.compact->id_code;
105+      id_string = record.compact->id_string;
106       break;
107+
108       case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
109       record.eventonly  = (struct sdr_record_eventonly_sensor *) raw ;
110-      snprintf(desc, (record.eventonly->id_code & 0x1f)  +1, "%s",
111-               (const char *)record.eventonly->id_string);
112-      break;
113+      id_code = record.eventonly->id_code;
114+      id_string = record.eventonly->id_string;
115+      break;
116+
117       case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
118       record.mcloc  = (struct sdr_record_mc_locator *) raw ;
119-      snprintf(desc, (record.mcloc->id_code & 0x1f)  +1, "%s",
120-               (const char *)record.mcloc->id_string);
121+      id_code = record.mcloc->id_code;
122+      id_string = record.mcloc->id_string;
123       break;
124+
125       default:
126       rc = -1;
127-      break;
128-   }
129+   }
130+   if (!rc) {
131+       snprintf(desc, sizeof(desc), "%.*s", (id_code & 0x1f) + 1, id_string);
132+   }
133
134-      lprintf(LOG_INFO, "ID: 0x%04x , NAME: %-16s", id, desc);
135+   lprintf(LOG_INFO, "ID: 0x%04x , NAME: %-16s", id, desc);
136    return rc;
137 }
138
139--
1402.20.1
141
142