Skip to content

Commit 71d69e8

Browse files
committed
platform/x86: think-lmi: Return EINVAL when kbdlang gets set to a 0 length string
Commit 0ddcf3a ("platform/x86: think-lmi: Avoid potential read before start of the buffer") moved the length == 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. Fix this by replacing the manual '\n' check with using strchrnul() to get the length till '\n' or terminating 0 in one go; and then do the length != 0 check after this. Fixes: 0ddcf3a ("platform/x86: think-lmi: Avoid potential read before start of the buffer") Reported-by: Juha Leppänen <[email protected]> Suggested-by: Andy Shevchenko <[email protected]> Signed-off-by: Hans de Goede <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 72fbcac commit 71d69e8

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

drivers/platform/x86/think-lmi.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,9 @@ static ssize_t kbdlang_store(struct kobject *kobj,
442442
struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
443443
int length;
444444

445-
length = strlen(buf);
446-
if (!length)
447-
return -EINVAL;
448-
449-
if (buf[length-1] == '\n')
450-
length--;
451-
452-
if (length >= TLMI_LANG_MAXLEN)
445+
/* Calculate length till '\n' or terminating 0 */
446+
length = strchrnul(buf, '\n') - buf;
447+
if (!length || length >= TLMI_LANG_MAXLEN)
453448
return -EINVAL;
454449

455450
memcpy(setting->kbdlang, buf, length);

0 commit comments

Comments
 (0)