linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] drivers/soc: Remove all strcpy() uses in favor of strscpy()
@ 2021-07-25 15:14 Len Baker
  2021-07-26  8:03 ` Geert Uytterhoeven
  0 siblings, 1 reply; 6+ messages in thread
From: Len Baker @ 2021-07-25 15:14 UTC (permalink / raw)
  To: Kees Cook, Andy Gross, Bjorn Andersson, Geert Uytterhoeven,
	Magnus Damm, Santosh Shilimkar
  Cc: Len Baker, linux-hardening, linux-arm-msm, linux-kernel,
	linux-renesas-soc, linux-arm-kernel

strcpy() performs no bounds checking on the destination buffer. This
could result in linear overflows beyond the end of the buffer, leading
to all kinds of misbehaviors. The safe replacement is strscpy().

Signed-off-by: Len Baker <len.baker@gmx.com>
---
This is a task of the KSPP [1]

[1] https://github.com/KSPP/linux/issues/88

 drivers/soc/qcom/pdr_interface.c    | 13 +++++++------
 drivers/soc/renesas/r8a779a0-sysc.c |  6 ++++--
 drivers/soc/renesas/rcar-sysc.c     |  6 ++++--
 drivers/soc/ti/knav_dma.c           |  2 +-
 4 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/drivers/soc/qcom/pdr_interface.c b/drivers/soc/qcom/pdr_interface.c
index 915d5bc3d46e..cf119fde749d 100644
--- a/drivers/soc/qcom/pdr_interface.c
+++ b/drivers/soc/qcom/pdr_interface.c
@@ -131,7 +131,7 @@ static int pdr_register_listener(struct pdr_handle *pdr,
 		return ret;

 	req.enable = enable;
-	strcpy(req.service_path, pds->service_path);
+	strscpy(req.service_path, pds->service_path, sizeof(req.service_path));

 	ret = qmi_send_request(&pdr->notifier_hdl, &pds->addr,
 			       &txn, SERVREG_REGISTER_LISTENER_REQ,
@@ -257,7 +257,7 @@ static int pdr_send_indack_msg(struct pdr_handle *pdr, struct pdr_service *pds,
 		return ret;

 	req.transaction_id = tid;
-	strcpy(req.service_path, pds->service_path);
+	strscpy(req.service_path, pds->service_path, sizeof(req.service_path));

 	ret = qmi_send_request(&pdr->notifier_hdl, &pds->addr,
 			       &txn, SERVREG_SET_ACK_REQ,
@@ -406,7 +406,7 @@ static int pdr_locate_service(struct pdr_handle *pdr, struct pdr_service *pds)
 		return -ENOMEM;

 	/* Prepare req message */
-	strcpy(req.service_name, pds->service_name);
+	strscpy(req.service_name, pds->service_name, sizeof(req.service_name));
 	req.domain_offset_valid = true;
 	req.domain_offset = 0;

@@ -531,8 +531,8 @@ struct pdr_service *pdr_add_lookup(struct pdr_handle *pdr,
 		return ERR_PTR(-ENOMEM);

 	pds->service = SERVREG_NOTIFIER_SERVICE;
-	strcpy(pds->service_name, service_name);
-	strcpy(pds->service_path, service_path);
+	strscpy(pds->service_name, service_name, sizeof(pds->service_name));
+	strscpy(pds->service_path, service_path, sizeof(pds->service_path));
 	pds->need_locator_lookup = true;

 	mutex_lock(&pdr->list_lock);
@@ -587,7 +587,8 @@ int pdr_restart_pd(struct pdr_handle *pdr, struct pdr_service *pds)
 			break;

 		/* Prepare req message */
-		strcpy(req.service_path, pds->service_path);
+		strscpy(req.service_path, pds->service_path,
+			sizeof(req.service_path));
 		addr = pds->addr;
 		break;
 	}
diff --git a/drivers/soc/renesas/r8a779a0-sysc.c b/drivers/soc/renesas/r8a779a0-sysc.c
index d464ffa1be33..25f8164b96d0 100644
--- a/drivers/soc/renesas/r8a779a0-sysc.c
+++ b/drivers/soc/renesas/r8a779a0-sysc.c
@@ -404,19 +404,21 @@ static int __init r8a779a0_sysc_pd_init(void)
 	for (i = 0; i < info->num_areas; i++) {
 		const struct r8a779a0_sysc_area *area = &info->areas[i];
 		struct r8a779a0_sysc_pd *pd;
+		size_t area_name_size;

 		if (!area->name) {
 			/* Skip NULLified area */
 			continue;
 		}

-		pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
+		area_name_size = strlen(area->name) + 1;
+		pd = kzalloc(sizeof(*pd) + area_name_size, GFP_KERNEL);
 		if (!pd) {
 			error = -ENOMEM;
 			goto out_put;
 		}

-		strcpy(pd->name, area->name);
+		strscpy(pd->name, area->name, area_name_size);
 		pd->genpd.name = pd->name;
 		pd->pdr = area->pdr;
 		pd->flags = area->flags;
diff --git a/drivers/soc/renesas/rcar-sysc.c b/drivers/soc/renesas/rcar-sysc.c
index 53387a72ca00..0eae5ce0eeb0 100644
--- a/drivers/soc/renesas/rcar-sysc.c
+++ b/drivers/soc/renesas/rcar-sysc.c
@@ -396,19 +396,21 @@ static int __init rcar_sysc_pd_init(void)
 	for (i = 0; i < info->num_areas; i++) {
 		const struct rcar_sysc_area *area = &info->areas[i];
 		struct rcar_sysc_pd *pd;
+		size_t area_name_size;

 		if (!area->name) {
 			/* Skip NULLified area */
 			continue;
 		}

-		pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
+		area_name_size = strlen(area->name) + 1;
+		pd = kzalloc(sizeof(*pd) + area_name_size, GFP_KERNEL);
 		if (!pd) {
 			error = -ENOMEM;
 			goto out_put;
 		}

-		strcpy(pd->name, area->name);
+		strscpy(pd->name, area->name, area_name_size);
 		pd->genpd.name = pd->name;
 		pd->ch.chan_offs = area->chan_offs;
 		pd->ch.chan_bit = area->chan_bit;
diff --git a/drivers/soc/ti/knav_dma.c b/drivers/soc/ti/knav_dma.c
index 591d14ebcb11..5f9816d317a5 100644
--- a/drivers/soc/ti/knav_dma.c
+++ b/drivers/soc/ti/knav_dma.c
@@ -691,7 +691,7 @@ static int dma_init(struct device_node *cloud, struct device_node *dma_node)
 	dma->max_rx_flow = max_rx_flow;
 	dma->max_tx_chan = min(max_tx_chan, max_tx_sched);
 	atomic_set(&dma->ref_count, 0);
-	strcpy(dma->name, node->name);
+	strscpy(dma->name, node->name, sizeof(dma->name));
 	spin_lock_init(&dma->lock);

 	for (i = 0; i < dma->max_tx_chan; i++) {
--
2.25.1


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

* Re: [PATCH] drivers/soc: Remove all strcpy() uses in favor of strscpy()
  2021-07-25 15:14 [PATCH] drivers/soc: Remove all strcpy() uses in favor of strscpy() Len Baker
@ 2021-07-26  8:03 ` Geert Uytterhoeven
  2021-07-28  8:36   ` David Laight
  2021-07-31 13:59   ` Len Baker
  0 siblings, 2 replies; 6+ messages in thread
From: Geert Uytterhoeven @ 2021-07-26  8:03 UTC (permalink / raw)
  To: Len Baker
  Cc: Kees Cook, Andy Gross, Bjorn Andersson, Magnus Damm,
	Santosh Shilimkar, linux-hardening, linux-arm-msm,
	Linux Kernel Mailing List, Linux-Renesas, Linux ARM

Hi Len,

On Sun, Jul 25, 2021 at 5:15 PM Len Baker <len.baker@gmx.com> wrote:
> strcpy() performs no bounds checking on the destination buffer. This
> could result in linear overflows beyond the end of the buffer, leading
> to all kinds of misbehaviors. The safe replacement is strscpy().
>
> Signed-off-by: Len Baker <len.baker@gmx.com>

Thanks for your patch!

> ---
> This is a task of the KSPP [1]
>
> [1] https://github.com/KSPP/linux/issues/88

Any chance the almost one year old question in that ticket can be
answered?

>  drivers/soc/renesas/rcar-sysc.c     |  6 ++++--

Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

But please see my comments below...

> --- a/drivers/soc/renesas/r8a779a0-sysc.c
> +++ b/drivers/soc/renesas/r8a779a0-sysc.c
> @@ -404,19 +404,21 @@ static int __init r8a779a0_sysc_pd_init(void)
>         for (i = 0; i < info->num_areas; i++) {
>                 const struct r8a779a0_sysc_area *area = &info->areas[i];
>                 struct r8a779a0_sysc_pd *pd;
> +               size_t area_name_size;

I wouldn't mind a shorter name, like "n".

>
>                 if (!area->name) {
>                         /* Skip NULLified area */
>                         continue;
>                 }
>
> -               pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
> +               area_name_size = strlen(area->name) + 1;
> +               pd = kzalloc(sizeof(*pd) + area_name_size, GFP_KERNEL);
>                 if (!pd) {
>                         error = -ENOMEM;
>                         goto out_put;
>                 }
>
> -               strcpy(pd->name, area->name);
> +               strscpy(pd->name, area->name, area_name_size);
>                 pd->genpd.name = pd->name;
>                 pd->pdr = area->pdr;
>                 pd->flags = area->flags;
> diff --git a/drivers/soc/renesas/rcar-sysc.c b/drivers/soc/renesas/rcar-sysc.c
> index 53387a72ca00..0eae5ce0eeb0 100644
> --- a/drivers/soc/renesas/rcar-sysc.c
> +++ b/drivers/soc/renesas/rcar-sysc.c
> @@ -396,19 +396,21 @@ static int __init rcar_sysc_pd_init(void)
>         for (i = 0; i < info->num_areas; i++) {
>                 const struct rcar_sysc_area *area = &info->areas[i];
>                 struct rcar_sysc_pd *pd;
> +               size_t area_name_size;

Likewise.

>
>                 if (!area->name) {
>                         /* Skip NULLified area */
>                         continue;
>                 }
>
> -               pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
> +               area_name_size = strlen(area->name) + 1;
> +               pd = kzalloc(sizeof(*pd) + area_name_size, GFP_KERNEL);
>                 if (!pd) {
>                         error = -ENOMEM;
>                         goto out_put;
>                 }
>
> -               strcpy(pd->name, area->name);
> +               strscpy(pd->name, area->name, area_name_size);
>                 pd->genpd.name = pd->name;
>                 pd->ch.chan_offs = area->chan_offs;
>                 pd->ch.chan_bit = area->chan_bit;

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

* RE: [PATCH] drivers/soc: Remove all strcpy() uses in favor of strscpy()
  2021-07-26  8:03 ` Geert Uytterhoeven
@ 2021-07-28  8:36   ` David Laight
  2021-07-28  9:36     ` Robin Murphy
  2021-07-31 13:59   ` Len Baker
  1 sibling, 1 reply; 6+ messages in thread
From: David Laight @ 2021-07-28  8:36 UTC (permalink / raw)
  To: 'Geert Uytterhoeven', Len Baker
  Cc: Kees Cook, Andy Gross, Bjorn Andersson, Magnus Damm,
	Santosh Shilimkar, linux-hardening, linux-arm-msm,
	Linux Kernel Mailing List, Linux-Renesas, Linux ARM

From: Geert Uytterhoeven
> Sent: 26 July 2021 09:03
> 
> Hi Len,
> 
> On Sun, Jul 25, 2021 at 5:15 PM Len Baker <len.baker@gmx.com> wrote:
> > strcpy() performs no bounds checking on the destination buffer. This
> > could result in linear overflows beyond the end of the buffer, leading
> > to all kinds of misbehaviors. The safe replacement is strscpy().
> >
> > Signed-off-by: Len Baker <len.baker@gmx.com>
> 
> Thanks for your patch!
> 
> > ---
> > This is a task of the KSPP [1]
> >
> > [1] https://github.com/KSPP/linux/issues/88
> 
> Any chance the almost one year old question in that ticket can be
> answered?
> 
> >  drivers/soc/renesas/rcar-sysc.c     |  6 ++++--
> 
> Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
> 
> But please see my comments below...
> 
> > --- a/drivers/soc/renesas/r8a779a0-sysc.c
> > +++ b/drivers/soc/renesas/r8a779a0-sysc.c
> > @@ -404,19 +404,21 @@ static int __init r8a779a0_sysc_pd_init(void)
> >         for (i = 0; i < info->num_areas; i++) {
> >                 const struct r8a779a0_sysc_area *area = &info->areas[i];
> >                 struct r8a779a0_sysc_pd *pd;
> > +               size_t area_name_size;
> 
> I wouldn't mind a shorter name, like "n".
> 
> >
> >                 if (!area->name) {
> >                         /* Skip NULLified area */
> >                         continue;
> >                 }
> >
> > -               pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
> > +               area_name_size = strlen(area->name) + 1;
> > +               pd = kzalloc(sizeof(*pd) + area_name_size, GFP_KERNEL);
> >                 if (!pd) {
> >                         error = -ENOMEM;
> >                         goto out_put;
> >                 }
> >
> > -               strcpy(pd->name, area->name);
> > +               strscpy(pd->name, area->name, area_name_size);

You can just use memcpy().

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

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

* Re: [PATCH] drivers/soc: Remove all strcpy() uses in favor of strscpy()
  2021-07-28  8:36   ` David Laight
@ 2021-07-28  9:36     ` Robin Murphy
  2021-07-31 14:53       ` Len Baker
  0 siblings, 1 reply; 6+ messages in thread
From: Robin Murphy @ 2021-07-28  9:36 UTC (permalink / raw)
  To: David Laight, 'Geert Uytterhoeven', Len Baker
  Cc: Kees Cook, Andy Gross, Bjorn Andersson, Magnus Damm,
	Santosh Shilimkar, linux-hardening, linux-arm-msm,
	Linux Kernel Mailing List, Linux-Renesas, Linux ARM

On 2021-07-28 09:36, David Laight wrote:
> From: Geert Uytterhoeven
>> Sent: 26 July 2021 09:03
>>
>> Hi Len,
>>
>> On Sun, Jul 25, 2021 at 5:15 PM Len Baker <len.baker@gmx.com> wrote:
>>> strcpy() performs no bounds checking on the destination buffer. This
>>> could result in linear overflows beyond the end of the buffer, leading
>>> to all kinds of misbehaviors. The safe replacement is strscpy().
>>>
>>> Signed-off-by: Len Baker <len.baker@gmx.com>
>>
>> Thanks for your patch!
>>
>>> ---
>>> This is a task of the KSPP [1]
>>>
>>> [1] https://github.com/KSPP/linux/issues/88
>>
>> Any chance the almost one year old question in that ticket can be
>> answered?
>>
>>>   drivers/soc/renesas/rcar-sysc.c     |  6 ++++--
>>
>> Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
>>
>> But please see my comments below...
>>
>>> --- a/drivers/soc/renesas/r8a779a0-sysc.c
>>> +++ b/drivers/soc/renesas/r8a779a0-sysc.c
>>> @@ -404,19 +404,21 @@ static int __init r8a779a0_sysc_pd_init(void)
>>>          for (i = 0; i < info->num_areas; i++) {
>>>                  const struct r8a779a0_sysc_area *area = &info->areas[i];
>>>                  struct r8a779a0_sysc_pd *pd;
>>> +               size_t area_name_size;
>>
>> I wouldn't mind a shorter name, like "n".
>>
>>>
>>>                  if (!area->name) {
>>>                          /* Skip NULLified area */
>>>                          continue;
>>>                  }
>>>
>>> -               pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
>>> +               area_name_size = strlen(area->name) + 1;
>>> +               pd = kzalloc(sizeof(*pd) + area_name_size, GFP_KERNEL);
>>>                  if (!pd) {
>>>                          error = -ENOMEM;
>>>                          goto out_put;
>>>                  }
>>>
>>> -               strcpy(pd->name, area->name);
>>> +               strscpy(pd->name, area->name, area_name_size);
> 
> You can just use memcpy().

Indeed. In fact I'd go as far as saying that it might be worth teaching 
static checkers to recognise patterns that boil down to strscpy(dst, 
src, strlen(src) + 1) and flag them as suspect, because AFAICS that 
would always represent either an unnecessarily elaborate memcpy(), or 
far worse just an obfuscated strcpy().

Robin.

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

* Re: [PATCH] drivers/soc: Remove all strcpy() uses in favor of strscpy()
  2021-07-26  8:03 ` Geert Uytterhoeven
  2021-07-28  8:36   ` David Laight
@ 2021-07-31 13:59   ` Len Baker
  1 sibling, 0 replies; 6+ messages in thread
From: Len Baker @ 2021-07-31 13:59 UTC (permalink / raw)
  To: Geert Uytterhoeven, Kees Cook
  Cc: Len Baker, David Laight, Robin Murphy, Andy Gross,
	Bjorn Andersson, Magnus Damm, Santosh Shilimkar, linux-hardening,
	linux-arm-msm, Linux Kernel Mailing List, Linux-Renesas,
	Linux ARM

Hi,

On Mon, Jul 26, 2021 at 10:03:18AM +0200, Geert Uytterhoeven wrote:
> Hi Len,
>
> On Sun, Jul 25, 2021 at 5:15 PM Len Baker <len.baker@gmx.com> wrote:
> > strcpy() performs no bounds checking on the destination buffer. This
> > could result in linear overflows beyond the end of the buffer, leading
> > to all kinds of misbehaviors. The safe replacement is strscpy().
> >
> > Signed-off-by: Len Baker <len.baker@gmx.com>
>
> Thanks for your patch!
>
> > ---
> > This is a task of the KSPP [1]
> >
> > [1] https://github.com/KSPP/linux/issues/88
>
> Any chance the almost one year old question in that ticket can be
> answered?

I'm a kernel newbie and I have chosen this task as a starting point. So,
I think that someone with more experience could answer this question.

Kees: Any comments?

>
> >  drivers/soc/renesas/rcar-sysc.c     |  6 ++++--
>
> Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
>
> But please see my comments below...
>
> > --- a/drivers/soc/renesas/r8a779a0-sysc.c
> > +++ b/drivers/soc/renesas/r8a779a0-sysc.c
> > @@ -404,19 +404,21 @@ static int __init r8a779a0_sysc_pd_init(void)
> >         for (i = 0; i < info->num_areas; i++) {
> >                 const struct r8a779a0_sysc_area *area = &info->areas[i];
> >                 struct r8a779a0_sysc_pd *pd;
> > +               size_t area_name_size;
>
> I wouldn't mind a shorter name, like "n".

Ok, I will change this for the next version.

> >
> >                 if (!area->name) {
> >                         /* Skip NULLified area */
> >                         continue;
> >                 }
> >
> > -               pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
> > +               area_name_size = strlen(area->name) + 1;
> > +               pd = kzalloc(sizeof(*pd) + area_name_size, GFP_KERNEL);
> >                 if (!pd) {
> >                         error = -ENOMEM;
> >                         goto out_put;
> >                 }
> >
> > -               strcpy(pd->name, area->name);
> > +               strscpy(pd->name, area->name, area_name_size);
> >                 pd->genpd.name = pd->name;
> >                 pd->pdr = area->pdr;
> >                 pd->flags = area->flags;
> > diff --git a/drivers/soc/renesas/rcar-sysc.c b/drivers/soc/renesas/rcar-sysc.c
> > index 53387a72ca00..0eae5ce0eeb0 100644
> > --- a/drivers/soc/renesas/rcar-sysc.c
> > +++ b/drivers/soc/renesas/rcar-sysc.c
> > @@ -396,19 +396,21 @@ static int __init rcar_sysc_pd_init(void)
> >         for (i = 0; i < info->num_areas; i++) {
> >                 const struct rcar_sysc_area *area = &info->areas[i];
> >                 struct rcar_sysc_pd *pd;
> > +               size_t area_name_size;
>
> Likewise.

Ok, understood.

Regards,
Len

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

* Re: [PATCH] drivers/soc: Remove all strcpy() uses in favor of strscpy()
  2021-07-28  9:36     ` Robin Murphy
@ 2021-07-31 14:53       ` Len Baker
  0 siblings, 0 replies; 6+ messages in thread
From: Len Baker @ 2021-07-31 14:53 UTC (permalink / raw)
  To: Robin Murphy, David Laight, 'Geert Uytterhoeven'
  Cc: Len Baker, Kees Cook, Andy Gross, Bjorn Andersson, Magnus Damm,
	Santosh Shilimkar, linux-hardening, linux-arm-msm,
	Linux Kernel Mailing List, Linux-Renesas, Linux ARM

Hi,

On Wed, Jul 28, 2021 at 10:36:09AM +0100, Robin Murphy wrote:
> On 2021-07-28 09:36, David Laight wrote:

> > > > -               strcpy(pd->name, area->name);
> > > > +               strscpy(pd->name, area->name, area_name_size);
> >
> > You can just use memcpy().
>
> Indeed. In fact I'd go as far as saying that it might be worth teaching
> static checkers to recognise patterns that boil down to strscpy(dst, src,
> strlen(src) + 1) and flag them as suspect, because AFAICS that would always
> represent either an unnecessarily elaborate memcpy(), or far worse just an
> obfuscated strcpy().

Ok, I will use the memcpy function instead of strscpy. Thanks for the
feedback.

>
> Robin.

Regards,
Len

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

end of thread, other threads:[~2021-07-31 14:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-25 15:14 [PATCH] drivers/soc: Remove all strcpy() uses in favor of strscpy() Len Baker
2021-07-26  8:03 ` Geert Uytterhoeven
2021-07-28  8:36   ` David Laight
2021-07-28  9:36     ` Robin Murphy
2021-07-31 14:53       ` Len Baker
2021-07-31 13:59   ` Len Baker

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).