Skip to contentSkip to conversation

platform/x86: hp-bioscfg: string buffer and password fixes

PATCH Superseded Muhammad Bilal 2 participants 7 messages Sep 28

Subsystems (3)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v2 cover platform/x86: hp-bioscfg: string buffer and password fixes

This series addresses string buffer issues and password validation in the hp-bioscfg driver.

Patch 1 fixes bounds checking, prescan OOB read, and character count accounting in hp_get_string_from_buffer().

Patch 2 replaces the truncating manual conversion loop with a two-stage
conversion: utf16s_to_utf8s() into a scratch buffer followed by string_escape_mem().

Patch 3 fixes validate_password_input() returning positive INVALID_BIOS_AUTH (0x0E), which caused userspace writes of out-of-range passwords to appear successful.

All patches tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.7).

Changes in v2:
- Split hp_get_string_from_buffer() fixes into two logical patches (bounds/OOB reads and UTF-8 conversion rewrite), as reviewed by Ilpo Järvinen.
- Dropped lib/string_helpers patch: pass "\\\r\n\t" to string_escape_mem() with existing flags (ESCAPE_SPACE | ESCAPE_SPECIAL) to escape the four characters without altering quotes, as reviewed by Andy Shevchenko.
- Added password validation errno fix as patch 3/3.

Muhammad Bilal (3):
platform/x86: hp-bioscfg: fix OOB reads in hp_get_string_from_buffer() platform/x86: hp-bioscfg: fix non-ASCII truncation in
hp_get_string_from_buffer()
platform/x86: hp-bioscfg: return -ERANGE from
validate_password_input()

 drivers/platform/x86/hp/hp-bioscfg/bioscfg.c  | 77 ++++++-------------
 .../x86/hp/hp-bioscfg/passwdobj-attributes.c  |  6 +-
 2 files changed, 27 insertions(+), 56 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/3 platform/x86: hp-bioscfg: fix OOB reads in hp_get_string_from_buffer()

The escape-prescan loop uses 'size' as both loop bound and accumulator, so each match extends the bound and reads past src[]:

  for (i = 0; i < size; i++)
      if (src[i] == '\\' || ...) size++;

Snapshot the real char count into orig_size and loop against that.

Also fix two adjacent issues: the bounds check omits the 2-byte length prefix already consumed, and utf16s_to_utf8s() receives the byte count (src_size) instead of the u16-unit count (orig_size).

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.7).

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

---
Changes in v2:
- Split bounds and prescan OOB fix into a separate patch, as reviewed by Ilpo Järvinen.

 drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 24 ++++++++++++--------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +14 -10
index 32b99a862082..ab1eff1deca1 100644
@@ -60,6 +60,7 @@ int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_
 	u16 *src = (u16 *)*buffer;
 	u16 src_size;
 
+	u16 orig_size;
 	u16 size;
 	int i;
 	int conv_dst_size;
@@ -67,17 +68,20 @@ int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_
 	if (*buffer_size < sizeof(u16))
 		return -EINVAL;
 
-	src_size = *(src++);
-	/* size value in u16 chars */
-	size = src_size / sizeof(u16);
+	src_size = *src;
 
-	/* Ensure there is enough space remaining to read and convert
-	 * the string
+	/* Ensure there is enough space remaining for the length prefix
+	 * just read plus the string data it describes.
 	 */
-	if (*buffer_size < src_size)
+	if (*buffer_size < sizeof(u16) + src_size)
 		return -EINVAL;
 
-	for (i = 0; i < size; i++)
+	src++;
+	/* size value in u16 chars */
+	orig_size = src_size / sizeof(u16);
+	size = orig_size;
+
+	for (i = 0; i < orig_size; i++)
 		if (src[i] == '\\' ||
 		    src[i] == '\r' ||
 		    src[i] == '\n' ||
@@ -95,7 +99,7 @@ int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_
 	/*
 	 * convert from UTF-16 unicode to ASCII
 	 */
-	utf16s_to_utf8s(src, src_size, UTF16_HOST_ENDIAN, dst, conv_dst_size);
+	utf16s_to_utf8s(src, orig_size, UTF16_HOST_ENDIAN, dst, conv_dst_size);
 	dst[conv_dst_size] = 0;
 
 	for (i = 0; i < conv_dst_size; i++) {
@@ -121,8 +125,8 @@ int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_
 		src++;
 	}
 
-	*buffer = (u8 *)src;
-	*buffer_size -= size * sizeof(u16);
+	*buffer += sizeof(u16) + src_size;
+	*buffer_size -= sizeof(u16) + src_size;
 
 	return size;
 }
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v2 1/3] platform/x86: hp-bioscfg: fix OOB reads in hp_get_string_from_buffer()

Quoted 14 lines · Muhammad Bilal

The escape-prescan loop uses 'size' as both loop bound and accumulator, so each match extends the bound and reads past src[]:

  for (i = 0; i < size; i++)
      if (src[i] == '\\' || ...) size++;

Snapshot the real char count into orig_size and loop against that.

Also fix two adjacent issues: the bounds check omits the 2-byte length prefix already consumed, and utf16s_to_utf8s() receives the byte count (src_size) instead of the u16-unit count (orig_size).

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.7).

...

Quoted 6 lines

int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_

        u16 *src = (u16 *)*buffer;
        u16 src_size;

While at it, drop this blank line.

Quoted 4 lines
+       u16 orig_size;
        u16 size;
        int i;
        int conv_dst_size;

--
With Best Regards,
Andy Shevchenko

#3 lore raw

PATCH v2 2/3 platform/x86: hp-bioscfg: fix non-ASCII truncation in hp_get_string_from_buffer()

In reply to Muhammad Bilal

utf16s_to_utf8s() writes into dst, but the manual loop that follows re-walks src from the start and overwrites dst with truncating casts (dst[i] = *src), discarding the UTF-8 output. Any code point above U+007F produces garbage.

Replace both with a two-stage conversion: utf16s_to_utf8s() into a scratch buffer, then string_escape_mem(ESCAPE_SPACE | ESCAPE_SPECIAL, "\\\r\n\t") into dst. Passing the four characters to @only avoids escaping quotes, leaving them for the existing strreplace() call.

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.7).

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

---
Changes in v2:
- Split from the combined string buffer fix into its own patch, as reviewed by Ilpo Järvinen.
- Drop lib/string_helpers patch: pass "\\\r\n\t" to string_escape_mem() with existing flags (ESCAPE_SPACE | ESCAPE_SPECIAL) to escape the four characters without modifying quotes, as reviewed by Andy Shevchenko.

 drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 61 +++++---------------
 1 file changed, 14 insertions(+), 47 deletions(-)
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +14 -47
index ab1eff1deca1..fd3d54ee3a94 100644
@@ -12,6 +12,7 @@
 #include <linux/kernel.h>
 #include <linux/printk.h>
 #include <linux/string.h>
+#include <linux/string_helpers.h>
 #include <linux/wmi.h>
 #include "bioscfg.h"
 #include "../../firmware_attributes_class.h"
@@ -60,10 +61,10 @@ int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_
 	u16 *src = (u16 *)*buffer;
 	u16 src_size;
 
+	char utf8_buf[MAX_BUFF_SIZE];
 	u16 orig_size;
-	u16 size;
-	int i;
-	int conv_dst_size;
+	int utf8_len;
+	int escaped_len;
 
 	if (*buffer_size < sizeof(u16))
 		return -EINVAL;
@@ -79,56 +80,22 @@ int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_
 	src++;
 	/* size value in u16 chars */
 	orig_size = src_size / sizeof(u16);
-	size = orig_size;
-
-	for (i = 0; i < orig_size; i++)
-		if (src[i] == '\\' ||
-		    src[i] == '\r' ||
-		    src[i] == '\n' ||
-		    src[i] == '\t')
-			size++;
 
-	/*
-	 * Conversion is limited to destination string max number of
-	 * bytes.
-	 */
-	conv_dst_size = size;
-	if (size >= dst_size)
-		conv_dst_size = dst_size - 1;
+	utf8_len = utf16s_to_utf8s(src, orig_size, UTF16_HOST_ENDIAN,
+				   utf8_buf, sizeof(utf8_buf));
 
-	/*
-	 * convert from UTF-16 unicode to ASCII
-	 */
-	utf16s_to_utf8s(src, orig_size, UTF16_HOST_ENDIAN, dst, conv_dst_size);
-	dst[conv_dst_size] = 0;
-
-	for (i = 0; i < conv_dst_size; i++) {
-		if (*src == '\\' ||
-		    *src == '\r' ||
-		    *src == '\n' ||
-		    *src == '\t') {
-			dst[i++] = '\\';
-			if (i == conv_dst_size)
-				break;
-		}
-
-		if (*src == '\r')
-			dst[i] = 'r';
-		else if (*src == '\n')
-			dst[i] = 'n';
-		else if (*src == '\t')
-			dst[i] = 't';
-		else if (*src == '"')
-			dst[i] = '\'';
-		else
-			dst[i] = *src;
-		src++;
-	}
+	escaped_len = string_escape_mem(utf8_buf, utf8_len, dst, dst_size - 1,
+					ESCAPE_SPACE | ESCAPE_SPECIAL,
+					"\\\r\n\t");
+	if (escaped_len > dst_size - 1)
+		escaped_len = dst_size - 1;
+	dst[escaped_len] = '\0';
+	strreplace(dst, '"', '\'');
 
 	*buffer += sizeof(u16) + src_size;
 	*buffer_size -= sizeof(u16) + src_size;
 
-	return size;
+	return escaped_len;
 }
 
 int hp_get_common_data_from_buffer(u8 **buffer_ptr, u32 *buffer_size,
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH v2 2/3] platform/x86: hp-bioscfg: fix non-ASCII truncation in hp_get_string_from_buffer()

Quoted 11 lines · Muhammad Bilal

utf16s_to_utf8s() writes into dst, but the manual loop that follows re-walks src from the start and overwrites dst with truncating casts (dst[i] = *src), discarding the UTF-8 output. Any code point above U+007F produces garbage.

Replace both with a two-stage conversion: utf16s_to_utf8s() into a scratch buffer, then string_escape_mem(ESCAPE_SPACE | ESCAPE_SPECIAL, "\\\r\n\t") into dst. Passing the four characters to @only avoids escaping quotes, leaving them for the existing strreplace() call.

Quoted 5 lines

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.7).

Wrong tag. I have not suggested the (main) idea behind this change. You're not supposed to arbitrarily add some tags without respective consent.

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

...

Quoted 2 lines

#include <linux/string.h>
+#include <linux/string_helpers.h>

You can replace string.h with string_helpers.h as the latter implies the former.

...

Quoted 4 lines
        u16 *src = (u16 *)*buffer;
        u16 src_size;

+ char utf8_buf[MAX_BUFF_SIZE];

Try to preserve reversed xmas tree order.

Quoted 6 lines
        u16 orig_size;
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c +2 -3
-       u16 size;
-       int i;
-       int conv_dst_size;
+       int utf8_len;
+       int escaped_len;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

PATCH v2 3/3 platform/x86: hp-bioscfg: return -ERANGE from validate_password_input()

In reply to Muhammad Bilal

validate_password_input() returns INVALID_BIOS_AUTH (0x0E, positive) on rejection. store_password_instance() skips the store on nonzero ret, but its final "return ret < 0 ? ret : count" treats the positive value as success, so userspace sees count instead of an error.

Return -ERANGE, matching the sibling validators for string, enumeration, and integer attribute types.

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.7): writing 999 bytes to current_password returned 999 with no error before this fix.

Fixes 8646a3b5ee3a ("platform/x86: hp-bioscfg: passwdobj-attributes")

---
Changes in v2:
- New patch in this series.

 drivers/platform/x86/hp/hp-bioscfg/passwdobj-attributes.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/platform/x86/hp/hp-bioscfg/passwdobj-attributes.c +3 -3
index 9b989ef756ea..b1c687fb4700 100644
@@ -70,12 +70,12 @@ static int validate_password_input(int instance_id, const char *buf)
 		length--;
 
 	if (length > MAX_PASSWD_SIZE)
-		return INVALID_BIOS_AUTH;
+		return -ERANGE;
 
 	if (password_data->min_password_length > length ||
 	    password_data->max_password_length < length)
-		return INVALID_BIOS_AUTH;
-	return SUCCESS;
+		return -ERANGE;
+	return 0;
 }
 
 ATTRIBUTE_N_PROPERTY_SHOW(is_enabled, password);
Signature · 1 line
2.55.0
#6 lore raw

Re: [PATCH v2 3/3] platform/x86: hp-bioscfg: return -ERANGE from validate_password_input()

Quoted 11 lines · Muhammad Bilal

validate_password_input() returns INVALID_BIOS_AUTH (0x0E, positive) on rejection. store_password_instance() skips the store on nonzero ret, but its final "return ret < 0 ? ret : count" treats the positive value as success, so userspace sees count instead of an error.

Return -ERANGE, matching the sibling validators for string, enumeration, and integer attribute types.

Tested on HP EliteBook 840 G2 (Fedora 44, Linux 7.2.7): writing 999 bytes to current_password returned 999 with no error before this fix.

...

Review

Quoted 3 lines

if (length > MAX_PASSWD_SIZE)

(quoted diff) +1 -1
-               return INVALID_BIOS_AUTH;
+               return -ERANGE;

Nope, ERANGE is for integer values. Here is definitely about the size of the input string. We have E2BIG for such cases.

drivers/platform/x86/hp/hp-bioscfg/passwdobj-attributes.c static int validate_password_input(int instance_id, const char *buf) · line 77

Quoted 5 lines
        if (password_data->min_password_length > length ||
            password_data->max_password_length < length)
drivers/platform/x86/hp/hp-bioscfg/passwdobj-attributes.c +1 -2
-               return INVALID_BIOS_AUTH;
-       return SUCCESS;
+               return -ERANGE;

Ditto.

Quoted 2 lines

+ return 0;
}

Signature · 2 lines
With Best Regards,
Andy Shevchenko