platform/x86: hp-bioscfg: fix heap OOB in hp_enforce_single_line_input()
Subsystems (3)
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.
---
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
@@ -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
Ilpo Järvinen
maintainerQuoted 27 lines · Muhammad Bilal
The input is copied with
kstrdup(), which stops at the first NUL, but the full write size is passed tohp_enforce_single_line_input(). With an embedded NUL the helper reads, and can write one byte, past the copy. Return-EINVALif 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_password100 times returns-EINVALevery time, without reading into the heap.Fixes a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")Suggested-by Ilpo Järvinen <[email protected]>Signed-off-by Muhammad Bilal <[email protected]>---
Changes in v6:
- Return-EINVALon 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
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.
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 tohp_enforce_single_line_input(). With an embedded NUL the helper reads, and can write one byte, past the copy. Return-EINVALif 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_password100 times returns-EINVALevery time, without reading into the heap.Fixes a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")Suggested-by Ilpo Järvinen <[email protected]>Signed-off-by Muhammad Bilal <[email protected]>---
Changes in v6:
- Return-EINVALon 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
-0drivers/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.
Quoted 8 lines · Muhammad Bilal
The input is copied with
kstrdup(), which stops at the first NUL, but the full write size is passed tohp_enforce_single_line_input(). With an embedded NUL the helper reads, and can write one byte, past the copy. Return-EINVALif 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_password100 times returns-EINVALevery 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
-0char *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
Ilpo Järvinen
maintainerQuoted 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 akstrdup()copy, sostrlen()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
MuhammadQuoted 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 tohp_enforce_single_line_input(). With an embedded NUL the helper reads, and can write one byte, past the copy. Return-EINVALif 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_password100 times returns-EINVALevery time, without reading into the heap.Fixes a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")Suggested-by Ilpo Järvinen <[email protected]>Signed-off-by Muhammad Bilal <[email protected]>---
Changes in v6:
- Return-EINVALon 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
-0drivers/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.
Ilpo Järvinen
maintainerQuoted 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 akstrdup()copy, sostrlen()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.
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 akstrdup()copy, sostrlen()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.