Netdev List
 help / color / mirror / Atom feed
From: David Miller <davem@davemloft.net>
To: jakub.kicinski@netronome.com
Cc: oss-drivers@netronome.com, netdev@vger.kernel.org
Subject: Re: [PATCH net-next] pktgen: convert safe uses of strncpy() to strcpy() to avoid string truncation warning
Date: Thu, 19 Jul 2018 07:24:33 +0900 (KST)	[thread overview]
Message-ID: <20180719.072433.2037475976239479239.davem@davemloft.net> (raw)
In-Reply-To: <20180717213224.14695-1-jakub.kicinski@netronome.com>

From: Jakub Kicinski <jakub.kicinski@netronome.com>
Date: Tue, 17 Jul 2018 14:32:24 -0700

> GCC 8 complains:
> 
> net/core/pktgen.c: In function ‘pktgen_if_write’:
> net/core/pktgen.c:1419:4: warning: ‘strncpy’ output may be truncated copying between 0 and 31 bytes from a string of length 127 [-Wstringop-truncation]
>     strncpy(pkt_dev->src_max, buf, len);
>     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> net/core/pktgen.c:1399:4: warning: ‘strncpy’ output may be truncated copying between 0 and 31 bytes from a string of length 127 [-Wstringop-truncation]
>     strncpy(pkt_dev->src_min, buf, len);
>     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> net/core/pktgen.c:1290:4: warning: ‘strncpy’ output may be truncated copying between 0 and 31 bytes from a string of length 127 [-Wstringop-truncation]
>     strncpy(pkt_dev->dst_max, buf, len);
>     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> net/core/pktgen.c:1268:4: warning: ‘strncpy’ output may be truncated copying between 0 and 31 bytes from a string of length 127 [-Wstringop-truncation]
>     strncpy(pkt_dev->dst_min, buf, len);
>     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> There is no bug here, but the code is not perfect either.  It copies
> sizeof(pkt_dev->/member/) - 1 from user space into buf, and then does
> a strcmp(pkt_dev->/member/, buf) hence assuming buf will be null-terminated
> and shorter than pkt_dev->/member/ (pkt_dev->/member/ is never
> explicitly null-terminated, and strncpy() doesn't have to null-terminate
> so the assumption must be on buf).  The use of strncpy() without explicit
> null-termination looks suspicious.  Convert to use straight strcpy().
> 
> strncpy() would also null-pad the output, but that's clearly unnecessary
> since the author calls memset(pkt_dev->/member/, 0, sizeof(..)); prior
> to strncpy(), anyway.
> 
> While at it format the code for "dst_min", "dst_max", "src_min" and
> "src_max" in the same way by removing extra new lines in one case.
> 
> Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
> Reviewed-by: Jiong Wang <jiong.wang@netronome.com>

Applied, thanks Jakub.

      reply	other threads:[~2018-07-18 23:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-17 21:32 [PATCH net-next] pktgen: convert safe uses of strncpy() to strcpy() to avoid string truncation warning Jakub Kicinski
2018-07-18 22:24 ` David Miller [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180719.072433.2037475976239479239.davem@davemloft.net \
    --to=davem@davemloft.net \
    --cc=jakub.kicinski@netronome.com \
    --cc=netdev@vger.kernel.org \
    --cc=oss-drivers@netronome.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox