All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next] ibmvnic: fix kernel build warning in strncpy
@ 2021-06-11 16:05 Lijun Pan
  2021-06-11 16:28 ` Shannon Nelson
  0 siblings, 1 reply; 4+ messages in thread
From: Lijun Pan @ 2021-06-11 16:05 UTC (permalink / raw)
  To: netdev; +Cc: Lijun Pan

drivers/net/ethernet/ibm/ibmvnic.c: In function ‘handle_vpd_rsp’:
drivers/net/ethernet/ibm/ibmvnic.c:4393:3: warning: ‘strncpy’ output truncated before terminating nul copying 3 bytes from a string of the same length [-Wstringop-truncation]
 4393 |   strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Lijun Pan <lijunp213@gmail.com>
---
 drivers/net/ethernet/ibm/ibmvnic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/ibm/ibmvnic.c b/drivers/net/ethernet/ibm/ibmvnic.c
index 497f1a7da70b..2675b2301ed7 100644
--- a/drivers/net/ethernet/ibm/ibmvnic.c
+++ b/drivers/net/ethernet/ibm/ibmvnic.c
@@ -4390,7 +4390,7 @@ static void handle_vpd_rsp(union ibmvnic_crq *crq,
 
 complete:
 	if (adapter->fw_version[0] == '\0')
-		strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
+		memcpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
 	complete(&adapter->fw_done);
 }
 
-- 
2.23.0


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

* Re: [PATCH net-next] ibmvnic: fix kernel build warning in strncpy
  2021-06-11 16:05 [PATCH net-next] ibmvnic: fix kernel build warning in strncpy Lijun Pan
@ 2021-06-11 16:28 ` Shannon Nelson
  2021-06-11 18:36   ` Lijun Pan
  0 siblings, 1 reply; 4+ messages in thread
From: Shannon Nelson @ 2021-06-11 16:28 UTC (permalink / raw)
  To: Lijun Pan, netdev

On 6/11/21 9:05 AM, Lijun Pan wrote:
> drivers/net/ethernet/ibm/ibmvnic.c: In function ‘handle_vpd_rsp’:
> drivers/net/ethernet/ibm/ibmvnic.c:4393:3: warning: ‘strncpy’ output truncated before terminating nul copying 3 bytes from a string of the same length [-Wstringop-truncation]
>   4393 |   strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
>        |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Signed-off-by: Lijun Pan <lijunp213@gmail.com>
> ---
>   drivers/net/ethernet/ibm/ibmvnic.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/net/ethernet/ibm/ibmvnic.c b/drivers/net/ethernet/ibm/ibmvnic.c
> index 497f1a7da70b..2675b2301ed7 100644
> --- a/drivers/net/ethernet/ibm/ibmvnic.c
> +++ b/drivers/net/ethernet/ibm/ibmvnic.c
> @@ -4390,7 +4390,7 @@ static void handle_vpd_rsp(union ibmvnic_crq *crq,
>   
>   complete:
>   	if (adapter->fw_version[0] == '\0')
> -		strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
> +		memcpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
>   	complete(&adapter->fw_done);
>   }
>   

This doesn't fix the real problem.  The error message is saying that 
there is no string terminating '\0' byte getting set after the "N/A" 
string, meaning that there could be garbage in the buffer after the 
string that could allow for surprising and bad things to happen when 
that string is used later, including buffer overruns that can cause 
stack smash or other memory munging.

Better would be to use strlcpy() with a limiter of 
sizeof(adapter->fw_version).

sln

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

* Re: [PATCH net-next] ibmvnic: fix kernel build warning in strncpy
  2021-06-11 16:28 ` Shannon Nelson
@ 2021-06-11 18:36   ` Lijun Pan
  2021-06-11 18:59     ` Shannon Nelson
  0 siblings, 1 reply; 4+ messages in thread
From: Lijun Pan @ 2021-06-11 18:36 UTC (permalink / raw)
  To: Shannon Nelson; +Cc: Networking

On Fri, Jun 11, 2021 at 11:28 AM Shannon Nelson <snelson@pensando.io> wrote:
>
> On 6/11/21 9:05 AM, Lijun Pan wrote:
> > drivers/net/ethernet/ibm/ibmvnic.c: In function ‘handle_vpd_rsp’:
> > drivers/net/ethernet/ibm/ibmvnic.c:4393:3: warning: ‘strncpy’ output truncated before terminating nul copying 3 bytes from a string of the same length [-Wstringop-truncation]
> >   4393 |   strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
> >        |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >
> > Signed-off-by: Lijun Pan <lijunp213@gmail.com>
> > ---
> >   drivers/net/ethernet/ibm/ibmvnic.c | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/net/ethernet/ibm/ibmvnic.c b/drivers/net/ethernet/ibm/ibmvnic.c
> > index 497f1a7da70b..2675b2301ed7 100644
> > --- a/drivers/net/ethernet/ibm/ibmvnic.c
> > +++ b/drivers/net/ethernet/ibm/ibmvnic.c
> > @@ -4390,7 +4390,7 @@ static void handle_vpd_rsp(union ibmvnic_crq *crq,
> >
> >   complete:
> >       if (adapter->fw_version[0] == '\0')
> > -             strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
> > +             memcpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
> >       complete(&adapter->fw_done);
> >   }
> >
>
> This doesn't fix the real problem.  The error message is saying that
> there is no string terminating '\0' byte getting set after the "N/A"
> string, meaning that there could be garbage in the buffer after the
> string that could allow for surprising and bad things to happen when
> that string is used later, including buffer overruns that can cause
> stack smash or other memory munging.
>
> Better would be to use strlcpy() with a limiter of
> sizeof(adapter->fw_version).
>
> sln

Thanks for the tip. I looked up both strscpy and strlcpy. It seems nowadays
strscpy is preferred.

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

* Re: [PATCH net-next] ibmvnic: fix kernel build warning in strncpy
  2021-06-11 18:36   ` Lijun Pan
@ 2021-06-11 18:59     ` Shannon Nelson
  0 siblings, 0 replies; 4+ messages in thread
From: Shannon Nelson @ 2021-06-11 18:59 UTC (permalink / raw)
  To: Lijun Pan; +Cc: Networking

On 6/11/21 11:36 AM, Lijun Pan wrote:
> On Fri, Jun 11, 2021 at 11:28 AM Shannon Nelson <snelson@pensando.io> wrote:
>> On 6/11/21 9:05 AM, Lijun Pan wrote:
>>> drivers/net/ethernet/ibm/ibmvnic.c: In function ‘handle_vpd_rsp’:
>>> drivers/net/ethernet/ibm/ibmvnic.c:4393:3: warning: ‘strncpy’ output truncated before terminating nul copying 3 bytes from a string of the same length [-Wstringop-truncation]
>>>    4393 |   strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
>>>         |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>
>>> Signed-off-by: Lijun Pan <lijunp213@gmail.com>
>>> ---
>>>    drivers/net/ethernet/ibm/ibmvnic.c | 2 +-
>>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/net/ethernet/ibm/ibmvnic.c b/drivers/net/ethernet/ibm/ibmvnic.c
>>> index 497f1a7da70b..2675b2301ed7 100644
>>> --- a/drivers/net/ethernet/ibm/ibmvnic.c
>>> +++ b/drivers/net/ethernet/ibm/ibmvnic.c
>>> @@ -4390,7 +4390,7 @@ static void handle_vpd_rsp(union ibmvnic_crq *crq,
>>>
>>>    complete:
>>>        if (adapter->fw_version[0] == '\0')
>>> -             strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
>>> +             memcpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
>>>        complete(&adapter->fw_done);
>>>    }
>>>
>> This doesn't fix the real problem.  The error message is saying that
>> there is no string terminating '\0' byte getting set after the "N/A"
>> string, meaning that there could be garbage in the buffer after the
>> string that could allow for surprising and bad things to happen when
>> that string is used later, including buffer overruns that can cause
>> stack smash or other memory munging.
>>
>> Better would be to use strlcpy() with a limiter of
>> sizeof(adapter->fw_version).
>>
>> sln
> Thanks for the tip. I looked up both strscpy and strlcpy. It seems nowadays
> strscpy is preferred.

Sure, that works too.
sln


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

end of thread, other threads:[~2021-06-11 18:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-11 16:05 [PATCH net-next] ibmvnic: fix kernel build warning in strncpy Lijun Pan
2021-06-11 16:28 ` Shannon Nelson
2021-06-11 18:36   ` Lijun Pan
2021-06-11 18:59     ` Shannon Nelson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.