Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

accessibility: speakup: refactor deprecated strncpy

`strncpy` is deprecated for use on NUL-terminated destination strings [1].

Let's refactor this function to just use synth_write().

Link: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings[1]
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
Suggested-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Tested-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Link: https://lore.kernel.org/r/20230918-strncpy-drivers-accessibility-speakup-kobjects-c-v2-1-d5b1976c5dbf@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Justin Stitt and committed by
Greg Kroah-Hartman
19e3e6cd 2953fa03

+11 -14
+11 -14
drivers/accessibility/speakup/kobjects.c
··· 413 413 struct kobj_attribute *attr, 414 414 const char *buf, size_t count) 415 415 { 416 - u_char tmp[256]; 417 - int len; 418 - int bytes; 419 - const char *ptr = buf; 416 + char *unescaped; 420 417 unsigned long flags; 421 418 422 419 if (!synth) 423 420 return -EPERM; 424 421 425 - len = strlen(buf); 422 + unescaped = kstrdup(buf, GFP_KERNEL); 423 + if (!unescaped) 424 + return -ENOMEM; 425 + 426 + string_unescape_any_inplace(unescaped); 427 + 426 428 spin_lock_irqsave(&speakup_info.spinlock, flags); 427 - while (len > 0) { 428 - bytes = min_t(size_t, len, 250); 429 - strncpy(tmp, ptr, bytes); 430 - tmp[bytes] = '\0'; 431 - string_unescape_any_inplace(tmp); 432 - synth_printf("%s", tmp); 433 - ptr += bytes; 434 - len -= bytes; 435 - } 429 + synth_write(unescaped, strlen(unescaped)); 436 430 spin_unlock_irqrestore(&speakup_info.spinlock, flags); 431 + 432 + kfree(unescaped); 433 + 437 434 return count; 438 435 } 439 436