Skip to contentSkip to conversation

platform/x86: think-lmi: Return EINVAL when kbdlang gets set to a 0 length string

PATCH Superseded Hans de Goede 2 participants 5 messages Jun 21, 2021 x86 platform drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: think-lmi: Return EINVAL when kbdlang gets set to a 0 length string

Commit 0ddcf3a6b442 ("platform/x86: think-lmi: Avoid potential read before start of the buffer") moved the lengt == 0 up to before stripping the '\n' which typically gets added when users echo a value to a sysfs-attribute from the shell.

This avoids a potential buffer-underrun, but it also causes a behavioral change, prior to this change "echo > kbdlang", iow writing just a single '\n' would result in an EINVAL error, but after the change this gets accepted setting kbdlang to an empty string.

Re-add the length != 0 check after stripping the '\n' to reject this again, as before the change.

Fixes 0ddcf3a6b442 ("platform/x86: think-lmi: Avoid potential read before start of the buffer")
---
 drivers/platform/x86/think-lmi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/think-lmi.c +1 -1
index c6c9fbb8a53e..c22435acebbe 100644
@@ -449,7 +449,7 @@ static ssize_t kbdlang_store(struct kobject *kobj,
 	if (buf[length-1] == '\n')
 		length--;
 
-	if (length >= TLMI_LANG_MAXLEN)
+	if (!length || length >= TLMI_LANG_MAXLEN)
 		return -EINVAL;
 
 	memcpy(setting->kbdlang, buf, length);
Signature · 1 line
2.31.1
#1 lore raw
Quoted 3 lines · Hans de Goede

Commit 0ddcf3a6b442 ("platform/x86: think-lmi: Avoid potential read before start of the buffer") moved the lengt == 0 up to before stripping the '\n'

length

Quoted 7 lines

which typically gets added when users echo a value to a sysfs-attribute from the shell.

This avoids a potential buffer-underrun, but it also causes a behavioral change, prior to this change "echo > kbdlang", iow writing just a single '\n' would result in an EINVAL error, but after the change this gets accepted setting kbdlang to an empty string.

And why is it a problem?

I mean since we haven't yet released this in any of the kernels, the ABIU can be adjusted one way or another.

Quoted 2 lines

Re-add the length != 0 check after stripping the '\n' to reject this again, as before the change.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw

Hi,

Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Hans de Goede

Commit 0ddcf3a6b442 ("platform/x86: think-lmi: Avoid potential read before start of the buffer") moved the lengt == 0 up to before stripping the '\n'

length

Ack, will fix.

Quoted 9 lines
Quoted 7 lines

which typically gets added when users echo a value to a sysfs-attribute from the shell.

This avoids a potential buffer-underrun, but it also causes a behavioral change, prior to this change "echo > kbdlang", iow writing just a single '\n' would result in an EINVAL error, but after the change this gets accepted setting kbdlang to an empty string.

And why is it a problem?

Because there are only a couple of valid string like "de", "fr", "es" and "us". We don't know the exact set of valid strings for a certain BIOS, but we do not for sure that an empty string is not valid.

Regards,

Hans

#3 lore raw
Quoted 18 lines · Hans de Goede
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Hans de Goede

Commit 0ddcf3a6b442 ("platform/x86: think-lmi: Avoid potential read before start of the buffer") moved the lengt == 0 up to before stripping the '\n'

length

Ack, will fix.

Quoted 8 lines
Quoted 7 lines

which typically gets added when users echo a value to a sysfs-attribute from the shell.

This avoids a potential buffer-underrun, but it also causes a behavioral change, prior to this change "echo > kbdlang", iow writing just a single '\n' would result in an EINVAL error, but after the change this gets accepted setting kbdlang to an empty string.

Quoted 5 lines
Quoted 1 line

And why is it a problem?

Because there are only a couple of valid string like "de", "fr", "es" and "us". We don't know the exact set of valid strings for a certain BIOS, but we do not for sure that an empty string is not valid.

Since we call strlen() on the buf it means we are expecting NUL-terminated string no matter what.
In this case the

  p = skip_spaces(buf);
  length = strchrnul(buf, '\n') - p;
  if (!length || length >= ...)
    return ...

seems the best, no?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Hi,

Quoted 29 lines · Andy Shevchenko
Quoted 18 lines · Hans de Goede
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Hans de Goede

Commit 0ddcf3a6b442 ("platform/x86: think-lmi: Avoid potential read before start of the buffer") moved the lengt == 0 up to before stripping the '\n'

length

Ack, will fix.

Quoted 8 lines
Quoted 7 lines

which typically gets added when users echo a value to a sysfs-attribute from the shell.

This avoids a potential buffer-underrun, but it also causes a behavioral change, prior to this change "echo > kbdlang", iow writing just a single '\n' would result in an EINVAL error, but after the change this gets accepted setting kbdlang to an empty string.

Quoted 5 lines
Quoted 1 line

And why is it a problem?

Because there are only a couple of valid string like "de", "fr", "es" and "us". We don't know the exact set of valid strings for a certain BIOS, but we do not for sure that an empty string is not valid.

Since we call strlen() on the buf it means we are expecting NUL-terminated string no matter what.
In this case the

Quoted 6 lines
  p = skip_spaces(buf);
  length = strchrnul(buf, '\n') - p;
  if (!length || length >= ...)
    return ...

seems the best, no?

I don't see the need to skip any leading whitespace, we don't do that for any of the other attributes either, so that would be inconsistent, other then that I think using strchrnul to calc the length + skip the '\n' in one go is a good idea. let me send out a v2.

Regards,

Hans