Skip to contentSkip to conversation

platform/x86: ibm_rtl: Fix potential OOB read at signature search

PATCH Superseded Takashi Iwai 2 participants 5 messages Oct 20 – Oct 21 x86 platform drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: ibm_rtl: Fix potential OOB read at signature search

The loop for signature search fetches 8 bytes at each check, while the loop count is calculated in 4 bytes unit, which means that, at the last entry, it fetches over the limit.

Correct the boundary condition to "ebda_size/sizeof(unsigned int)-1" for avoiding the potential OOB access.

Fixes 15dd100349b8 ("platform/x86: ibm_rtl: fix EBDA signature search pointer arithmetic")
---
 drivers/platform/x86/ibm_rtl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/ibm_rtl.c +1 -1
index 139956168cf9..6d6998049d8c 100644
@@ -271,7 +271,7 @@ static int __init ibm_rtl_init(void) {
 		return -ENOMEM;
 
 	/* search for the _RTL_ signature at the start of the table */
-	for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
+	for (i = 0 ; i < ebda_size/sizeof(unsigned int) - 1; i++) {
 		struct ibm_rtl_table __iomem * tmp;
 		tmp = (struct ibm_rtl_table __iomem *) (ebda_map + i*sizeof(unsigned int));
 		if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 1 line · Takashi Iwai

The loop for signature search fetches 8 bytes at each check,

But that's not all it's going to access??

Quoted 5 lines

while the
loop count is calculated in 4 bytes unit, which means that, at the last entry, it fetches over the limit.

Correct the boundary condition to "ebda_size/sizeof(unsigned int)-1"

This seems even more obfuscated than this code already is!

I think it should do this instead:

(ebda_size - sizeof(*rtl_table)) / sizeof(u32)

Admittedly, it a logical change too but why you think only partial RTL table is good enough just because signature can be checked? After the sig check, it will read the struct beyond that.

...And that other sizeof() should use u32 as well.

Unrelated to this change and if you have this HW and can test changes, it would be nice to also get rid of the debug parameter and use dynamic debugging instead of RTL_DEBUG() for consistency.

I also don't understand why it's mixing readq() and ioread*() for reading from the same area.

Quoted 13 lines

for avoiding the potential OOB access.

Fixes 15dd100349b8 ("platform/x86: ibm_rtl: fix EBDA signature search pointer arithmetic")
---
 drivers/platform/x86/ibm_rtl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/ibm_rtl.c +0 -0
index 139956168cf9..6d6998049d8c 100644

drivers/platform/x86/ibm_rtl.c static int __init ibm_rtl_init(void) { · line 274

Quoted 5 lines
drivers/platform/x86/ibm_rtl.c +1 -1
@@ -271,7 +271,7 @@ static int __init ibm_rtl_init(void) {
 		return -ENOMEM;
 
 	/* search for the _RTL_ signature at the start of the table */
-	for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
+	for (i = 0 ; i < ebda_size/sizeof(unsigned int) - 1; i++) {

Also remove that extra space after i = 0.

Quoted 4 lines
drivers/platform/x86/ibm_rtl.c +0 -0
 		struct ibm_rtl_table __iomem * tmp;
 		tmp = (struct ibm_rtl_table __iomem *) (ebda_map + i*sizeof(unsigned int));
 		if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
Signature · 1 line
 i.
#2 lore raw
Quoted 6 lines · Ilpo Järvinen
Quoted 1 line · Takashi Iwai

The loop for signature search fetches 8 bytes at each check,

But that's not all it's going to access??

Oh indeed it access further.

Quoted 7 lines
Quoted 5 lines

while the
loop count is calculated in 4 bytes unit, which means that, at the last entry, it fetches over the limit.

Correct the boundary condition to "ebda_size/sizeof(unsigned int)-1"

This seems even more obfuscated than this code already is!

Yeah, the driver code is really ugly, and I made a whip cream on the top :)

Quoted 3 lines

I think it should do this instead:

(ebda_size - sizeof(*rtl_table)) / sizeof(u32)

Hrm, this will result in the omit of the last loop entry?

I guess it'd be better understandable rather by directly adding the offset in the loop instead of the counter. Something like below.

Quoted 10 lines

Admittedly, it a logical change too but why you think only partial RTL table is good enough just because signature can be checked? After the sig check, it will read the struct beyond that.

...And that other sizeof() should use u32 as well.

Unrelated to this change and if you have this HW and can test changes, it would be nice to also get rid of the debug parameter and use dynamic debugging instead of RTL_DEBUG() for consistency.

No, I don't. It was caught only through reviews at backporting to the distro kernels.

Quoted 2 lines

I also don't understand why it's mixing readq() and ioread*() for reading from the same area.

I guess it's just an optimization for the signature check. The driver needs significant code cleanup, in anyway...

thanks,

Takashi

-- 8< --
From: Takashi Iwai <[email protected]>
Subject: [PATCH v2] platform/x86: ibm_rtl: Fix potential OOB read at signature
search

The loop for signature search fetches 22 bytes of struct ibm_rtl_table at each check, while the loop count is calculated in 4 bytes unit, which means that, at the last entry, it fetches over the limit.

Rewrite the loop to use the byte offset and correct the boundary condition check for avoiding the potential OOB access.

Fixes 15dd100349b8 ("platform/x86: ibm_rtl: fix EBDA signature search pointer arithmetic")
---
 drivers/platform/x86/ibm_rtl.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/ibm_rtl.c +4 -4
index 139956168cf9..6f0a10e1d81e 100644
@@ -234,9 +234,9 @@ static const struct dmi_system_id ibm_rtl_dmi_table[] __initconst = {
 };
 
 static int __init ibm_rtl_init(void) {
-	unsigned long ebda_addr, ebda_size;
+	unsigned long ebda_addr, ebda_size, offset;
 	unsigned int ebda_kb;
-	int ret = -ENODEV, i;
+	int ret = -ENODEV;
 
 	if (force)
 		pr_warn("module loaded by force\n");
@@ -271,9 +271,9 @@ static int __init ibm_rtl_init(void) {
 		return -ENOMEM;
 
 	/* search for the _RTL_ signature at the start of the table */
-	for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
+	for (offset = 0 ; offset + sizeof(*rtl_table) <= ebda_size; offset += 4) {
 		struct ibm_rtl_table __iomem * tmp;
-		tmp = (struct ibm_rtl_table __iomem *) (ebda_map + i*sizeof(unsigned int));
+		tmp = (struct ibm_rtl_table __iomem *) (ebda_map + offset);
 		if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
 			phys_addr_t addr;
 			unsigned int plen;
Signature · 1 line
2.55.0
maintainer
#3 lore raw
Quoted 22 lines · Takashi Iwai
Quoted 6 lines · Ilpo Järvinen
Quoted 1 line · Takashi Iwai

The loop for signature search fetches 8 bytes at each check,

But that's not all it's going to access??

Oh indeed it access further.

Quoted 7 lines
Quoted 5 lines

while the
loop count is calculated in 4 bytes unit, which means that, at the last entry, it fetches over the limit.

Correct the boundary condition to "ebda_size/sizeof(unsigned int)-1"

This seems even more obfuscated than this code already is!

Yeah, the driver code is really ugly, and I made a whip cream on the top :)

Quoted 8 lines
Quoted 3 lines

I think it should do this instead:

(ebda_size - sizeof(*rtl_table)) / sizeof(u32)

Hrm, this will result in the omit of the last loop entry?

I guess it'd be better understandable rather by directly adding the offset in the loop instead of the counter. Something like below.

The patch below looks fine sans one nit but could you please send it in own thread so it gets captured by patchwork & sashiko & etc.

Quoted 7 lines
Quoted 7 lines

Admittedly, it a logical change too but why you think only partial RTL table is good enough just because signature can be checked? After the sig check, it will read the struct beyond that.

...And that other sizeof() should use u32 as well.

Quoted 6 lines
Quoted 3 lines

Unrelated to this change and if you have this HW and can test changes, it would be nice to also get rid of the debug parameter and use dynamic debugging instead of RTL_DEBUG() for consistency.

No, I don't. It was caught only through reviews at backporting to the distro kernels.

Fair.

Quoted 5 lines
Quoted 2 lines

I also don't understand why it's mixing readq() and ioread*() for reading from the same area.

I guess it's just an optimization for the signature check. The driver needs significant code cleanup, in anyway...

Sure. I had written something along the lines of having plenty of additional comments but cut it out in the end. :-)

Quoted 39 lines

thanks,

Takashi

-- 8< --
From: Takashi Iwai <[email protected]>
Subject: [PATCH v2] platform/x86: ibm_rtl: Fix potential OOB read at signature
search

The loop for signature search fetches 22 bytes of struct ibm_rtl_table at each check, while the loop count is calculated in 4 bytes unit, which means that, at the last entry, it fetches over the limit.

Rewrite the loop to use the byte offset and correct the boundary condition check for avoiding the potential OOB access.

Fixes 15dd100349b8 ("platform/x86: ibm_rtl: fix EBDA signature search pointer arithmetic")
---
 drivers/platform/x86/ibm_rtl.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/ibm_rtl.c +2 -2
index 139956168cf9..6f0a10e1d81e 100644
@@ -234,9 +234,9 @@ static const struct dmi_system_id ibm_rtl_dmi_table[] __initconst = {
 };
 
 static int __init ibm_rtl_init(void) {
-	unsigned long ebda_addr, ebda_size;
+	unsigned long ebda_addr, ebda_size, offset;
 	unsigned int ebda_kb;
-	int ret = -ENODEV, i;
+	int ret = -ENODEV;
 
 	if (force)
 		pr_warn("module loaded by force\n");

drivers/platform/x86/ibm_rtl.c static int __init ibm_rtl_init(void) { · line 274

Quoted 5 lines
drivers/platform/x86/ibm_rtl.c +1 -1
@@ -271,9 +271,9 @@ static int __init ibm_rtl_init(void) {
 		return -ENOMEM;
 
 	/* search for the _RTL_ signature at the start of the table */
-	for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
+	for (offset = 0 ; offset + sizeof(*rtl_table) <= ebda_size; offset += 4) {

Nit, remove the extra space before semicolon.

Signature · 1 line
 i.
#4 lore raw
Quoted 29 lines · Ilpo Järvinen
Quoted 26 lines · Takashi Iwai
Quoted 6 lines · Ilpo Järvinen
Quoted 1 line · Takashi Iwai

The loop for signature search fetches 8 bytes at each check,

But that's not all it's going to access??

Oh indeed it access further.

Quoted 7 lines
Quoted 5 lines

while the
loop count is calculated in 4 bytes unit, which means that, at the last entry, it fetches over the limit.

Correct the boundary condition to "ebda_size/sizeof(unsigned int)-1"

This seems even more obfuscated than this code already is!

Yeah, the driver code is really ugly, and I made a whip cream on the top :)

Quoted 3 lines

I think it should do this instead:

(ebda_size - sizeof(*rtl_table)) / sizeof(u32)

Quoted 7 lines
Quoted 4 lines

Hrm, this will result in the omit of the last loop entry?

I guess it'd be better understandable rather by directly adding the offset in the loop instead of the counter. Something like below.

The patch below looks fine sans one nit but could you please send it in own thread so it gets captured by patchwork & sashiko & etc.

OK, will do.

(snip)

Quoted 8 lines

drivers/platform/x86/ibm_rtl.c static int __init ibm_rtl_init(void) { · line 274

Quoted 6 lines
drivers/platform/x86/ibm_rtl.c +1 -1
@@ -271,9 +271,9 @@ static int __init ibm_rtl_init(void) {
 		return -ENOMEM;
 
 	/* search for the _RTL_ signature at the start of the table */
-	for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
+	for (offset = 0 ; offset + sizeof(*rtl_table) <= ebda_size; offset += 4) {

Nit, remove the extra space before semicolon.

Will be corrected in the next submission.

thanks,

Takashi