Skip to contentSkip to conversation

edac: edac_mc_sysfs.c: Use strlcpy to Copy a C-string into a sized buffer

PATCH In review Karim Eshapa 2 participants 2 messages May 26, 2017 – May 28, 2017 EDAC-core

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 edac: edac_mc_sysfs.c: Use strlcpy to Copy a C-string into a sized buffer

Use strlcpy with sized buffer instead of strncpy to avoid non NUL-terminated source strings problems.

---
 drivers/edac/edac_mc_sysfs.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/edac/edac_mc_sysfs.c +2 -4
index 445862d..e893b94 100644
@@ -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;
 }
maintainer
#1 lore raw
Quoted 2 lines · Karim Eshapa

Use strlcpy with sized buffer instead of strncpy to avoid non NUL-terminated source strings problems.

Huh? But it does get terminated.

Quoted 20 lines
---
 drivers/edac/edac_mc_sysfs.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/edac/edac_mc_sysfs.c +1 -2
index 445862d..e893b94 100644
@@ -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;
 }

drivers/edac/edac_mc_sysfs.c static ssize_t dimmdev_label_store(struct device *dev, · line 532

Quoted 6 lines
drivers/edac/edac_mc_sysfs.c +1 -2
@@ -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.