Skip to contentSkip to conversation

platform/x86: dell-wmi-sysman: Clean up security buffer helpers

PATCH Superseded Thorsten Blum 2 participants 4 messages Mar 9 – Mar 31

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: dell-wmi-sysman: Clean up security buffer helpers

In calculate_security_buffer(), call strlen() once and use ALIGN() to round up to an even size.

In populate_security_buffer(), also avoid recomputing strlen(), rename the u32 pointer from 'seclen' to 'seclenp' to avoid confusion with the new length variable, and drop the memcpy() guard since calling it with size 0 is a no-op and therefore safe.

Use 'const char *' for the read-only source string in both helpers.

---
 .../dell/dell-wmi-sysman/dell-wmi-sysman.h    |  4 ++--
 .../x86/dell/dell-wmi-sysman/sysman.c         | 20 ++++++++-----------
 2 files changed, 10 insertions(+), 14 deletions(-)
drivers/platform/x86/dell/dell-wmi-sysman/dell-wmi-sysman.h +2 -2
index 817ee7ba07ca..5278a93fdaf7 100644
@@ -189,8 +189,8 @@ void exit_bios_attr_set_interface(void);
 int init_bios_attr_set_interface(void);
 int map_wmi_error(int error_code);
 size_t calculate_string_buffer(const char *str);
-size_t calculate_security_buffer(char *authentication);
-void populate_security_buffer(char *buffer, char *authentication);
+size_t calculate_security_buffer(const char *authentication);
+void populate_security_buffer(char *buffer, const char *authentication);
 ssize_t populate_string_buffer(char *buffer, size_t buffer_len, const char *str);
 int set_new_password(const char *password_type, const char *new);
 int init_bios_attr_pass_interface(void);
drivers/platform/x86/dell/dell-wmi-sysman/sysman.c +8 -12
index 9dddab6c9397..aba9ef2cfc03 100644
@@ -72,13 +72,9 @@ size_t calculate_string_buffer(const char *str)
  *
  * Currently only supported type is Admin password
  */
-size_t calculate_security_buffer(char *authentication)
+size_t calculate_security_buffer(const char *authentication)
 {
-	if (strlen(authentication) > 0) {
-		return (sizeof(u32) * 2) + strlen(authentication) +
-			strlen(authentication) % 2;
-	}
-	return sizeof(u32) * 2;
+	return sizeof(u32) * 2 + ALIGN(strlen(authentication), 2);
 }
 
 /**
@@ -88,18 +84,18 @@ size_t calculate_security_buffer(char *authentication)
  *
  * Currently only supported type is PLAIN TEXT
  */
-void populate_security_buffer(char *buffer, char *authentication)
+void populate_security_buffer(char *buffer, const char *authentication)
 {
+	size_t seclen = strlen(authentication);
 	char *auth = buffer + sizeof(u32) * 2;
 	u32 *sectype = (u32 *) buffer;
-	u32 *seclen = sectype + 1;
+	u32 *seclenp = sectype + 1;
 
-	*sectype = strlen(authentication) > 0 ? 1 : 0;
-	*seclen = strlen(authentication);
+	*sectype = !!seclen;
+	*seclenp = seclen;
 
 	/* plain text */
-	if (strlen(authentication) > 0)
-		memcpy(auth, authentication, *seclen);
+	memcpy(auth, authentication, seclen);
 }
 
 /**
maintainer
#1 lore raw
Quoted 38 lines · Thorsten Blum

In calculate_security_buffer(), call strlen() once and use ALIGN() to round up to an even size.

In populate_security_buffer(), also avoid recomputing strlen(), rename the u32 pointer from 'seclen' to 'seclenp' to avoid confusion with the new length variable, and drop the memcpy() guard since calling it with size 0 is a no-op and therefore safe.

Use 'const char *' for the read-only source string in both helpers.

---
 .../dell/dell-wmi-sysman/dell-wmi-sysman.h    |  4 ++--
 .../x86/dell/dell-wmi-sysman/sysman.c         | 20 ++++++++-----------
 2 files changed, 10 insertions(+), 14 deletions(-)
drivers/platform/x86/dell/dell-wmi-sysman/dell-wmi-sysman.h +2 -2
index 817ee7ba07ca..5278a93fdaf7 100644
@@ -189,8 +189,8 @@ void exit_bios_attr_set_interface(void);
 int init_bios_attr_set_interface(void);
 int map_wmi_error(int error_code);
 size_t calculate_string_buffer(const char *str);
-size_t calculate_security_buffer(char *authentication);
-void populate_security_buffer(char *buffer, char *authentication);
+size_t calculate_security_buffer(const char *authentication);
+void populate_security_buffer(char *buffer, const char *authentication);
 ssize_t populate_string_buffer(char *buffer, size_t buffer_len, const char *str);
 int set_new_password(const char *password_type, const char *new);
 int init_bios_attr_pass_interface(void);
drivers/platform/x86/dell/dell-wmi-sysman/sysman.c +0 -0
index 9dddab6c9397..aba9ef2cfc03 100644
@@ -72,13 +72,9 @@ size_t calculate_string_buffer(const char *str)
  *
  * Currently only supported type is Admin password

drivers/platform/x86/dell/dell-wmi-sysman/sysman.c size_t calculate_string_buffer(const char *str) · line 77

Quoted 10 lines
drivers/platform/x86/dell/dell-wmi-sysman/sysman.c +2 -6
  */
-size_t calculate_security_buffer(char *authentication)
+size_t calculate_security_buffer(const char *authentication)
 {
-	if (strlen(authentication) > 0) {
-		return (sizeof(u32) * 2) + strlen(authentication) +
-			strlen(authentication) % 2;
-	}
-	return sizeof(u32) * 2;
+	return sizeof(u32) * 2 + ALIGN(strlen(authentication), 2);

You need to add an include for ALIGN().

Also header for strlen() is missing (a pre-existing problem).

Quoted 14 lines
drivers/platform/x86/dell/dell-wmi-sysman/sysman.c +2 -2
 }
 
 /**
@@ -88,18 +84,18 @@ size_t calculate_security_buffer(char *authentication)
  *
  * Currently only supported type is PLAIN TEXT
  */
-void populate_security_buffer(char *buffer, char *authentication)
+void populate_security_buffer(char *buffer, const char *authentication)
 {
+	size_t seclen = strlen(authentication);
 	char *auth = buffer + sizeof(u32) * 2;
 	u32 *sectype = (u32 *) buffer;
-	u32 *seclen = sectype + 1;

drivers/platform/x86/dell/dell-wmi-sysman/sysman.c size_t calculate_security_buffer(char *authentication) · line 97

Quoted 10 lines
drivers/platform/x86/dell/dell-wmi-sysman/sysman.c +4 -4
+	u32 *seclenp = sectype + 1;
-	*sectype = strlen(authentication) > 0 ? 1 : 0;
-	*seclen = strlen(authentication);
+	*sectype = !!seclen;
+	*seclenp = seclen;
 
 	/* plain text */
-	if (strlen(authentication) > 0)
-		memcpy(auth, authentication, *seclen);
+	memcpy(auth, authentication, seclen);

Logicwise this change seem fine to me but I wonder why is buffer char * (again, this is a pre-existing problem). It seems u8 * would be more appropriate given how it's handled here as something else than characters.

Also, you don't need resend if your patch is still listed in the patchwork, it's not lost as long as it remains in patchwork and I'll eventually get to it.

Signature · 1 line
 i.
#2 lore raw
Quoted 17 lines · Ilpo Järvinen
Quoted 16 lines · Thorsten Blum

In calculate_security_buffer(), call strlen() once and use ALIGN() to round up to an even size.

In populate_security_buffer(), also avoid recomputing strlen(), rename the u32 pointer from 'seclen' to 'seclenp' to avoid confusion with the new length variable, and drop the memcpy() guard since calling it with size 0 is a no-op and therefore safe.

Use 'const char *' for the read-only source string in both helpers.

---
 .../dell/dell-wmi-sysman/dell-wmi-sysman.h    |  4 ++--
 .../x86/dell/dell-wmi-sysman/sysman.c         | 20 ++++++++-----------
 2 files changed, 10 insertions(+), 14 deletions(-)
Quoted 5 lines
Quoted 1 line

[...]

Logicwise this change seem fine to me but I wonder why is buffer char * (again, this is a pre-existing problem). It seems u8 * would be more appropriate given how it's handled here as something else than characters.

Yes, I agree. However, using u8 * would also require changing the call sites in biosattr-interface.c and passwordattr-interface.c. Happy to change it, but it would probably be better as a follow-up patch.

Quoted 3 lines

Also, you don't need resend if your patch is still listed in the patchwork, it's not lost as long as it remains in patchwork and I'll eventually get to it.

I wasn't aware of the patchwork instance - is it this one?

https://patchwork.kernel.org/project/platform-driver-x86/list/

I see 67 patches in total, but this one is not part of the list, afaict.

Thanks,
Thorsten

maintainer
#3 lore raw
Quoted 20 lines · Thorsten Blum
Quoted 19 lines · Ilpo Järvinen
Quoted 17 lines · Thorsten Blum

In calculate_security_buffer(), call strlen() once and use ALIGN() to round up to an even size.

In populate_security_buffer(), also avoid recomputing strlen(), rename the u32 pointer from 'seclen' to 'seclenp' to avoid confusion with the new length variable, and drop the memcpy() guard since calling it with size 0 is a no-op and therefore safe.

Use 'const char *' for the read-only source string in both helpers.

---
 .../dell/dell-wmi-sysman/dell-wmi-sysman.h    |  4 ++--
 .../x86/dell/dell-wmi-sysman/sysman.c         | 20 ++++++++-----------
 2 files changed, 10 insertions(+), 14 deletions(-)

[...]

Quoted 7 lines
Quoted 3 lines

Logicwise this change seem fine to me but I wonder why is buffer char * (again, this is a pre-existing problem). It seems u8 * would be more appropriate given how it's handled here as something else than characters.

Yes, I agree. However, using u8 * would also require changing the call sites in biosattr-interface.c and passwordattr-interface.c. Happy to change it, but it would probably be better as a follow-up patch.

A follow-up patch is fine.

Quoted 7 lines
Quoted 3 lines

Also, you don't need resend if your patch is still listed in the patchwork, it's not lost as long as it remains in patchwork and I'll eventually get to it.

I wasn't aware of the patchwork instance - is it this one?

https://patchwork.kernel.org/project/platform-driver-x86/list/

Correct.

You can find that URL from MAINTAINERS.

Quoted 1 line

I see 67 patches in total, but this one is not part of the list, afaict.

It was there until today but I marked it changes required after noting it lacks the header so I'm expecting a new version.

Signature · 1 line
 i.