2019-11-19 05:31:28

by Greg Kroah-Hartman

[permalink] [raw]
Subject: [PATCH 4.19 079/422] i40e: use correct length for strncpy

From: Mitch Williams <[email protected]>

[ Upstream commit 7eb74ff891b4e94b8bac48f648a21e4b94ddee64 ]

Caught by GCC 8. When we provide a length for strncpy, we should not
include the terminating null. So we must tell it one less than the size
of the destination buffer.

Signed-off-by: Mitch Williams <[email protected]>
Tested-by: Andrew Bowers <[email protected]>
Signed-off-by: Jeff Kirsher <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
---
drivers/net/ethernet/intel/i40e/i40e_ptp.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/intel/i40e/i40e_ptp.c b/drivers/net/ethernet/intel/i40e/i40e_ptp.c
index 35f2866b38c6b..1199f0502d6d5 100644
--- a/drivers/net/ethernet/intel/i40e/i40e_ptp.c
+++ b/drivers/net/ethernet/intel/i40e/i40e_ptp.c
@@ -694,7 +694,8 @@ static long i40e_ptp_create_clock(struct i40e_pf *pf)
if (!IS_ERR_OR_NULL(pf->ptp_clock))
return 0;

- strncpy(pf->ptp_caps.name, i40e_driver_name, sizeof(pf->ptp_caps.name));
+ strncpy(pf->ptp_caps.name, i40e_driver_name,
+ sizeof(pf->ptp_caps.name) - 1);
pf->ptp_caps.owner = THIS_MODULE;
pf->ptp_caps.max_adj = 999999999;
pf->ptp_caps.n_ext_ts = 0;
--
2.20.1




2019-11-21 10:36:41

by Pavel Machek

[permalink] [raw]
Subject: Re: [PATCH 4.19 079/422] i40e: use correct length for strncpy

Hi!

> From: Mitch Williams <[email protected]>
>
> [ Upstream commit 7eb74ff891b4e94b8bac48f648a21e4b94ddee64 ]
>
> Caught by GCC 8. When we provide a length for strncpy, we should not
> include the terminating null. So we must tell it one less than the size
> of the destination buffer.

> +++ b/drivers/net/ethernet/intel/i40e/i40e_ptp.c
> @@ -694,7 +694,8 @@ static long i40e_ptp_create_clock(struct i40e_pf *pf)
> if (!IS_ERR_OR_NULL(pf->ptp_clock))
> return 0;
>
> - strncpy(pf->ptp_caps.name, i40e_driver_name, sizeof(pf->ptp_caps.name));
> + strncpy(pf->ptp_caps.name, i40e_driver_name,
> + sizeof(pf->ptp_caps.name) - 1);
> pf->ptp_caps.owner = THIS_MODULE;
> pf->ptp_caps.max_adj = 999999999;
> pf->ptp_caps.n_ext_ts = 0;

So... pf is allocated with kzalloc, which will provide the null
termination... so the code is okay.

On the other hand, the = 0 below is unneeded by the same logic, so
this is a bit confusing.

Best regards,
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html


Attachments:
(No filename) (1.12 kB)
signature.asc (188.00 B)
Digital signature
Download all attachments

2019-11-21 15:36:36

by Williams, Mitch A

[permalink] [raw]
Subject: RE: [PATCH 4.19 079/422] i40e: use correct length for strncpy



> -----Original Message-----
> From: Pavel Machek <[email protected]>
> Sent: Thursday, November 21, 2019 2:35 AM
> To: Greg Kroah-Hartman <[email protected]>
> Cc: [email protected]; [email protected]; Williams, Mitch A
> <[email protected]>; Bowers, AndrewX <[email protected]>;
> Kirsher, Jeffrey T <[email protected]>; Sasha Levin
> <[email protected]>
> Subject: Re: [PATCH 4.19 079/422] i40e: use correct length for strncpy
>
> Hi!
>
> > From: Mitch Williams <[email protected]>
> >
> > [ Upstream commit 7eb74ff891b4e94b8bac48f648a21e4b94ddee64 ]
> >
> > Caught by GCC 8. When we provide a length for strncpy, we should not
> > include the terminating null. So we must tell it one less than the size
> > of the destination buffer.
>
> > +++ b/drivers/net/ethernet/intel/i40e/i40e_ptp.c
> > @@ -694,7 +694,8 @@ static long i40e_ptp_create_clock(struct i40e_pf *pf)
> > if (!IS_ERR_OR_NULL(pf->ptp_clock))
> > return 0;
> >
> > - strncpy(pf->ptp_caps.name, i40e_driver_name, sizeof(pf->ptp_caps.name));
> > + strncpy(pf->ptp_caps.name, i40e_driver_name,
> > + sizeof(pf->ptp_caps.name) - 1);
> > pf->ptp_caps.owner = THIS_MODULE;
> > pf->ptp_caps.max_adj = 999999999;
> > pf->ptp_caps.n_ext_ts = 0;
>
> So... pf is allocated with kzalloc, which will provide the null
> termination... so the code is okay.
>
> On the other hand, the = 0 below is unneeded by the same logic, so
> this is a bit confusing.
>
> Best regards,
> Pavel

Thanks for catching this, Pavel. Aleksandr Loktionov owns this driver now. He can get this taken care of.
-Mitch


> --
> (english) http://www.livejournal.com/~pavelmachek
> (cesky, pictures)
> http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

2019-11-21 23:55:55

by Jeff Kirsher

[permalink] [raw]
Subject: Re: [PATCH 4.19 079/422] i40e: use correct length for strncpy

On Thu, 2019-11-21 at 11:35 +0100, Pavel Machek wrote:
> > From: Mitch Williams <[email protected]>
> >
> > [ Upstream commit 7eb74ff891b4e94b8bac48f648a21e4b94ddee64 ]
> >
> > Caught by GCC 8. When we provide a length for strncpy, we should not
> > include the terminating null. So we must tell it one less than the size
> > of the destination buffer.
>
> > +++ b/drivers/net/ethernet/intel/i40e/i40e_ptp.c
> > @@ -694,7 +694,8 @@ static long i40e_ptp_create_clock(struct i40e_pf
> *pf)
> > if (!IS_ERR_OR_NULL(pf->ptp_clock))
> > return 0;
> >
> > - strncpy(pf->ptp_caps.name, i40e_driver_name, sizeof(pf-
> >ptp_caps.name));
> > + strncpy(pf->ptp_caps.name, i40e_driver_name,
> > + sizeof(pf->ptp_caps.name) - 1);
> > pf->ptp_caps.owner = THIS_MODULE;
> > pf->ptp_caps.max_adj = 999999999;
> > pf->ptp_caps.n_ext_ts = 0;
>
> So... pf is allocated with kzalloc, which will provide the null
> termination... so the code is okay.
>
> On the other hand, the = 0 below is unneeded by the same logic, so
> this is a bit confusing.

Thanks for the catch, we are putting together a follow-on patch to cleanup
up the unneeded code.


Attachments:
signature.asc (849.00 B)
This is a digitally signed message part