* [PATCH v4 1/3] drivers/soc/qcom: Prefer strscpy over strcpy
2021-08-08 12:50 [PATCH v4 0/3] drivers/soc: Remove all strcpy() uses Len Baker
@ 2021-08-08 12:50 ` Len Baker
2021-08-08 12:50 ` [PATCH v4 2/3] drivers/soc/renesas: Prefer memcpy " Len Baker
2021-08-08 12:50 ` [PATCH v4 3/3] drivers/soc/ti: Prefer strscpy " Len Baker
2 siblings, 0 replies; 8+ messages in thread
From: Len Baker @ 2021-08-08 12:50 UTC (permalink / raw)
To: Andy Gross, Bjorn Andersson, Geert Uytterhoeven, Magnus Damm,
Santosh Shilimkar
Cc: Len Baker, Kees Cook, David Laight, Robin Murphy,
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().
This is a previous step in the path to remove the strcpy() function
entirely from the kernel.
Signed-off-by: Len Baker <len.baker@gmx.com>
---
drivers/soc/qcom/pdr_interface.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/drivers/soc/qcom/pdr_interface.c b/drivers/soc/qcom/pdr_interface.c
index 915d5bc3d46e..fc580a3c4336 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,7 @@ 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;
}
--
2.25.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v4 2/3] drivers/soc/renesas: Prefer memcpy over strcpy
2021-08-08 12:50 [PATCH v4 0/3] drivers/soc: Remove all strcpy() uses Len Baker
2021-08-08 12:50 ` [PATCH v4 1/3] drivers/soc/qcom: Prefer strscpy over strcpy Len Baker
@ 2021-08-08 12:50 ` Len Baker
2021-08-08 15:35 ` Bernd Petrovitsch
2021-08-11 9:38 ` Geert Uytterhoeven
2021-08-08 12:50 ` [PATCH v4 3/3] drivers/soc/ti: Prefer strscpy " Len Baker
2 siblings, 2 replies; 8+ messages in thread
From: Len Baker @ 2021-08-08 12:50 UTC (permalink / raw)
To: Andy Gross, Bjorn Andersson, Geert Uytterhoeven, Magnus Damm,
Santosh Shilimkar
Cc: Len Baker, Kees Cook, David Laight, Robin Murphy,
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. So, use memcpy() as a safe replacement.
This is a previous step in the path to remove the strcpy() function
entirely from the kernel.
Signed-off-by: Len Baker <len.baker@gmx.com>
---
drivers/soc/renesas/r8a779a0-sysc.c | 6 ++++--
drivers/soc/renesas/rcar-sysc.c | 6 ++++--
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/drivers/soc/renesas/r8a779a0-sysc.c b/drivers/soc/renesas/r8a779a0-sysc.c
index d464ffa1be33..7410b9fa9846 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 n;
if (!area->name) {
/* Skip NULLified area */
continue;
}
- pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
+ n = strlen(area->name) + 1;
+ pd = kzalloc(sizeof(*pd) + n, GFP_KERNEL);
if (!pd) {
error = -ENOMEM;
goto out_put;
}
- strcpy(pd->name, area->name);
+ memcpy(pd->name, area->name, n);
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..b0a80de34c98 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 n;
if (!area->name) {
/* Skip NULLified area */
continue;
}
- pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
+ n = strlen(area->name) + 1;
+ pd = kzalloc(sizeof(*pd) + n, GFP_KERNEL);
if (!pd) {
error = -ENOMEM;
goto out_put;
}
- strcpy(pd->name, area->name);
+ memcpy(pd->name, area->name, n);
pd->genpd.name = pd->name;
pd->ch.chan_offs = area->chan_offs;
pd->ch.chan_bit = area->chan_bit;
--
2.25.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/3] drivers/soc/renesas: Prefer memcpy over strcpy
2021-08-08 12:50 ` [PATCH v4 2/3] drivers/soc/renesas: Prefer memcpy " Len Baker
@ 2021-08-08 15:35 ` Bernd Petrovitsch
2021-08-08 17:06 ` Christophe JAILLET
2021-08-11 9:38 ` Geert Uytterhoeven
1 sibling, 1 reply; 8+ messages in thread
From: Bernd Petrovitsch @ 2021-08-08 15:35 UTC (permalink / raw)
To: Len Baker, Andy Gross, Bjorn Andersson, Geert Uytterhoeven,
Magnus Damm, Santosh Shilimkar
Cc: Kees Cook, David Laight, Robin Murphy, linux-hardening,
linux-arm-msm, linux-kernel, linux-renesas-soc, linux-arm-kernel
Hi all!
On 08/08/2021 14:50, Len Baker 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. So, use memcpy() as a safe replacement.
>
> This is a previous step in the path to remove the strcpy() function
> entirely from the kernel.
>
> Signed-off-by: Len Baker <len.baker@gmx.com>
> ---
> drivers/soc/renesas/r8a779a0-sysc.c | 6 ++++--
> drivers/soc/renesas/rcar-sysc.c | 6 ++++--
> 2 files changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/soc/renesas/r8a779a0-sysc.c b/drivers/soc/renesas/r8a779a0-sysc.c
> index d464ffa1be33..7410b9fa9846 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 n;
>
> if (!area->name) {
> /* Skip NULLified area */
> continue;
> }
>
> - pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL)> + n = strlen(area->name) + 1;
> + pd = kzalloc(sizeof(*pd) + n, GFP_KERNEL);
Zeroing the allocated bytes is not needed since it's completly
overwritten with the strcpy()/memcpy().
> if (!pd) {
> error = -ENOMEM;
> goto out_put;
> }
>
> - strcpy(pd->name, area->name);
> + memcpy(pd->name, area->name, n);
> pd->genpd.name = pd->name;
> pd->pdr = area->pdr;
> pd->flags = area->flags;
And similar for the second hunk.
MfG,
Bernd
--
Bernd Petrovitsch Email : bernd@petrovitsch.priv.at
There is NO CLOUD, just other people's computers. - FSFE
LUGA : http://www.luga.at
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/3] drivers/soc/renesas: Prefer memcpy over strcpy
2021-08-08 15:35 ` Bernd Petrovitsch
@ 2021-08-08 17:06 ` Christophe JAILLET
2021-08-10 15:59 ` Len Baker
0 siblings, 1 reply; 8+ messages in thread
From: Christophe JAILLET @ 2021-08-08 17:06 UTC (permalink / raw)
To: Bernd Petrovitsch, Len Baker, Andy Gross, Bjorn Andersson,
Geert Uytterhoeven, Magnus Damm, Santosh Shilimkar
Cc: Kees Cook, David Laight, Robin Murphy, linux-hardening,
linux-arm-msm, linux-kernel, linux-renesas-soc, linux-arm-kernel
Hi,
Le 08/08/2021 à 17:35, Bernd Petrovitsch a écrit :
> Hi all!
>
> On 08/08/2021 14:50, Len Baker 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. So, use memcpy() as a safe replacement.
>>
>> This is a previous step in the path to remove the strcpy() function
>> entirely from the kernel.
>>
>> Signed-off-by: Len Baker <len.baker@gmx.com>
>> ---
>> drivers/soc/renesas/r8a779a0-sysc.c | 6 ++++--
>> drivers/soc/renesas/rcar-sysc.c | 6 ++++--
>> 2 files changed, 8 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/soc/renesas/r8a779a0-sysc.c b/drivers/soc/renesas/r8a779a0-sysc.c
>> index d464ffa1be33..7410b9fa9846 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 n;
>>
>> if (!area->name) {
>> /* Skip NULLified area */
>> continue;
>> }
>>
>> - pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
>> + n = strlen(area->name) + 1;
>> + pd = kzalloc(sizeof(*pd) + n, GFP_KERNEL);
> Zeroing the allocated bytes is not needed since it's completly
> overwritten with the strcpy()/memcpy().
The strcpy()/memcpy() only overwrites the pd->name field, not the whole
pd structure.
I think that it is needed to keep the kzalloc.
Just my 2c,
CJ
>> if (!pd) {
>> error = -ENOMEM;
>> goto out_put;
>> }
>>
>> - strcpy(pd->name, area->name);
>> + memcpy(pd->name, area->name, n);
>> pd->genpd.name = pd->name;
>> pd->pdr = area->pdr;
>> pd->flags = area->flags;
>
> And similar for the second hunk.
>
> MfG,
> Bernd
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/3] drivers/soc/renesas: Prefer memcpy over strcpy
2021-08-08 17:06 ` Christophe JAILLET
@ 2021-08-10 15:59 ` Len Baker
0 siblings, 0 replies; 8+ messages in thread
From: Len Baker @ 2021-08-10 15:59 UTC (permalink / raw)
To: Christophe JAILLET, Bernd Petrovitsch
Cc: Andy Gross, Bjorn Andersson, Geert Uytterhoeven, Magnus Damm,
Santosh Shilimkar, Len Baker, Kees Cook, David Laight,
Robin Murphy, linux-hardening, linux-arm-msm, linux-kernel,
linux-renesas-soc, linux-arm-kernel
Hi,
On Sun, Aug 08, 2021 at 07:06:30PM +0200, Christophe JAILLET wrote:
> Hi,
>
> Le 08/08/2021 à 17:35, Bernd Petrovitsch a écrit :
> > Hi all!
> >
> > On 08/08/2021 14:50, Len Baker 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. So, use memcpy() as a safe replacement.
> > >
> > > This is a previous step in the path to remove the strcpy() function
> > > entirely from the kernel.
> > >
> > > Signed-off-by: Len Baker <len.baker@gmx.com>
> > > ---
> > > drivers/soc/renesas/r8a779a0-sysc.c | 6 ++++--
> > > drivers/soc/renesas/rcar-sysc.c | 6 ++++--
> > > 2 files changed, 8 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/drivers/soc/renesas/r8a779a0-sysc.c b/drivers/soc/renesas/r8a779a0-sysc.c
> > > index d464ffa1be33..7410b9fa9846 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 n;
> > >
> > > if (!area->name) {
> > > /* Skip NULLified area */
> > > continue;
> > > }
> > >
> > > - pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
> > > + n = strlen(area->name) + 1;
> > > + pd = kzalloc(sizeof(*pd) + n, GFP_KERNEL);
> > Zeroing the allocated bytes is not needed since it's completly
> > overwritten with the strcpy()/memcpy().
>
> The strcpy()/memcpy() only overwrites the pd->name field, not the whole pd
> structure.
You are right.
> I think that it is needed to keep the kzalloc.
Yes, I think so. The kzalloc is needed to guarantee that the whole struct is
initialize (all the members are initialized with zeros).
Regards,
Len
>
> Just my 2c,
> CJ
>
> > > if (!pd) {
> > > error = -ENOMEM;
> > > goto out_put;
> > > }
> > >
> > > - strcpy(pd->name, area->name);
> > > + memcpy(pd->name, area->name, n);
> > > pd->genpd.name = pd->name;
> > > pd->pdr = area->pdr;
> > > pd->flags = area->flags;
> >
> > And similar for the second hunk.
> >
> > MfG,
> > Bernd
> >
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/3] drivers/soc/renesas: Prefer memcpy over strcpy
2021-08-08 12:50 ` [PATCH v4 2/3] drivers/soc/renesas: Prefer memcpy " Len Baker
2021-08-08 15:35 ` Bernd Petrovitsch
@ 2021-08-11 9:38 ` Geert Uytterhoeven
1 sibling, 0 replies; 8+ messages in thread
From: Geert Uytterhoeven @ 2021-08-11 9:38 UTC (permalink / raw)
To: Len Baker
Cc: Andy Gross, Bjorn Andersson, Magnus Damm, Santosh Shilimkar,
Kees Cook, David Laight, Robin Murphy, linux-hardening,
linux-arm-msm, Linux Kernel Mailing List, Linux-Renesas,
Linux ARM
On Sun, Aug 8, 2021 at 4:50 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. So, use memcpy() as a safe replacement.
>
> This is a previous step in the path to remove the strcpy() function
> entirely from the kernel.
>
> Signed-off-by: Len Baker <len.baker@gmx.com>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
i.e. will queue in renesas-devel for v5.15.
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] 8+ messages in thread
* [PATCH v4 3/3] drivers/soc/ti: Prefer strscpy over strcpy
2021-08-08 12:50 [PATCH v4 0/3] drivers/soc: Remove all strcpy() uses Len Baker
2021-08-08 12:50 ` [PATCH v4 1/3] drivers/soc/qcom: Prefer strscpy over strcpy Len Baker
2021-08-08 12:50 ` [PATCH v4 2/3] drivers/soc/renesas: Prefer memcpy " Len Baker
@ 2021-08-08 12:50 ` Len Baker
2 siblings, 0 replies; 8+ messages in thread
From: Len Baker @ 2021-08-08 12:50 UTC (permalink / raw)
To: Andy Gross, Bjorn Andersson, Geert Uytterhoeven, Magnus Damm,
Santosh Shilimkar
Cc: Len Baker, Kees Cook, David Laight, Robin Murphy,
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().
This is a previous step in the path to remove the strcpy() function
entirely from the kernel.
Signed-off-by: Len Baker <len.baker@gmx.com>
---
drivers/soc/ti/knav_dma.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 [flat|nested] 8+ messages in thread