linux-edac.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RESEND] edac: replace sprintf() by scnprintf()
@ 2021-07-10 16:35 Salah Triki
  2021-07-10 16:58 ` Joe Perches
  0 siblings, 1 reply; 4+ messages in thread
From: Salah Triki @ 2021-07-10 16:35 UTC (permalink / raw)
  To: bp, mchehab, tony.luck, james.morse, rric; +Cc: linux-edac, linux-kernel

Replace sprintf() by scnprintf() in order to avoid buffer overflows.

Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
 drivers/edac/debugfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/edac/debugfs.c b/drivers/edac/debugfs.c
index 4804332d9946..c83653084aa3 100644
--- a/drivers/edac/debugfs.c
+++ b/drivers/edac/debugfs.c
@@ -61,7 +61,7 @@ void edac_create_debugfs_nodes(struct mem_ctl_info *mci)
 	parent = debugfs_create_dir(mci->dev.kobj.name, edac_debugfs);
 
 	for (i = 0; i < mci->n_layers; i++) {
-		sprintf(name, "fake_inject_%s",
+		scnprintf(name, sizeof(name), "fake_inject_%s",
 			     edac_layer_name[mci->layers[i].type]);
 		debugfs_create_u8(name, S_IRUGO | S_IWUSR, parent,
 				  &mci->fake_inject_layer[i]);
-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RESEND] edac: replace sprintf() by scnprintf()
  2021-07-10 16:35 [RESEND] edac: replace sprintf() by scnprintf() Salah Triki
@ 2021-07-10 16:58 ` Joe Perches
  2021-07-12 12:09   ` Salah Triki
  0 siblings, 1 reply; 4+ messages in thread
From: Joe Perches @ 2021-07-10 16:58 UTC (permalink / raw)
  To: Salah Triki, bp, mchehab, tony.luck, james.morse, rric
  Cc: linux-edac, linux-kernel

On Sat, 2021-07-10 at 17:35 +0100, Salah Triki wrote:
> Replace sprintf() by scnprintf() in order to avoid buffer overflows.

While of course safe, this is not strictly necessary as the
maximum length of any edac_layer_name is 8 bytes.

drivers/edac/edac_mc.c:const char *edac_layer_name[] = {
drivers/edac/edac_mc.c- [EDAC_MC_LAYER_BRANCH] = "branch",
drivers/edac/edac_mc.c- [EDAC_MC_LAYER_CHANNEL] = "channel",
drivers/edac/edac_mc.c- [EDAC_MC_LAYER_SLOT] = "slot",
drivers/edac/edac_mc.c- [EDAC_MC_LAYER_CHIP_SELECT] = "csrow",
drivers/edac/edac_mc.c- [EDAC_MC_LAYER_ALL_MEM] = "memory",
drivers/edac/edac_mc.c-};

And name is:

		char name[80];

I suppose name[80] could be changed to name[32] or so
at the same time to reduce stack usage.

Maybe name should be moved into the loop too.

---
 drivers/edac/debugfs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/edac/debugfs.c b/drivers/edac/debugfs.c
index 4804332d99465..a41071f2ad428 100644
--- a/drivers/edac/debugfs.c
+++ b/drivers/edac/debugfs.c
@@ -55,14 +55,15 @@ void edac_debugfs_exit(void)
 void edac_create_debugfs_nodes(struct mem_ctl_info *mci)
 {
 	struct dentry *parent;
-	char name[80];
 	int i;
 
 	parent = debugfs_create_dir(mci->dev.kobj.name, edac_debugfs);
 
 	for (i = 0; i < mci->n_layers; i++) {
-		sprintf(name, "fake_inject_%s",
-			     edac_layer_name[mci->layers[i].type]);
+		char name[32];
+
+		scnprintf(name, sizeof(name), "fake_inject_%s",
+			  edac_layer_name[mci->layers[i].type]);
 		debugfs_create_u8(name, S_IRUGO | S_IWUSR, parent,
 				  &mci->fake_inject_layer[i]);
 	}



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RESEND] edac: replace sprintf() by scnprintf()
  2021-07-10 16:58 ` Joe Perches
@ 2021-07-12 12:09   ` Salah Triki
  2021-07-12 16:30     ` Joe Perches
  0 siblings, 1 reply; 4+ messages in thread
From: Salah Triki @ 2021-07-12 12:09 UTC (permalink / raw)
  To: Joe Perches
  Cc: bp, mchehab, tony.luck, james.morse, rric, linux-edac, linux-kernel

On Sat, Jul 10, 2021 at 09:58:46AM -0700, Joe Perches wrote:
> On Sat, 2021-07-10 at 17:35 +0100, Salah Triki wrote:
> > Replace sprintf() by scnprintf() in order to avoid buffer overflows.
> 
> While of course safe, this is not strictly necessary as the
> maximum length of any edac_layer_name is 8 bytes.
> 
> drivers/edac/edac_mc.c:const char *edac_layer_name[] = {
> drivers/edac/edac_mc.c- [EDAC_MC_LAYER_BRANCH] = "branch",
> drivers/edac/edac_mc.c- [EDAC_MC_LAYER_CHANNEL] = "channel",
> drivers/edac/edac_mc.c- [EDAC_MC_LAYER_SLOT] = "slot",
> drivers/edac/edac_mc.c- [EDAC_MC_LAYER_CHIP_SELECT] = "csrow",
> drivers/edac/edac_mc.c- [EDAC_MC_LAYER_ALL_MEM] = "memory",
> drivers/edac/edac_mc.c-};
> 
> And name is:
> 
> 		char name[80];
> 
> I suppose name[80] could be changed to name[32] or so
> at the same time to reduce stack usage.
> 
> Maybe name should be moved into the loop too.
> 
> ---
>  drivers/edac/debugfs.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/edac/debugfs.c b/drivers/edac/debugfs.c
> index 4804332d99465..a41071f2ad428 100644
> --- a/drivers/edac/debugfs.c
> +++ b/drivers/edac/debugfs.c
> @@ -55,14 +55,15 @@ void edac_debugfs_exit(void)
>  void edac_create_debugfs_nodes(struct mem_ctl_info *mci)
>  {
>  	struct dentry *parent;
> -	char name[80];
>  	int i;
>  
>  	parent = debugfs_create_dir(mci->dev.kobj.name, edac_debugfs);
>  
>  	for (i = 0; i < mci->n_layers; i++) {
> -		sprintf(name, "fake_inject_%s",
> -			     edac_layer_name[mci->layers[i].type]);
> +		char name[32];
> +
> +		scnprintf(name, sizeof(name), "fake_inject_%s",
> +			  edac_layer_name[mci->layers[i].type]);
>  		debugfs_create_u8(name, S_IRUGO | S_IWUSR, parent,
>  				  &mci->fake_inject_layer[i]);
>  	}
> 
> 

I think name should be [20] since len("fake_inject_") is 12 and maximum 
length of any edac_layer_name is 7 bytes. In addition, for moving the
declaration of name inside the loop we need to compile the source in 
C99 mode.

Thanx

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RESEND] edac: replace sprintf() by scnprintf()
  2021-07-12 12:09   ` Salah Triki
@ 2021-07-12 16:30     ` Joe Perches
  0 siblings, 0 replies; 4+ messages in thread
From: Joe Perches @ 2021-07-12 16:30 UTC (permalink / raw)
  To: Salah Triki
  Cc: bp, mchehab, tony.luck, james.morse, rric, linux-edac, linux-kernel

On Mon, 2021-07-12 at 13:09 +0100, Salah Triki wrote:
> On Sat, Jul 10, 2021 at 09:58:46AM -0700, Joe Perches wrote:
> > On Sat, 2021-07-10 at 17:35 +0100, Salah Triki wrote:
> > > Replace sprintf() by scnprintf() in order to avoid buffer overflows.
> > Maybe name should be moved into the loop too.
> In addition, for moving the
> declaration of name inside the loop we need to compile the source in 
> C99 mode.

You need to read the c90 spec.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-07-12 16:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-10 16:35 [RESEND] edac: replace sprintf() by scnprintf() Salah Triki
2021-07-10 16:58 ` Joe Perches
2021-07-12 12:09   ` Salah Triki
2021-07-12 16:30     ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).