soc: qcom: apr: Avoid string overflow
authorNiklas Cassel <niklas.cassel@linaro.org>
Wed, 29 Aug 2018 07:57:22 +0000 (09:57 +0200)
committerAndy Gross <andy.gross@linaro.org>
Thu, 13 Sep 2018 21:11:37 +0000 (16:11 -0500)
'adev->name' is used as a NUL-terminated string, but using strncpy() with the
length equal to the buffer size may result in lack of the termination:

In function 'apr_add_device',
    inlined from 'of_register_apr_devices' at drivers//soc/qcom/apr.c:264:7,
    inlined from 'apr_probe' at drivers//soc/qcom/apr.c:290:2:
drivers//soc/qcom/apr.c:222:3: warning: 'strncpy' specified bound 32 equals destination size [-Wstringop-truncation]
   strncpy(adev->name, np->name, APR_NAME_SIZE);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This changes it to use the safer strscpy() instead.

Signed-off-by: Niklas Cassel <niklas.cassel@linaro.org>
Reviewed-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Signed-off-by: Andy Gross <andy.gross@linaro.org>
drivers/soc/qcom/apr.c

index 7f8c4c0..716762d 100644 (file)
@@ -219,9 +219,9 @@ static int apr_add_device(struct device *dev, struct device_node *np,
        adev->domain_id = id->domain_id;
        adev->version = id->svc_version;
        if (np)
-               strncpy(adev->name, np->name, APR_NAME_SIZE);
+               strscpy(adev->name, np->name, APR_NAME_SIZE);
        else
-               strncpy(adev->name, id->name, APR_NAME_SIZE);
+               strscpy(adev->name, id->name, APR_NAME_SIZE);
 
        dev_set_name(&adev->dev, "aprsvc:%s:%x:%x", adev->name,
                     id->domain_id, id->svc_id);