Skip to contentSkip to conversation

platform/x86: hp-bioscfg: fix heap OOB in hp_enforce_single_line_input()

PATCH Superseded Muhammad Bilal 3 participants 7 messages Oct 16 – Oct 24

Subsystems (3)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 platform/x86: hp-bioscfg: fix heap OOB in hp_enforce_single_line_input()

The input is copied with kstrdup(), which stops at the first NUL, but the full write size is passed to hp_enforce_single_line_input(). With an embedded NUL the helper reads, and can write one byte, past the copy. Return -EINVAL if the input contains a NUL.

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.8): writing "A\0" followed by 4093 bytes of "B" to current_password 100 times returns -EINVAL every time, without reading into the heap.

Fixes a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")

---
Changes in v6:
- Return -EINVAL on embedded NUL instead of limiting the scan (Ilpo)

v5: https://lore.kernel.org/all/[email protected]/

 drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +4 -0
index 876fc35d035b..e306bd8b1e72 100644
@@ -162,6 +162,10 @@ int hp_enforce_single_line_input(char *buf, size_t count)
 {
 	char *p;
 
+	/* buf is a string, so a NUL before count is invalid input */
+	if (strnlen(buf, count) < count)
+		return -EINVAL;
+
 	p = memchr(buf, '\n', count);
 
 	if (p == buf + count - 1)

base-commit: 8667bd30fd033295b3038ce374c551d1059538ba

Signature · 1 line
2.56.0
maintainer
#1 lore raw
Quoted 27 lines · Muhammad Bilal

The input is copied with kstrdup(), which stops at the first NUL, but the full write size is passed to hp_enforce_single_line_input(). With an embedded NUL the helper reads, and can write one byte, past the copy. Return -EINVAL if the input contains a NUL.

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.8): writing "A\0" followed by 4093 bytes of "B" to current_password 100 times returns -EINVAL every time, without reading into the heap.

Fixes a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")

---
Changes in v6:
- Return -EINVAL on embedded NUL instead of limiting the scan (Ilpo)

v5: https://lore.kernel.org/all/[email protected]/

 drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +0 -0
index 876fc35d035b..e306bd8b1e72 100644

drivers/platform/x86/hp/hp-bioscfg/bioscfg.c int hp_enforce_single_line_input(char *buf, size_t count) · line 166

Quoted 5 lines
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +2 -0
@@ -162,6 +162,10 @@ int hp_enforce_single_line_input(char *buf, size_t count)
 {
 	char *p;
 
+	/* buf is a string, so a NUL before count is invalid input */
+	if (strnlen(buf, count) < count)

Why use n variant?

Quoted 8 lines
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +2 -0
+		return -EINVAL;
+
 	p = memchr(buf, '\n', count);
 
 	if (p == buf + count - 1)

base-commit: 8667bd30fd033295b3038ce374c551d1059538ba

Signature · 1 line
 i.
#2 lore raw

Hi Ilpo,

Quoted 1 line

Why use n variant?

strnlen() keeps the read within count, which is the size of buf as far as this helper knows, so it does not depend on the caller NUL terminating the buffer. Both current callers pass a kstrdup() copy, so strlen() works too. I can switch to it in v7 if you prefer.

Thanks
Muhammad

Quoted 49 lines · Ilpo Järvinen
Quoted 27 lines · Muhammad Bilal

The input is copied with kstrdup(), which stops at the first NUL, but the full write size is passed to hp_enforce_single_line_input(). With an embedded NUL the helper reads, and can write one byte, past the copy. Return -EINVAL if the input contains a NUL.

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.8): writing "A\0" followed by 4093 bytes of "B" to current_password 100 times returns -EINVAL every time, without reading into the heap.

Fixes a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")

---
Changes in v6:
- Return -EINVAL on embedded NUL instead of limiting the scan (Ilpo)

v5: https://lore.kernel.org/all/[email protected]/

 drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +0 -0
index 876fc35d035b..e306bd8b1e72 100644

drivers/platform/x86/hp/hp-bioscfg/bioscfg.c int hp_enforce_single_line_input(char *buf, size_t count) · line 166

Quoted 5 lines
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +2 -0
@@ -162,6 +162,10 @@ int hp_enforce_single_line_input(char *buf, size_t count)
 {
      char *p;
 
+     /* buf is a string, so a NUL before count is invalid input */
+     if (strnlen(buf, count) < count)

Why use n variant?

Quoted 8 lines
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +2 -0
+             return -EINVAL;
+
      p = memchr(buf, '\n', count);
 
      if (p == buf + count - 1)

base-commit: 8667bd30fd033295b3038ce374c551d1059538ba

--
i.

maintainer
#3 lore raw
Quoted 7 lines · Muhammad Bilal

Hi Ilpo,

Quoted 1 line

Why use n variant?

strnlen() keeps the read within count, which is the size of buf as far as this helper knows, so it does not depend on the caller NUL terminating the buffer. Both current callers pass a kstrdup() copy, so strlen() works too. I can switch to it in v7 if you prefer.

As the contract here is the input must be a string, I don't see any need to limit the length with n variant so I do prefer strlen(). Also, we know that it should not be longer than count anyway.

--
i.

Quoted 55 lines

Thanks
Muhammad

Quoted 49 lines · Ilpo Järvinen
Quoted 27 lines · Muhammad Bilal

The input is copied with kstrdup(), which stops at the first NUL, but the full write size is passed to hp_enforce_single_line_input(). With an embedded NUL the helper reads, and can write one byte, past the copy. Return -EINVAL if the input contains a NUL.

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.8): writing "A\0" followed by 4093 bytes of "B" to current_password 100 times returns -EINVAL every time, without reading into the heap.

Fixes a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")

---
Changes in v6:
- Return -EINVAL on embedded NUL instead of limiting the scan (Ilpo)

v5: https://lore.kernel.org/all/[email protected]/

 drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +0 -0
index 876fc35d035b..e306bd8b1e72 100644

drivers/platform/x86/hp/hp-bioscfg/bioscfg.c int hp_enforce_single_line_input(char *buf, size_t count) · line 166

Quoted 5 lines
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +2 -0
@@ -162,6 +162,10 @@ int hp_enforce_single_line_input(char *buf, size_t count)
 {
      char *p;
 
+     /* buf is a string, so a NUL before count is invalid input */
+     if (strnlen(buf, count) < count)

Why use n variant?

Quoted 8 lines
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +2 -0
+             return -EINVAL;
+
      p = memchr(buf, '\n', count);
 
      if (p == buf + count - 1)

base-commit: 8667bd30fd033295b3038ce374c551d1059538ba

--
i.

maintainer
#4 lore raw
Quoted 5 lines · Ilpo Järvinen
Quoted 3 lines · Muhammad Bilal

Hi Ilpo,

Quoted 1 line

Why use n variant?

Quoted 8 lines
Quoted 4 lines

strnlen() keeps the read within count, which is the size of buf as far as this helper knows, so it does not depend on the caller NUL terminating the buffer. Both current callers pass a kstrdup() copy, so strlen() works too. I can switch to it in v7 if you prefer.

As the contract here is the input must be a string, I don't see any need to limit the length with n variant so I do prefer strlen(). Also, we know that it should not be longer than count anyway.

I only now notice Andy's comments which seem better suggestion in general.

Signature · 1 line
 i.
#5 lore raw

Hi Andy, Ilpo,

Thanks for the review. I switched to strnchrnul() as Andy suggested and posted v7:
https://lore.kernel.org/all/[email protected]/

Thanks
Muhammad

Quoted 21 lines · Ilpo Järvinen
Quoted 5 lines · Ilpo Järvinen
Quoted 3 lines · Muhammad Bilal

Hi Ilpo,

Quoted 1 line

Why use n variant?

Quoted 8 lines
Quoted 4 lines

strnlen() keeps the read within count, which is the size of buf as far as this helper knows, so it does not depend on the caller NUL terminating the buffer. Both current callers pass a kstrdup() copy, so strlen() works too. I can switch to it in v7 if you prefer.

As the contract here is the input must be a string, I don't see any need to limit the length with n variant so I do prefer strlen(). Also, we know that it should not be longer than count anyway.

I only now notice Andy's comments which seem better suggestion in general.

--
i.

#6 lore raw
In reply to Muhammad Bilal
Quoted 8 lines · Muhammad Bilal

The input is copied with kstrdup(), which stops at the first NUL, but the full write size is passed to hp_enforce_single_line_input(). With an embedded NUL the helper reads, and can write one byte, past the copy. Return -EINVAL if the input contains a NUL.

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.8): writing "A\0" followed by 4093 bytes of "B" to current_password 100 times returns -EINVAL every time, without reading into the heap.

...

drivers/platform/x86/hp/hp-bioscfg/bioscfg.c int hp_enforce_single_line_input(char *buf, size_t count) · line 171

Quoted 10 lines

{

drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +4 -0
 	char *p;
 
+	/* buf is a string, so a NUL before count is invalid input */
+	if (strnlen(buf, count) < count)
+		return -EINVAL;
+
 	p = memchr(buf, '\n', count);
 
 	if (p == buf + count - 1)

Let's look at the full implementation

int hp_enforce_single_line_input(char *buf, size_t count)

{
	char *p;

	p = memchr(buf, '\n', count);

	if (p == buf + count - 1)
		*p = '\0'; /* strip trailing newline */
	else if (p)
		return -EINVAL;  /* enforce single line input */

	return 0;
}

With your enforcement in place the rest can be simplified and refactored. The hint is to use strnchrnul().

int hp_enforce_single_line_input(char *buf, size_t count)

{
	char *p;

	p = strnchrnul(buf, count, '\n');

	if (p == buf + count - 1)
		*p = '\0'; /* strip trailing newline */
	else if (p < buf + count - 1)
		return -EINVAL;  /* enforce single line input */

	return 0;
}

(the above done with the less intrusive changes)

Signature · 2 lines
With Best Regards,
Andy Shevchenko