* [PATCH 0/2] EDAC: Fix sysfs dimm_label show & store operations
@ 2015-09-21 19:19 Toshi Kani
2015-09-21 19:19 ` [PATCH 1/2] EDAC: Fix sysfs dimm_label show operation Toshi Kani
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Toshi Kani @ 2015-09-21 19:19 UTC (permalink / raw)
To: mchehab, bp, dougthompson; +Cc: linux-edac, linux-kernel, elliott, tony.luck
This patch-set fixes the show and store operations of EDAC sysfs
files, "dimm_label" and "chX_dimm_label".
---
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 | 26 ++++++++++++++------------
1 file changed, 14 insertions(+), 12 deletions(-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] EDAC: Fix sysfs dimm_label show operation
2015-09-21 19:19 [PATCH 0/2] EDAC: Fix sysfs dimm_label show & store operations Toshi Kani
@ 2015-09-21 19:19 ` Toshi Kani
2015-09-21 19:19 ` [PATCH 2/2] EDAC: Fix sysfs dimm_label store operation Toshi Kani
2015-09-21 21:20 ` [PATCH 0/2] EDAC: Fix sysfs dimm_label show & store operations Luck, Tony
2 siblings, 0 replies; 5+ messages in thread
From: Toshi Kani @ 2015-09-21 19:19 UTC (permalink / raw)
To: mchehab, bp, dougthompson
Cc: linux-edac, linux-kernel, elliott, tony.luck, Toshi Kani
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().
Reported-by: Robert Elliott <elliott@hpe.com>
Signed-off-by: Toshi Kani <toshi.kani@hpe.com>
Cc: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Doug Thompson <dougthompson@xmission.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Robert Elliott <elliott@hpe.com>
---
drivers/edac/edac_mc_sysfs.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/edac/edac_mc_sysfs.c b/drivers/edac/edac_mc_sysfs.c
index 33df7d9..8983755 100644
--- a/drivers/edac/edac_mc_sysfs.c
+++ b/drivers/edac/edac_mc_sysfs.c
@@ -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,
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/2] EDAC: Fix sysfs dimm_label store operation
2015-09-21 19:19 [PATCH 0/2] EDAC: Fix sysfs dimm_label show & store operations Toshi Kani
2015-09-21 19:19 ` [PATCH 1/2] EDAC: Fix sysfs dimm_label show operation Toshi Kani
@ 2015-09-21 19:19 ` Toshi Kani
2015-09-21 23:40 ` Toshi Kani
2015-09-21 21:20 ` [PATCH 0/2] EDAC: Fix sysfs dimm_label show & store operations Luck, Tony
2 siblings, 1 reply; 5+ messages in thread
From: Toshi Kani @ 2015-09-21 19:19 UTC (permalink / raw)
To: mchehab, bp, dougthompson
Cc: linux-edac, linux-kernel, elliott, tony.luck, 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
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" \
> /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
0000002
3) 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" \
> /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 with a null, if any.
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 <toshi.kani@hpe.com>
Cc: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Doug Thompson <dougthompson@xmission.com>
Cc: Robert Elliott <elliott@hpe.com>
---
drivers/edac/edac_mc_sysfs.c | 22 ++++++++++++----------
1 file changed, 12 insertions(+), 10 deletions(-)
diff --git a/drivers/edac/edac_mc_sysfs.c b/drivers/edac/edac_mc_sysfs.c
index 8983755..f2ea914 100644
--- a/drivers/edac/edac_mc_sysfs.c
+++ b/drivers/edac/edac_mc_sysfs.c
@@ -241,13 +241,14 @@ 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);
+ if (rank->dimm->label[count - 1] == '\n')
+ rank->dimm->label[count - 1] = '\0';
- return max_size;
+ return count;
}
/* show function for dynamic chX_ce_count attribute */
@@ -495,13 +496,14 @@ 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);
+ if (dimm->label[count - 1] == '\n')
+ dimm->label[count - 1] = '\0';
- return max_size;
+ return count;
}
static ssize_t dimmdev_size_show(struct device *dev,
^ permalink raw reply related [flat|nested] 5+ messages in thread
* RE: [PATCH 0/2] EDAC: Fix sysfs dimm_label show & store operations
2015-09-21 19:19 [PATCH 0/2] EDAC: Fix sysfs dimm_label show & store operations Toshi Kani
2015-09-21 19:19 ` [PATCH 1/2] EDAC: Fix sysfs dimm_label show operation Toshi Kani
2015-09-21 19:19 ` [PATCH 2/2] EDAC: Fix sysfs dimm_label store operation Toshi Kani
@ 2015-09-21 21:20 ` Luck, Tony
2 siblings, 0 replies; 5+ messages in thread
From: Luck, Tony @ 2015-09-21 21:20 UTC (permalink / raw)
To: Toshi Kani, mchehab@osg.samsung.com, bp@alien8.de,
dougthompson@xmission.com
Cc: linux-edac@vger.kernel.org, linux-kernel@vger.kernel.org,
elliott@hpe.com
Toshi Kani (2):
1/2 EDAC: Fix sysfs dimm_label show operation
2/2 EDAC: Fix sysfs dimm_label store operation
Looks good. Both parts:
Acked-by: Tony Luck <tony.luck@intel.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] EDAC: Fix sysfs dimm_label store operation
2015-09-21 19:19 ` [PATCH 2/2] EDAC: Fix sysfs dimm_label store operation Toshi Kani
@ 2015-09-21 23:40 ` Toshi Kani
0 siblings, 0 replies; 5+ messages in thread
From: Toshi Kani @ 2015-09-21 23:40 UTC (permalink / raw)
To: mchehab, bp, dougthompson; +Cc: linux-edac, linux-kernel, elliott, tony.luck
On Mon, 2015-09-21 at 13:19 -0600, Toshi Kani wrote:
:
>
> diff --git a/drivers/edac/edac_mc_sysfs.c b/drivers/edac/edac_mc_sysfs.c
> index 8983755..f2ea914 100644
> --- a/drivers/edac/edac_mc_sysfs.c
> +++ b/drivers/edac/edac_mc_sysfs.c
> @@ -241,13 +241,14 @@ 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);
> + if (rank->dimm->label[count - 1] == '\n')
> + rank->dimm->label[count - 1] = '\0';
Thinking further, it'd be safer to always set a null at the end. I will
update the patch.
-Toshi
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-09-21 23:43 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-21 19:19 [PATCH 0/2] EDAC: Fix sysfs dimm_label show & store operations Toshi Kani
2015-09-21 19:19 ` [PATCH 1/2] EDAC: Fix sysfs dimm_label show operation Toshi Kani
2015-09-21 19:19 ` [PATCH 2/2] EDAC: Fix sysfs dimm_label store operation Toshi Kani
2015-09-21 23:40 ` Toshi Kani
2015-09-21 21:20 ` [PATCH 0/2] EDAC: Fix sysfs dimm_label show & store operations Luck, Tony
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox