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

eeprom: idt_89hpesx: replace open-coded kmemdup_nul

A malloc + strncpy + manual NUL_termination is just kmemdup_nul. Let's use
this interface as it is less error-prone and more readable.

Also drop `csraddr_len` as it is just used in a single place and we can
just do the arithmetic in-line.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20230927-strncpy-drivers-misc-eeprom-idt_89hpesx-c-v1-1-08e3d45b8c05@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Justin Stitt and committed by
Greg Kroah-Hartman
bcdf91c9 78510a4d

+3 -8
+3 -8
drivers/misc/eeprom/idt_89hpesx.c
··· 905 905 { 906 906 struct idt_89hpesx_dev *pdev = filep->private_data; 907 907 char *colon_ch, *csraddr_str, *csrval_str; 908 - int ret, csraddr_len; 908 + int ret; 909 909 u32 csraddr, csrval; 910 910 char *buf; 911 911 ··· 927 927 * no new CSR value 928 928 */ 929 929 if (colon_ch != NULL) { 930 - csraddr_len = colon_ch - buf; 931 - csraddr_str = 932 - kmalloc(csraddr_len + 1, GFP_KERNEL); 930 + /* Copy the register address to the substring buffer */ 931 + csraddr_str = kmemdup_nul(buf, colon_ch - buf, GFP_KERNEL); 933 932 if (csraddr_str == NULL) { 934 933 ret = -ENOMEM; 935 934 goto free_buf; 936 935 } 937 - /* Copy the register address to the substring buffer */ 938 - strncpy(csraddr_str, buf, csraddr_len); 939 - csraddr_str[csraddr_len] = '\0'; 940 936 /* Register value must follow the colon */ 941 937 csrval_str = colon_ch + 1; 942 938 } else /* if (str_colon == NULL) */ { 943 939 csraddr_str = (char *)buf; /* Just to shut warning up */ 944 - csraddr_len = strnlen(csraddr_str, count); 945 940 csrval_str = NULL; 946 941 } 947 942