isdn: kcapi: replace deprecated strncpy with strscpy_pad
authorJustin Stitt <justinstitt@google.com>
Fri, 22 Sep 2023 11:49:14 +0000 (11:49 +0000)
committerKees Cook <keescook@chromium.org>
Mon, 2 Oct 2023 18:07:02 +0000 (11:07 -0700)
`strncpy` is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

`buf` is used in this context as a data buffer with 64 bytes of memory
to be occupied by capi_manufakturer.

We see the caller capi20_get_manufacturer() passes data.manufacturer as
its `buf` argument which is then later passed over to user space. Due to
this, let's keep the NUL-padding that strncpy provided by using
strscpy_pad so as to not leak any stack data.
|  cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
|  if (cdev->errcode)
|  return -EIO;
|
|  if (copy_to_user(argp, data.manufacturer,
|   sizeof(data.manufacturer)))
|  return -EFAULT;

Perhaps this would also be a good instance to use `strtomem_pad` for but
in my testing the compiler was not able to determine the size of `buf`
-- even with all the hints.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20230922-strncpy-drivers-isdn-capi-kcapi-c-v1-1-55fcf8b075fb@google.com
Signed-off-by: Kees Cook <keescook@chromium.org>
drivers/isdn/capi/kcapi.c

index ae24848af2332456289a7fe261eef53f674262c1..136ba9fe55e0808c508d3e5d0f69edb58fe48a1f 100644 (file)
@@ -732,7 +732,7 @@ u16 capi20_get_manufacturer(u32 contr, u8 buf[CAPI_MANUFACTURER_LEN])
        u16 ret;
 
        if (contr == 0) {
-               strncpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
+               strscpy_pad(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
                return CAPI_NOERROR;
        }
 
@@ -740,7 +740,7 @@ u16 capi20_get_manufacturer(u32 contr, u8 buf[CAPI_MANUFACTURER_LEN])
 
        ctr = get_capi_ctr_by_nr(contr);
        if (ctr && ctr->state == CAPI_CTR_RUNNING) {
-               strncpy(buf, ctr->manu, CAPI_MANUFACTURER_LEN);
+               strscpy_pad(buf, ctr->manu, CAPI_MANUFACTURER_LEN);
                ret = CAPI_NOERROR;
        } else
                ret = CAPI_REGNOTINSTALLED;