Skip to contentSkip to conversation

drm/amdgpu: bounds check VBIOS part number extraction

PATCH New Lijo Lazar 1 participant 5 messages Jun 22

Subsystems (2)
lore
Share & tools
lore

Lijo Lazar

author
#0 lore raw

PATCH 1/5 drm/amdgpu: bounds check VBIOS part number extraction

Bound atom_get_vbios_pn() by the BIOS size to avoid out-of-bounds reads.

---
 drivers/gpu/drm/amd/amdgpu/atom.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/atom.c +12 -4
index ebb4ac279393..5a4b06dfd096 100644
@@ -1467,12 +1467,17 @@ static unsigned char *atom_find_str_in_rom(struct atom_context *ctx, char *str,
 static void atom_get_vbios_pn(struct atom_context *ctx)
 {
 	unsigned char *p_rom;
+	unsigned char *p_end;
 	unsigned short off_to_vbios_str;
 	unsigned char *vbios_str;
 	int count;
 
 	off_to_vbios_str = 0;
 	p_rom = ctx->bios;
+	p_end = p_rom + ctx->bios_size;
+
+	if (p_rom + OFFSET_TO_GET_ATOMBIOS_STRING_START + 1 >= p_end)
+		return;
 
 	if (*(p_rom + OFFSET_TO_GET_ATOMBIOS_NUMBER_OF_STRINGS) != 0) {
 		off_to_vbios_str =
@@ -1483,19 +1488,22 @@ static void atom_get_vbios_pn(struct atom_context *ctx)
 		vbios_str = p_rom + OFFSET_TO_VBIOS_PART_NUMBER;
 	}
 
+	if (vbios_str >= p_end)
+		return;
+
 	if (*vbios_str == 0) {
 		vbios_str = atom_find_str_in_rom(ctx, BIOS_ATOM_PREFIX, 3, 1024, 64);
 		if (vbios_str == NULL)
 			vbios_str += sizeof(BIOS_ATOM_PREFIX) - 1;
 	}
 	OPTIMIZER_HIDE_VAR(vbios_str);
-	if (vbios_str != NULL && *vbios_str == 0)
+	if (vbios_str != NULL && vbios_str < p_end && *vbios_str == 0)
 		vbios_str++;
 
-	if (vbios_str != NULL) {
+	if (vbios_str != NULL && vbios_str < p_end) {
 		count = 0;
-		while ((count < BIOS_STRING_LENGTH) && vbios_str[count] >= ' ' &&
-		       vbios_str[count] <= 'z') {
+		while ((count < BIOS_STRING_LENGTH) && &vbios_str[count] < p_end &&
+		       vbios_str[count] >= ' ' && vbios_str[count] <= 'z') {
 			ctx->vbios_pn[count] = vbios_str[count];
 			count++;
 		}
Signature · 1 line
2.49.0
author
#1 lore raw

PATCH 2/5 drm/amdgpu: bounds check VBIOS date extraction

Bound atom_get_vbios_date() by the BIOS size to avoid out-of-bounds reads.

---
 drivers/gpu/drm/amd/amdgpu/atom.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/atom.c +8 -0
index 5a4b06dfd096..d18e46422520 100644
@@ -59,6 +59,9 @@
 
 #define ATOM_CMD_TIMEOUT_SEC	20
 
+/* Number of raw bytes read at OFFSET_TO_VBIOS_DATE (indices 0..13). */
+#define VBIOS_DATE_LEN		14
+
 /* Limit ATOM command table recursion (calltable) to avoid kernel stack overflow. */
 #define ATOM_EXECUTE_MAX_DEPTH	32
 
@@ -1420,6 +1423,11 @@ static void atom_get_vbios_date(struct atom_context *ctx)
 
 	p_rom = ctx->bios;
 
+	if (OFFSET_TO_VBIOS_DATE + VBIOS_DATE_LEN > ctx->bios_size) {
+		ctx->date[0] = '\0';
+		return;
+	}
+
 	date_in_rom = p_rom + OFFSET_TO_VBIOS_DATE;
 
 	ctx->date[0] = '2';
Signature · 1 line
2.49.0
author
#2 lore raw

PATCH 3/5 drm/amdgpu: bounds check VBIOS string search

In reply to Lijo Lazar

Bound atom_find_str_in_rom() by the BIOS size to avoid out-of-bounds reads.

---
 drivers/gpu/drm/amd/amdgpu/atom.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/atom.c +5 -2
index d18e46422520..07a33f94ed6a 100644
@@ -1449,8 +1449,8 @@ static void atom_get_vbios_date(struct atom_context *ctx)
 	ctx->date[16] = '\0';
 }
 
-static unsigned char *atom_find_str_in_rom(struct atom_context *ctx, char *str, int start,
-					   int end, int maxlen)
+static unsigned char *atom_find_str_in_rom(struct atom_context *ctx, char *str, u32 start,
+					   u32 end, int maxlen)
 {
 	unsigned long str_off;
 	unsigned char *p_rom;
@@ -1460,6 +1460,9 @@ static unsigned char *atom_find_str_in_rom(struct atom_context *ctx, char *str,
 	str_len = strnlen(str, maxlen);
 	p_rom = ctx->bios;
 
+	if (end + str_len > ctx->bios_size)
+		return NULL;
+
 	for (; start <= end; ++start) {
 		for (str_off = 0; str_off < str_len; ++str_off) {
 			if (str[str_off] != *(p_rom + start + str_off))
Signature · 1 line
2.49.0
author
#3 lore raw

PATCH 4/5 drm/amdgpu: bounds check VBIOS version extraction

In reply to Lijo Lazar

Bound atom_get_vbios_version() by the BIOS size to avoid out-of-bounds reads.

---
 drivers/gpu/drm/amd/amdgpu/atom.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/atom.c +8 -2
index 07a33f94ed6a..0f6def9c3318 100644
@@ -1528,8 +1528,10 @@ static void atom_get_vbios_version(struct atom_context *ctx)
 	unsigned short start = 3, end;
 	unsigned char *vbios_ver;
 	unsigned char *p_rom;
+	unsigned char *p_end;
 
 	p_rom = ctx->bios;
+	p_end = p_rom + ctx->bios_size;
 	/* Search from strings offset if it's present */
 	start = *(unsigned short *)(p_rom +
 				    OFFSET_TO_GET_ATOMBIOS_STRING_START);
@@ -1546,10 +1548,14 @@ static void atom_get_vbios_version(struct atom_context *ctx)
 	/* find anchor ATOMBIOSBK-AMD */
 	vbios_ver =
 		atom_find_str_in_rom(ctx, BIOS_VERSION_PREFIX, start, end, 64);
-	if (vbios_ver != NULL) {
+
+	if (vbios_ver != NULL && vbios_ver + 18 < p_end) {
 		/* skip ATOMBIOSBK-AMD VER */
 		vbios_ver += 18;
-		memcpy(ctx->vbios_ver_str, vbios_ver, STRLEN_NORMAL);
+		/* copy the version, truncating to whatever fits in the image */
+		strscpy(ctx->vbios_ver_str, vbios_ver,
+			min(sizeof(ctx->vbios_ver_str),
+			    (size_t)(p_end - vbios_ver)));
 	} else {
 		ctx->vbios_ver_str[0] = '\0';
 	}
Signature · 1 line
2.49.0
author
#4 lore raw

PATCH 5/5 drm/amdgpu: bounds check VBIOS build extraction

In reply to Lijo Lazar

Bound atom_get_vbios_build() by the BIOS size to avoid out-of-bounds reads.

---
 drivers/gpu/drm/amd/amdgpu/atom.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/atom.c +4 -0
index 0f6def9c3318..21ca934af26e 100644
@@ -1568,6 +1568,10 @@ static void atom_get_vbios_build(struct atom_context *ctx)
 	uint16_t base, len;
 
 	base = CU16(ATOM_ROM_TABLE_PTR);
+
+	if (base + ATOM_ROM_CFG_PTR + 1 >= ctx->bios_size)
+		return;
+
 	atom_rom_hdr = CSTR(base);
 
 	str = CSTR(CU16(base + ATOM_ROM_CFG_PTR));
Signature · 1 line
2.49.0