* [PATCH v2] edac: edac_mc_sysfs.c: Use strlcpy to Copy a C-string into a sized buffer
@ 2017-05-26 19:10 Karim Eshapa
2017-05-28 18:12 ` Borislav Petkov
0 siblings, 1 reply; 2+ messages in thread
From: Karim Eshapa @ 2017-05-26 19:10 UTC (permalink / raw)
To: bp; +Cc: mchehab, linux-edac, linux-kernel, Karim Eshapa
Use strlcpy with sized buffer instead of strncpy to
avoid non NUL-terminated source strings problems.
Signed-off-by: Karim Eshapa <karim.eshapa@gmail.com>
---
drivers/edac/edac_mc_sysfs.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/drivers/edac/edac_mc_sysfs.c b/drivers/edac/edac_mc_sysfs.c
index 445862d..e893b94 100644
--- a/drivers/edac/edac_mc_sysfs.c
+++ b/drivers/edac/edac_mc_sysfs.c
@@ -251,8 +251,7 @@ static ssize_t channel_dimm_label_store(struct device *dev,
if (copy_count == 0 || copy_count >= sizeof(rank->dimm->label))
return -EINVAL;
- strncpy(rank->dimm->label, data, copy_count);
- rank->dimm->label[copy_count] = '\0';
+ strlcpy(rank->dimm->label, data, copy_count);
return count;
}
@@ -530,8 +529,7 @@ static ssize_t dimmdev_label_store(struct device *dev,
if (copy_count == 0 || copy_count >= sizeof(dimm->label))
return -EINVAL;
- strncpy(dimm->label, data, copy_count);
- dimm->label[copy_count] = '\0';
+ strlcpy(dimm->label, data, copy_count);
return count;
}
--
2.7.4
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] edac: edac_mc_sysfs.c: Use strlcpy to Copy a C-string into a sized buffer
2017-05-26 19:10 [PATCH v2] edac: edac_mc_sysfs.c: Use strlcpy to Copy a C-string into a sized buffer Karim Eshapa
@ 2017-05-28 18:12 ` Borislav Petkov
0 siblings, 0 replies; 2+ messages in thread
From: Borislav Petkov @ 2017-05-28 18:12 UTC (permalink / raw)
To: Karim Eshapa; +Cc: mchehab, linux-edac, linux-kernel
On Fri, May 26, 2017 at 09:10:21PM +0200, Karim Eshapa wrote:
> Use strlcpy with sized buffer instead of strncpy to
> avoid non NUL-terminated source strings problems.
Huh? But it does get terminated.
> Signed-off-by: Karim Eshapa <karim.eshapa@gmail.com>
> ---
> drivers/edac/edac_mc_sysfs.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/edac/edac_mc_sysfs.c b/drivers/edac/edac_mc_sysfs.c
> index 445862d..e893b94 100644
> --- a/drivers/edac/edac_mc_sysfs.c
> +++ b/drivers/edac/edac_mc_sysfs.c
> @@ -251,8 +251,7 @@ static ssize_t channel_dimm_label_store(struct device *dev,
> if (copy_count == 0 || copy_count >= sizeof(rank->dimm->label))
> return -EINVAL;
>
> - strncpy(rank->dimm->label, data, copy_count);
> - rank->dimm->label[copy_count] = '\0';
> + strlcpy(rank->dimm->label, data, copy_count);
>
> return count;
> }
> @@ -530,8 +529,7 @@ static ssize_t dimmdev_label_store(struct device *dev,
> if (copy_count == 0 || copy_count >= sizeof(dimm->label))
> return -EINVAL;
>
> - strncpy(dimm->label, data, copy_count);
> - dimm->label[copy_count] = '\0';
> + strlcpy(dimm->label, data, copy_count);
Did you try your patch before sending it?
# cat /sys/devices/system/edac/mc/mc0/dimm0/dimm_label
CPU_SrcID#0_Ha#0_Chan#0_DIMM#0
# echo "bla" > /sys/devices/system/edac/mc/mc0/dimm0/dimm_label
# cat /sys/devices/system/edac/mc/mc0/dimm0/dimm_label
bl
# echo "bla boom" > /sys/devices/system/edac/mc/mc0/dimm0/dimm_label
# cat /sys/devices/system/edac/mc/mc0/dimm0/dimm_label
bla boo
Also, I don't like the fact that strlcpy does not pad the remainder of
the dest buffer with NUL.
So unless you're seeing a real bug, please don't waste your time with
this. There are enough real bugs in the kernel which need fixing.
Thanks.
--
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-05-28 18:12 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-26 19:10 [PATCH v2] edac: edac_mc_sysfs.c: Use strlcpy to Copy a C-string into a sized buffer Karim Eshapa
2017-05-28 18:12 ` Borislav Petkov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®