NVDIMM Device and Persistent Memory development
 help / color / Atom feed
* [PATCH] dax: replace sprintf() by scnprintf()
@ 2021-07-10 16:46 Salah Triki
  2021-07-10 17:04 ` Joe Perches
  0 siblings, 1 reply; 5+ messages in thread
From: Salah Triki @ 2021-07-10 16:46 UTC (permalink / raw)
  To: dan.j.williams, vishal.l.verma, dave.jiang; +Cc: nvdimm, linux-kernel

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

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

diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
index 5aee26e1bbd6..bcae4be6ae76 100644
--- a/drivers/dax/bus.c
+++ b/drivers/dax/bus.c
@@ -76,7 +76,7 @@ static ssize_t do_id_store(struct device_driver *drv, const char *buf,
 	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
 	if (fields != 2)
 		return -EINVAL;
-	sprintf(devname, "dax%d.%d", region_id, id);
+	scnprintf(devname, DAX_NAME_LEN, "dax%d.%d", region_id, id);
 	if (!sysfs_streq(buf, devname))
 		return -EINVAL;
 
-- 
2.25.1


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

* Re: [PATCH] dax: replace sprintf() by scnprintf()
  2021-07-10 16:46 [PATCH] dax: replace sprintf() by scnprintf() Salah Triki
@ 2021-07-10 17:04 ` Joe Perches
  2021-07-12 12:26   ` Salah Triki
  0 siblings, 1 reply; 5+ messages in thread
From: Joe Perches @ 2021-07-10 17:04 UTC (permalink / raw)
  To: Salah Triki, dan.j.williams, vishal.l.verma, dave.jiang
  Cc: nvdimm, linux-kernel

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

OK but also not strictly necessary.  DAX_NAME_LEN is 30.

Are you finding and changing these manually or with a script?

> diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
[]
> @@ -76,7 +76,7 @@ static ssize_t do_id_store(struct device_driver *drv, const char *buf,
>  	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
>  	if (fields != 2)
>  		return -EINVAL;
> -	sprintf(devname, "dax%d.%d", region_id, id);
> +	scnprintf(devname, DAX_NAME_LEN, "dax%d.%d", region_id, id);
>  	if (!sysfs_streq(buf, devname))
>  		return -EINVAL;
>  
> 



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

* Re: [PATCH] dax: replace sprintf() by scnprintf()
  2021-07-10 17:04 ` Joe Perches
@ 2021-07-12 12:26   ` Salah Triki
  2021-07-12 16:14     ` Joe Perches
  0 siblings, 1 reply; 5+ messages in thread
From: Salah Triki @ 2021-07-12 12:26 UTC (permalink / raw)
  To: Joe Perches
  Cc: dan.j.williams, vishal.l.verma, dave.jiang, nvdimm, linux-kernel

On Sat, Jul 10, 2021 at 10:04:48AM -0700, Joe Perches wrote:
> On Sat, 2021-07-10 at 17:46 +0100, Salah Triki wrote:
> > Replace sprintf() by scnprintf() in order to avoid buffer overflows.
> 
> OK but also not strictly necessary.  DAX_NAME_LEN is 30.
> 
> Are you finding and changing these manually or with a script?
> 
> > diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
> []
> > @@ -76,7 +76,7 @@ static ssize_t do_id_store(struct device_driver *drv, const char *buf,
> >  	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
> >  	if (fields != 2)
> >  		return -EINVAL;
> > -	sprintf(devname, "dax%d.%d", region_id, id);
> > +	scnprintf(devname, DAX_NAME_LEN, "dax%d.%d", region_id, id);
> >  	if (!sysfs_streq(buf, devname))
> >  		return -EINVAL;
> >  
> > 
> 
> 

since region_id and id are unsigned long may be devname should be
char[21].

I'm finding and changing these manually.

Thanx

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

* Re: [PATCH] dax: replace sprintf() by scnprintf()
  2021-07-12 12:26   ` Salah Triki
@ 2021-07-12 16:14     ` Joe Perches
  2021-07-12 16:48       ` Salah Triki
  0 siblings, 1 reply; 5+ messages in thread
From: Joe Perches @ 2021-07-12 16:14 UTC (permalink / raw)
  To: Salah Triki
  Cc: dan.j.williams, vishal.l.verma, dave.jiang, nvdimm, linux-kernel

On Mon, 2021-07-12 at 13:26 +0100, Salah Triki wrote:
> On Sat, Jul 10, 2021 at 10:04:48AM -0700, Joe Perches wrote:
> > On Sat, 2021-07-10 at 17:46 +0100, Salah Triki wrote:
> > > Replace sprintf() by scnprintf() in order to avoid buffer overflows.
> > 
> > OK but also not strictly necessary.  DAX_NAME_LEN is 30.
> > 
> > Are you finding and changing these manually or with a script?
> > 
> > > diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
> > []
> > > @@ -76,7 +76,7 @@ static ssize_t do_id_store(struct device_driver *drv, const char *buf,
> > >  	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
> > >  	if (fields != 2)
> > >  		return -EINVAL;
> > > -	sprintf(devname, "dax%d.%d", region_id, id);
> > > +	scnprintf(devname, DAX_NAME_LEN, "dax%d.%d", region_id, id);
> > >  	if (!sysfs_streq(buf, devname))
> > >  		return -EINVAL;
> > >  
> > > 
> > 
> > 
> 
> since region_id and id are unsigned long may be devname should be
> char[21].

I think you need to look at the code a bit more carefully.

	unsigned int region_id, id;

Also the output is %d, so the maximum length of each output
int is 10 with a possible leading minus sign.

3 + 10 + 1 + 10 + 1.  So 25 not 21 which is too small.

The %d uses could be changed to %u to make it 23.
But really it hardly matters as 30 is sufficent and the
function call depth here isn't particularly high.

> I'm finding and changing these manually.

coccinelle could help.
https://coccinelle.gitlabpages.inria.fr/website/



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

* Re: [PATCH] dax: replace sprintf() by scnprintf()
  2021-07-12 16:14     ` Joe Perches
@ 2021-07-12 16:48       ` Salah Triki
  0 siblings, 0 replies; 5+ messages in thread
From: Salah Triki @ 2021-07-12 16:48 UTC (permalink / raw)
  To: Joe Perches
  Cc: dan.j.williams, vishal.l.verma, dave.jiang, nvdimm, linux-kernel

On Mon, Jul 12, 2021 at 09:14:53AM -0700, Joe Perches wrote:
> On Mon, 2021-07-12 at 13:26 +0100, Salah Triki wrote:
> > On Sat, Jul 10, 2021 at 10:04:48AM -0700, Joe Perches wrote:
> > > On Sat, 2021-07-10 at 17:46 +0100, Salah Triki wrote:
> > > > Replace sprintf() by scnprintf() in order to avoid buffer overflows.
> > > 
> > > OK but also not strictly necessary.  DAX_NAME_LEN is 30.
> > > 
> > > Are you finding and changing these manually or with a script?
> > > 
> > > > diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c
> > > []
> > > > @@ -76,7 +76,7 @@ static ssize_t do_id_store(struct device_driver *drv, const char *buf,
> > > >  	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
> > > >  	if (fields != 2)
> > > >  		return -EINVAL;
> > > > -	sprintf(devname, "dax%d.%d", region_id, id);
> > > > +	scnprintf(devname, DAX_NAME_LEN, "dax%d.%d", region_id, id);
> > > >  	if (!sysfs_streq(buf, devname))
> > > >  		return -EINVAL;
> > > >  
> > > > 
> > > 
> > > 
> > 
> > since region_id and id are unsigned long may be devname should be
> > char[21].
> 
> I think you need to look at the code a bit more carefully.
> 
> 	unsigned int region_id, id;
> 
> Also the output is %d, so the maximum length of each output
> int is 10 with a possible leading minus sign.
> 
> 3 + 10 + 1 + 10 + 1.  So 25 not 21 which is too small.
> 
> The %d uses could be changed to %u to make it 23.
> But really it hardly matters as 30 is sufficent and the
> function call depth here isn't particularly high.
> 
> > I'm finding and changing these manually.
> 
> coccinelle could help.
> https://coccinelle.gitlabpages.inria.fr/website/
> 
> 

Thanx 


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

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-10 16:46 [PATCH] dax: replace sprintf() by scnprintf() Salah Triki
2021-07-10 17:04 ` Joe Perches
2021-07-12 12:26   ` Salah Triki
2021-07-12 16:14     ` Joe Perches
2021-07-12 16:48       ` Salah Triki

NVDIMM Device and Persistent Memory development

Archives are clonable:
	git clone --mirror https://lore.kernel.org/nvdimm/0 nvdimm/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 nvdimm nvdimm/ https://lore.kernel.org/nvdimm \
		nvdimm@lists.linux.dev
	public-inbox-index nvdimm

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/dev.linux.lists.nvdimm


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git