* [PATCH net-next] octeontx2-pf: Treat truncation of IRQ name as an error
@ 2024-05-01 18:27 Simon Horman
2024-05-01 19:46 ` Andrew Lunn
2024-05-03 6:20 ` [EXTERNAL] " Geethasowjanya Akula
0 siblings, 2 replies; 6+ messages in thread
From: Simon Horman @ 2024-05-01 18:27 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Sunil Goutham, Geethasowjanya Akula, Subbaraya Sundeep,
Hariprasad Kelam, Dan Carpenter, netdev
According to GCC, the constriction of irq_name in otx2_open()
may, theoretically, be truncated.
This patch takes the approach of treating such a situation as an error
which it detects by making use of the return value of snprintf, which is
the total number of bytes, including the trailing '\0', that would have
been written.
Based on the approach taken to a similar problem in
commit 54b909436ede ("rtc: fix snprintf() checking in is_rtc_hctosys()")
Flagged by gcc-13 W=1 builds as:
.../otx2_pf.c:1933:58: warning: 'snprintf' output may be truncated before the last format character [-Wformat-truncation=]
1933 | snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev->name,
| ^
.../otx2_pf.c:1933:17: note: 'snprintf' output between 8 and 33 bytes into a destination of size 32
1933 | snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev->name,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1934 | qidx);
| ~~~~~
Compile tested only.
Signed-off-by: Simon Horman <horms@kernel.org>
---
drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c
index 6a44dacff508..14bccff0ee5c 100644
--- a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c
+++ b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c
@@ -1886,9 +1886,17 @@ int otx2_open(struct net_device *netdev)
vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
+ int name_len;
- snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev->name,
- qidx);
+ name_len = snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d",
+ pf->netdev->name, qidx);
+ if (name_len >= NAME_SIZE) {
+ dev_err(pf->dev,
+ "RVUPF%d: IRQ registration failed for CQ%d, irq name is too long\n",
+ rvu_get_pf(pf->pcifunc), qidx);
+ err = -EINVAL;
+ goto err_free_cints;
+ }
err = request_irq(pci_irq_vector(pf->pdev, vec),
otx2_cq_intr_handler, 0, irq_name,
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH net-next] octeontx2-pf: Treat truncation of IRQ name as an error
2024-05-01 18:27 [PATCH net-next] octeontx2-pf: Treat truncation of IRQ name as an error Simon Horman
@ 2024-05-01 19:46 ` Andrew Lunn
2024-05-01 20:11 ` Simon Horman
2024-05-03 6:20 ` [EXTERNAL] " Geethasowjanya Akula
1 sibling, 1 reply; 6+ messages in thread
From: Andrew Lunn @ 2024-05-01 19:46 UTC (permalink / raw)
To: Simon Horman
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Sunil Goutham, Geethasowjanya Akula, Subbaraya Sundeep,
Hariprasad Kelam, Dan Carpenter, netdev
On Wed, May 01, 2024 at 07:27:09PM +0100, Simon Horman wrote:
> According to GCC, the constriction of irq_name in otx2_open()
> may, theoretically, be truncated.
>
> This patch takes the approach of treating such a situation as an error
> which it detects by making use of the return value of snprintf, which is
> the total number of bytes, including the trailing '\0', that would have
> been written.
> + name_len = snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d",
> + pf->netdev->name, qidx);
> + if (name_len >= NAME_SIZE) {
You say name_len includes the trailing \0. So you should be able to
get NAME_SIZE bytes into an NAME_SIZE length array? So i think this
can be >, not >= ?
Andrew
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH net-next] octeontx2-pf: Treat truncation of IRQ name as an error
2024-05-01 19:46 ` Andrew Lunn
@ 2024-05-01 20:11 ` Simon Horman
2024-05-01 20:21 ` Andrew Lunn
0 siblings, 1 reply; 6+ messages in thread
From: Simon Horman @ 2024-05-01 20:11 UTC (permalink / raw)
To: Andrew Lunn
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Sunil Goutham, Geethasowjanya Akula, Subbaraya Sundeep,
Hariprasad Kelam, Dan Carpenter, netdev
On Wed, May 01, 2024 at 09:46:15PM +0200, Andrew Lunn wrote:
> On Wed, May 01, 2024 at 07:27:09PM +0100, Simon Horman wrote:
> > According to GCC, the constriction of irq_name in otx2_open()
> > may, theoretically, be truncated.
> >
> > This patch takes the approach of treating such a situation as an error
> > which it detects by making use of the return value of snprintf, which is
> > the total number of bytes, including the trailing '\0', that would have
> > been written.
> > + name_len = snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d",
> > + pf->netdev->name, qidx);
> > + if (name_len >= NAME_SIZE) {
>
> You say name_len includes the trailing \0. So you should be able to
> get NAME_SIZE bytes into an NAME_SIZE length array? So i think this
> can be >, not >= ?
Sorry, I misspoke.
name_len excludes the trailing \0.
--
pw-bot: changes-requested
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH net-next] octeontx2-pf: Treat truncation of IRQ name as an error
2024-05-01 20:11 ` Simon Horman
@ 2024-05-01 20:21 ` Andrew Lunn
2024-05-01 20:33 ` Simon Horman
0 siblings, 1 reply; 6+ messages in thread
From: Andrew Lunn @ 2024-05-01 20:21 UTC (permalink / raw)
To: Simon Horman
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Sunil Goutham, Geethasowjanya Akula, Subbaraya Sundeep,
Hariprasad Kelam, Dan Carpenter, netdev
On Wed, May 01, 2024 at 09:11:46PM +0100, Simon Horman wrote:
> On Wed, May 01, 2024 at 09:46:15PM +0200, Andrew Lunn wrote:
> > On Wed, May 01, 2024 at 07:27:09PM +0100, Simon Horman wrote:
> > > According to GCC, the constriction of irq_name in otx2_open()
> > > may, theoretically, be truncated.
> > >
> > > This patch takes the approach of treating such a situation as an error
> > > which it detects by making use of the return value of snprintf, which is
> > > the total number of bytes, including the trailing '\0', that would have
> > > been written.
> > > + name_len = snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d",
> > > + pf->netdev->name, qidx);
> > > + if (name_len >= NAME_SIZE) {
> >
> > You say name_len includes the trailing \0. So you should be able to
> > get NAME_SIZE bytes into an NAME_SIZE length array? So i think this
> > can be >, not >= ?
>
> Sorry, I misspoke.
> name_len excludes the trailing \0.
The man page say:
Upon successful return, these functions return the number of characters
printed (excluding the null byte used to end output to strings).
The functions snprintf() and vsnprintf() do not write more than size
bytes (including the terminating null byte ('\0')). If the output was
truncated due to this limit, then the return value is the number of
characters (excluding the terminating null byte) which would have been
written to the final string if enough space had been available. Thus,
a return value of size or more means that the output was truncated.
(See also below under NOTES.)
Assuming the kernel snprintf() follows this, your condition is
correct. So once the commit message is corrected, please add:
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Andrew
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH net-next] octeontx2-pf: Treat truncation of IRQ name as an error
2024-05-01 20:21 ` Andrew Lunn
@ 2024-05-01 20:33 ` Simon Horman
0 siblings, 0 replies; 6+ messages in thread
From: Simon Horman @ 2024-05-01 20:33 UTC (permalink / raw)
To: Andrew Lunn
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Sunil Goutham, Geethasowjanya Akula, Subbaraya Sundeep,
Hariprasad Kelam, Dan Carpenter, netdev
On Wed, May 01, 2024 at 10:21:30PM +0200, Andrew Lunn wrote:
> On Wed, May 01, 2024 at 09:11:46PM +0100, Simon Horman wrote:
> > On Wed, May 01, 2024 at 09:46:15PM +0200, Andrew Lunn wrote:
> > > On Wed, May 01, 2024 at 07:27:09PM +0100, Simon Horman wrote:
> > > > According to GCC, the constriction of irq_name in otx2_open()
> > > > may, theoretically, be truncated.
> > > >
> > > > This patch takes the approach of treating such a situation as an error
> > > > which it detects by making use of the return value of snprintf, which is
> > > > the total number of bytes, including the trailing '\0', that would have
> > > > been written.
> > > > + name_len = snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d",
> > > > + pf->netdev->name, qidx);
> > > > + if (name_len >= NAME_SIZE) {
> > >
> > > You say name_len includes the trailing \0. So you should be able to
> > > get NAME_SIZE bytes into an NAME_SIZE length array? So i think this
> > > can be >, not >= ?
> >
> > Sorry, I misspoke.
> > name_len excludes the trailing \0.
>
> The man page say:
>
> Upon successful return, these functions return the number of characters
> printed (excluding the null byte used to end output to strings).
>
> The functions snprintf() and vsnprintf() do not write more than size
> bytes (including the terminating null byte ('\0')). If the output was
> truncated due to this limit, then the return value is the number of
> characters (excluding the terminating null byte) which would have been
> written to the final string if enough space had been available. Thus,
> a return value of size or more means that the output was truncated.
> (See also below under NOTES.)
>
> Assuming the kernel snprintf() follows this, your condition is
> correct. So once the commit message is corrected, please add:
>
> Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Thanks Andrew,
According to the documentation, the Kernel implementation
it matches the return value scheme described above.
For the record, the text in lib/vsprintf.c says:
* The return value is the number of characters which would be
* generated for the given input, excluding the trailing null,
* as per ISO C99. If the return is greater than or equal to
* @size, the resulting string is truncated.
So I think the code is correct but my patch description text was wrong.
As you suggest, I'll fix that in v2.
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [EXTERNAL] [PATCH net-next] octeontx2-pf: Treat truncation of IRQ name as an error
2024-05-01 18:27 [PATCH net-next] octeontx2-pf: Treat truncation of IRQ name as an error Simon Horman
2024-05-01 19:46 ` Andrew Lunn
@ 2024-05-03 6:20 ` Geethasowjanya Akula
1 sibling, 0 replies; 6+ messages in thread
From: Geethasowjanya Akula @ 2024-05-03 6:20 UTC (permalink / raw)
To: Simon Horman, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni
Cc: Sunil Kovvuri Goutham, Subbaraya Sundeep Bhatta, Hariprasad Kelam,
Dan Carpenter, netdev@vger.kernel.org
> -----Original Message-----
> From: Simon Horman <horms@kernel.org>
> Sent: Wednesday, May 1, 2024 11:57 PM
> To: David S. Miller <davem@davemloft.net>; Eric Dumazet
> <edumazet@google.com>; Jakub Kicinski <kuba@kernel.org>; Paolo Abeni
> <pabeni@redhat.com>
> Cc: Sunil Kovvuri Goutham <sgoutham@marvell.com>; Geethasowjanya
> Akula <gakula@marvell.com>; Subbaraya Sundeep Bhatta
> <sbhatta@marvell.com>; Hariprasad Kelam <hkelam@marvell.com>; Dan
> Carpenter <dan.carpenter@linaro.org>; netdev@vger.kernel.org
> Subject: [EXTERNAL] [PATCH net-next] octeontx2-pf: Treat truncation of IRQ
> name as an error
>
> ----------------------------------------------------------------------
> According to GCC, the constriction of irq_name in otx2_open() may,
> theoretically, be truncated.
>
> This patch takes the approach of treating such a situation as an error which it
> detects by making use of the return value of snprintf, which is the total
> number of bytes, including the trailing '\0', that would have been written.
>
> Based on the approach taken to a similar problem in commit 54b909436ede
> ("rtc: fix snprintf() checking in is_rtc_hctosys()")
>
> Flagged by gcc-13 W=1 builds as:
>
> .../otx2_pf.c:1933:58: warning: 'snprintf' output may be truncated before the
> last format character [-Wformat-truncation=]
> 1933 | snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev-
> >name,
> | ^
> .../otx2_pf.c:1933:17: note: 'snprintf' output between 8 and 33 bytes into a
> destination of size 32
> 1933 | snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev-
> >name,
> |
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 1934 | qidx);
> | ~~~~~
>
> Compile tested only.
>
> Signed-off-by: Simon Horman <horms@kernel.org>
> ---
> drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c
> b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c
> index 6a44dacff508..14bccff0ee5c 100644
> --- a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c
> +++ b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c
> @@ -1886,9 +1886,17 @@ int otx2_open(struct net_device *netdev)
> vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
> for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
> irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
> + int name_len;
>
> - snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev-
> >name,
> - qidx);
> + name_len = snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d",
> + pf->netdev->name, qidx);
> + if (name_len >= NAME_SIZE) {
> + dev_err(pf->dev,
> + "RVUPF%d: IRQ registration failed for CQ%d,
> irq name is too long\n",
> + rvu_get_pf(pf->pcifunc), qidx);
> + err = -EINVAL;
> + goto err_free_cints;
> + }
>
> err = request_irq(pci_irq_vector(pf->pdev, vec),
> otx2_cq_intr_handler, 0, irq_name,
Tested-by: Geetha sowjanya <gakula@marvell.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-05-03 6:20 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-01 18:27 [PATCH net-next] octeontx2-pf: Treat truncation of IRQ name as an error Simon Horman
2024-05-01 19:46 ` Andrew Lunn
2024-05-01 20:11 ` Simon Horman
2024-05-01 20:21 ` Andrew Lunn
2024-05-01 20:33 ` Simon Horman
2024-05-03 6:20 ` [EXTERNAL] " Geethasowjanya Akula
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).