EDAC: Fix sysfs dimm_label show & store operations
v2 cover EDAC: Fix sysfs dimm_label show & store operations
This patch-set fixes the show and store operations of EDAC sysfs files, "dimm_label" and "chX_dimm_label".
---
v2:
- Always set a null at the end in the store operation.
---
Toshi Kani (2):
1/2 EDAC: Fix sysfs dimm_label show operation
2/2 EDAC: Fix sysfs dimm_label store operation
--- drivers/edac/edac_mc_sysfs.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
PATCH v2 1/2 EDAC: Fix sysfs dimm_label show operation
After 'commit 7d375bffa524 ("sb_edac: Fix support for systems with two home agents per socket")', sysfs "dimm_label" and "chX_dimm_label" show their label string without a newline "\n" at the end.
[root@orange ~]# cat /sys/bus/mc0/devices/dimm0/dimm_label CPU_SrcID#0_Ha#0_Chan#0_DIMM#0[root@orange ~]#
[root@orange ~]# cat /sys/devices/system/edac/mc/mc0/csrow0/ch0_dimm_label CPU_SrcID#0_Ha#0_Chan#0_DIMM#0[root@orange ~]#
The label strings now have 31 characters, which are the same as EDAC_MC_LABEL_LEN. Since the snprintf()s in channel_dimm_label_show() and dimmdev_label_show() limit the whole length by EDAC_MC_LABEL_LEN, the newline in the format "%s\n" is ignored.
[root@orange ~]# od -bc /sys/bus/mc0/devices/dimm0/dimm_label 0000000 103 120 125 137 123 162 143 111 104 043 060 137 110 141 043 060
C P U _ S r c I D # 0 _ H a # 0
0000020 137 103 150 141 156 043 060 137 104 111 115 115 043 060 000
_ C h a n # 0 _ D I M M # 0 \0
0000037
Fix it by using 'sizeof(dimm->label) + 1' as the whole length in the snprintf()s in channel_dimm_label_show() and dimmdev_label_show().
--- drivers/edac/edac_mc_sysfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/edac/edac_mc_sysfs.c +2 -2
@@ -229,7 +229,7 @@ static ssize_t channel_dimm_label_show(struct device *dev,if (!rank->dimm->label[0 ])return 0 ; -return snprintf (data,EDAC_MC_LABEL_LEN ,"%s\n" , +return snprintf (data,sizeof (rank->dimm->label) +1 ,"%s\n" , rank->dimm->label); }
@@ -485,7 +485,7 @@ static ssize_t dimmdev_label_show(struct device *dev,if (!dimm->label[0 ])return 0 ; -return snprintf (data,EDAC_MC_LABEL_LEN ,"%s\n" , dimm->label); +return snprintf (data,sizeof (dimm->label) +1 ,"%s\n" , dimm->label); } static ssize_t dimmdev_label_store(struct device *dev,
PATCH v2 2/2 EDAC: Fix sysfs dimm_label store operation
In reply to Toshi KaniSysfs "dimm_label" and "chX_dimm_label" have the following issues in their store operation.
1) A newline-terminated input string causes redundant newlines
# echo "test" > /sys/bus/mc0/devices/dimm0/dimm_label # cat /sys/bus/mc0/devices/dimm0/dimm_label test
# od -bc /sys/bus/mc0/devices/dimm0/dimm_label
0000000 164 145 163 164 012 012
t e s t \n \n
0000006
2) The original label string (31 characters) cannot be stored due to an improper size check
# echo "CPU_SrcID#0_Ha#0_Chan#0_DIMM#0" \
Quoted 1 line
/sys/bus/mc0/devices/dimm0/dimm_label
# cat /sys/bus/mc0/devices/dimm0/dimm_label
# od -bc /sys/bus/mc0/devices/dimm0/dimm_label
0000000 012 012
\n \n
00000023) An input string longer than the buffer size results a wrong label info as it allows a retry with the remaining string.
# echo "CPU_SrcID#0_Ha#0_Chan#0_DIMM#0_TEST" \
Quoted 1 line
/sys/bus/mc0/devices/dimm0/dimm_label
# cat /sys/bus/mc0/devices/dimm0/dimm_label _TEST
Fix these issues by making the following changes:
1) Replace a newline charactor at the end by setting a null. It also assures that the string is null-terminated within the size.
2) Check the label buffer size with 'sizeof(dimm->label)'.
3) Fail a request if its string exceeds the label buffer size.
--- drivers/edac/edac_mc_sysfs.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/edac/edac_mc_sysfs.c +10 -10
@@ -241,13 +241,13 @@ static ssize_t channel_dimm_label_store(struct device *dev,unsigned chan =to_channel (mattr);struct rank_info *rank = csrow->channels[chan]; -ssize_t max_size =0 ; +if (count ==0 || count >sizeof (rank->dimm->label)) +return -EINVAL ; - max_size =min ((ssize_t ) count, (ssize_t )EDAC_MC_LABEL_LEN -1 ); -strncpy (rank->dimm->label, data, max_size); - rank->dimm->label[max_size] ='\0' ; +strncpy (rank->dimm->label, data, count); + rank->dimm->label[count -1 ] ='\0' ; -return max_size; +return count; }/* show function for dynamic chX_ce_count attribute */
@@ -495,13 +495,13 @@ static ssize_t dimmdev_label_store(struct device *dev, {struct dimm_info *dimm =to_dimm (dev); -ssize_t max_size =0 ; +if (count ==0 || count >sizeof (dimm->label)) +return -EINVAL ; - max_size =min ((ssize_t ) count, (ssize_t )EDAC_MC_LABEL_LEN -1 ); -strncpy (dimm->label, data, max_size); - dimm->label[max_size] ='\0' ; +strncpy (dimm->label, data, count); + dimm->label[count -1 ] ='\0' ; -return max_size; +return count; } static ssize_t dimmdev_size_show(struct device *dev,
Borislav Petkov
maintainerRe: [PATCH v2 2/2] EDAC: Fix sysfs dimm_label store operation
Quoted 51 lines · Toshi Kani
Sysfs "
dimm_label" and "chX_dimm_label" have the following issues in their store operation.1) A newline-terminated input string causes redundant newlines
# echo "test" > /sys/bus/mc0/devices/dimm0/dimm_label # cat /sys/bus/mc0/devices/dimm0/dimm_label test# od -bc
/sys/bus/mc0/devices/dimm0/dimm_label
0000000 164 145 163 164 012 012
t e s t \n \n
00000062) The original label string (31 characters) cannot be stored due to an improper size check
# echo "CPU_SrcID#0_Ha#0_Chan#0_DIMM#0" \Quoted 1 line
/sys/bus/mc0/devices/dimm0/dimm_label# cat /sys/bus/mc0/devices/dimm0/dimm_label# od -bc /sys/bus/mc0/devices/dimm0/dimm_label 0000000 012 012 \n \n 00000023) An input string longer than the buffer size results a wrong label info as it allows a retry with the remaining string.
# echo "CPU_SrcID#0_Ha#0_Chan#0_DIMM#0_TEST" \Quoted 1 line
/sys/bus/mc0/devices/dimm0/dimm_label# cat /sys/bus/mc0/devices/dimm0/dimm_label _TESTFix these issues by making the following changes:
1) Replace a newline charactor at the end by setting a null. It also assures that the string is null-terminated within the size.
2) Check the label buffer size with 'sizeof(dimm->label)'.
3) Fail a request if its string exceeds the label buffer size.Signed-off-by Toshi Kani <[email protected]>Acked-by Tony Luck <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Cc Borislav Petkov <[email protected]>Cc Doug Thompson <[email protected]>Cc Robert Elliott <[email protected]>Cc Tony Luck <[email protected]>--- drivers/edac/edac_mc_sysfs.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
...
Review static ssize_t dimmdev_label_store(struct device *dev, · line 499
Quoted 7 lines
(quoted diff) +2
-1@@ -495,13 +495,13 @@ static ssize_t dimmdev_label_store(struct device *dev, { struct dimm_info *dimm = to_dimm(dev); - ssize_t max_size = 0; + if (count == 0 || count > sizeof(dimm->label)) + return -EINVAL;
$ echo "" > /sys/bus/mc0/devices/dimm0/dimm_label $ od -bc /sys/bus/mc0/devices/dimm0/dimm_label 0000000 $ cat /sys/bus/mc0/devices/dimm0/dimm_label $
I don't think we want to allow empty labels. I guess something like this (2 because there's also the additional "\n"):
drivers/edac/edac_mc_sysfs.c +2 -2
@@ -241,7 +241,7 @@ static ssize_t channel_dimm_label_store(struct device *dev,unsigned chan =to_channel (mattr);struct rank_info *rank = csrow->channels[chan]; -if (count ==0 || count >sizeof (rank->dimm->label)) +if (count <2 || count >sizeof (rank->dimm->label))return -EINVAL ;strncpy (rank->dimm->label, data, count);
@@ -495,7 +495,7 @@ static ssize_t dimmdev_label_store(struct device *dev, {struct dimm_info *dimm =to_dimm (dev); -if (count ==0 || count >sizeof (dimm->label)) +if (count <2 || count >sizeof (dimm->label))return -EINVAL ;strncpy (dimm->label, data, count);
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Toshi Kani
Acked-by Tony LuckRe: [PATCH v2 2/2] EDAC: Fix sysfs dimm_label store operation
Quoted 69 lines · Borislav Petkov
Quoted 51 lines · Toshi Kani
Sysfs "
dimm_label" and "chX_dimm_label" have the following issues in their store operation.1) A newline-terminated input string causes redundant newlines
# echo "test" > /sys/bus/mc0/devices/dimm0/dimm_label # cat /sys/bus/mc0/devices/dimm0/dimm_label test# od -bc
/sys/bus/mc0/devices/dimm0/dimm_label
0000000 164 145 163 164 012 012
t e s t \n \n
00000062) The original label string (31 characters) cannot be stored due to an improper size check
# echo "CPU_SrcID#0_Ha#0_Chan#0_DIMM#0" \Quoted 1 line
/sys/bus/mc0/devices/dimm0/dimm_label# cat /sys/bus/mc0/devices/dimm0/dimm_label# od -bc /sys/bus/mc0/devices/dimm0/dimm_label 0000000 012 012 \n \n 00000023) An input string longer than the buffer size results a wrong label info as it allows a retry with the remaining string.
# echo "CPU_SrcID#0_Ha#0_Chan#0_DIMM#0_TEST" \Quoted 1 line
/sys/bus/mc0/devices/dimm0/dimm_label# cat /sys/bus/mc0/devices/dimm0/dimm_label _TESTFix these issues by making the following changes:
1) Replace a newline charactor at the end by setting a null. It also assures that the string is null-terminated within the size.
2) Check the label buffer size with 'sizeof(dimm->label)'.
3) Fail a request if its string exceeds the label buffer size.Signed-off-by Toshi Kani <[email protected]>Acked-by Tony Luck <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Cc Borislav Petkov <[email protected]>Cc Doug Thompson <[email protected]>Cc Robert Elliott <[email protected]>Cc Tony Luck <[email protected]>--- drivers/edac/edac_mc_sysfs.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)...
Review static ssize_t dimmdev_label_store(struct device *dev, · line 499
Quoted 7 lines
(quoted diff) +2
-1@@ -495,13 +495,13 @@ static ssize_t dimmdev_label_store(struct device *dev, { struct dimm_info *dimm = to_dimm(dev); - ssize_t max_size = 0; + if (count == 0 || count > sizeof(dimm->label)) + return -EINVAL;$ echo "" > /sys/bus/mc0/devices/dimm0/dimm_label $ od -bc /sys/bus/mc0/devices/dimm0/dimm_label 0000000 $ cat /sys/bus/mc0/devices/dimm0/dimm_label $
Quoted 2 lines
I don't think we want to allow empty labels. I guess something like this (2 because there's also the additional "\n"):
edac-utils(1) checks empty labels and shows them as "ch%d" [1]. So, I think empty labels are supported today, and using 'echo "" >' seems to be a legitimate way to set them empty if desired.
While looking at the count check, though, I noticed that 'echo -n' does not send a null-terminated string (hence, the count check needs to take it into account) since it simply calls putchar() to send '1' only in the example below. [2]
$ echo -n "1" | wc
0 1 1Attached is updated patch that handles the 'echo -n' case properly. When an input string is not null-terminated, it appends '\0' to the
end as long as it fits into the label buffer.
Thanks,
-Toshi
[1] https://github.com/grondo/edac-utils/search?utf8=%E2%9C%93&q=dimm_label_valid&type=Code
[2] http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/echo.c
======
Subject: [PATCH v2 UPDATE 2/2] EDAC: Fix sysfs dimm_label store operation
Sysfs "dimm_label" and "chX_dimm_label" have the following issues in their store operation.
1) A newline-terminated input string causes redundant newlines
# echo "test" > /sys/bus/mc0/devices/dimm0/dimm_label # cat /sys/bus/mc0/devices/dimm0/dimm_label test
# od -bc /sys/bus/mc0/devices/dimm0/dimm_label
0000000 164 145 163 164 012 012
t e s t \n \n
0000006
2) The original label string (31 characters) cannot be stored due to an improper size check
# echo "CPU_SrcID#0_Ha#0_Chan#0_DIMM#0" \
Quoted 1 line
/sys/bus/mc0/devices/dimm0/dimm_label
# cat /sys/bus/mc0/devices/dimm0/dimm_label
# od -bc /sys/bus/mc0/devices/dimm0/dimm_label
0000000 012 012
\n \n
00000023) An input string longer than the buffer size results a wrong label info as it allows a retry with the remaining string.
# echo "CPU_SrcID#0_Ha#0_Chan#0_DIMM#0_TEST" \
Quoted 1 line
/sys/bus/mc0/devices/dimm0/dimm_label
# cat /sys/bus/mc0/devices/dimm0/dimm_label _TEST
Fix these issues by making the following changes:
1) Replace a newline character at the end by setting a null. It also assures that the string is null-terminated in the label buffer.
2) Check the label buffer size with 'sizeof(dimm->label)'.
3) Fail a request if its string exceeds the label buffer size.
--- drivers/edac/edac_mc_sysfs.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
drivers/edac/edac_mc_sysfs.c +24 -10
@@ -240,14 +240,21 @@ static ssize_t channel_dimm_label_store(struct device *dev,struct csrow_info *csrow =to_csrow (dev);unsigned chan =to_channel (mattr);struct rank_info *rank = csrow->channels[chan]; +size_t copy_count = count; -ssize_t max_size =0 ; +if (count ==0 ) +return -EINVAL ; + +if (data[count -1 ] =='\0' || data[count -1 ] =='\n' ) + copy_count -=1 ; - max_size =min ((ssize_t ) count, (ssize_t )EDAC_MC_LABEL_LEN -1 ); -strncpy (rank->dimm->label, data, max_size); - rank->dimm->label[max_size] ='\0' ; +if (copy_count >=sizeof (rank->dimm->label)) +return -EINVAL ; -return max_size; +strncpy (rank->dimm->label, data, copy_count); + rank->dimm->label[copy_count] ='\0' ; + +return count; }/* show function for dynamic chX_ce_count attribute */
@@ -494,14 +501,21 @@ static ssize_t dimmdev_label_store(struct device *dev, size_t count) {struct dimm_info *dimm =to_dimm (dev); +size_t copy_count = count; -ssize_t max_size =0 ; +if (count ==0 ) +return -EINVAL ; + +if (data[count -1 ] =='\0' || data[count -1 ] =='\n' ) + copy_count -=1 ; - max_size =min ((ssize_t ) count, (ssize_t )EDAC_MC_LABEL_LEN -1 ); -strncpy (dimm->label, data, max_size); - dimm->label[max_size] ='\0' ; +if (copy_count >=sizeof (dimm->label)) +return -EINVAL ; -return max_size; +strncpy (dimm->label, data, copy_count); + dimm->label[copy_count] ='\0' ; + +return count; } static ssize_t dimmdev_size_show(struct device *dev,
Borislav Petkov
maintainerRe: [PATCH v2 2/2] EDAC: Fix sysfs dimm_label store operation
Quoted 3 lines · Toshi Kani
edac-utils(1) checks empty labels and shows them as "ch%d" [1]. So, I think empty labels are supported today, and using 'echo "" >' seems to be a legitimate way to set them empty if desired.
What would be a sane use case to set a DIMM label to an empty string?
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Re: [PATCH v2 2/2] EDAC: Fix sysfs dimm_label store operation
Quoted 6 lines · Borislav Petkov
Quoted 3 lines · Toshi Kani
edac-utils(1) checks empty labels and shows them as "ch%d" [1]. So, I think empty labels are supported today, and using 'echo "" >' seems to be a legitimate way to set them empty if desired.
What would be a sane use case to set a DIMM label to an empty string?
Well, I guess there isn't a sane use case for it... :-)
Attached is a patch (on top of patch 2/2) to disallow an empty string. I prefer to make it as a separate patch in case someone comes up with a valid use-case for it.
Thanks,
-Toshi
====
Subject: [PATCH] EDAC: Fail empty string to sysfs dimm_label store
Updating dimm_label to an empty string does not make much sense. Change the sysfs dimm_label store option to fail a request when an input string is empty.
--- drivers/edac/edac_mc_sysfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/edac/edac_mc_sysfs.c +2 -2
@@ -248,7 +248,7 @@ static ssize_t channel_dimm_label_store(struct device *dev,if (data[count -1 ] =='\0' || data[count -1 ] =='\n' ) copy_count -=1 ; -if (copy_count >=sizeof (rank->dimm->label)) +if (copy_count ==0 || copy_count >=sizeof (rank->dimm->label))return -EINVAL ;strncpy (rank->dimm->label, data, copy_count);
@@ -509,7 +509,7 @@ static ssize_t dimmdev_label_store(struct device *dev,if (data[count -1 ] =='\0' || data[count -1 ] =='\n' ) copy_count -=1 ; -if (copy_count >=sizeof (dimm->label)) +if (copy_count ==0 || copy_count >=sizeof (dimm->label))return -EINVAL ;strncpy (dimm->label, data, copy_count);
Borislav Petkov
maintainerRe: [PATCH v2 2/2] EDAC: Fix sysfs dimm_label store operation
Quoted 24 lines · Toshi Kani
Quoted 6 lines · Borislav Petkov
Quoted 3 lines · Toshi Kani
edac-utils(1) checks empty labels and shows them as "ch%d" [1]. So, I think empty labels are supported today, and using 'echo "" >' seems to be a legitimate way to set them empty if desired.
What would be a sane use case to set a DIMM label to an empty string?
Well, I guess there isn't a sane use case for it... :-)
Attached is a patch (on top of patch 2/2) to disallow an empty string. I prefer to make it as a separate patch in case someone comes up with a valid use-case for it.
Thanks,
-Toshi====
Subject: [PATCH] EDAC: Fail empty string to sysfsdimm_labelstoreUpdating
dimm_labelto an empty string does not make much sense. Change the sysfsdimm_labelstore option to fail a request when an input string is empty.
Quoted 5 lines
Suggested-by Borislav Petkov <[email protected]>Signed-off-by Toshi Kani <[email protected]>--- drivers/edac/edac_mc_sysfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
All three applied.
Thanks.
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.