[v1,1/2] pci: fix allowing underflow when parsing PCI id

Message ID 20200513104751.46466-2-grive@u256.net (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers
Series pci: a comment and a minor fix |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-nxp-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-testing fail Testing issues

Commit Message

Gaëtan Rivet May 13, 2020, 10:47 a.m. UTC
  The function strtoul will not return ERANGE if the input is negative, as
one might expect.

   0000:-FFFFFFFFFFFFFFFB:00.0

is not a better way to write 0000:05:00.0.
To simplify checking for '-', forbid using spaces before the field value.

   0000: 00:   2c.0

Should not be accepted.

Fixes: af75078fece3 ("first public release")
Cc: stable@dpdk.org
Signed-off-by: Gaetan Rivet <grive@u256.net>
---
 lib/librte_pci/rte_pci.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
  

Comments

Stojaczyk, Dariusz May 14, 2020, 8:52 a.m. UTC | #1
> -----Original Message-----
> From: dev <dev-bounces@dpdk.org> On Behalf Of Gaetan Rivet
> Sent: Wednesday, May 13, 2020 12:48 PM
> To: dev@dpdk.org
> Cc: stable@dpdk.org
> Subject: [dpdk-dev] [PATCH v1 1/2] pci: fix allowing underflow when parsing PCI
> id
> 
> The function strtoul will not return ERANGE if the input is negative, as
> one might expect.
> 
>    0000:-FFFFFFFFFFFFFFFB:00.0
> 
> is not a better way to write 0000:05:00.0.
> To simplify checking for '-', forbid using spaces before the field value.
> 
>    0000: 00:   2c.0
> 
> Should not be accepted.
> 
> Fixes: af75078fece3 ("first public release")
> Cc: stable@dpdk.org
> Signed-off-by: Gaetan Rivet <grive@u256.net>
> ---

Acked-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
  
David Marchand May 19, 2020, 9:17 a.m. UTC | #2
On Wed, May 13, 2020 at 12:48 PM Gaetan Rivet <grive@u256.net> wrote:
>
> The function strtoul will not return ERANGE if the input is negative, as
> one might expect.
>
>    0000:-FFFFFFFFFFFFFFFB:00.0
>
> is not a better way to write 0000:05:00.0.
> To simplify checking for '-', forbid using spaces before the field value.
>
>    0000: 00:   2c.0
>
> Should not be accepted.
>
> Fixes: af75078fece3 ("first public release")
> Cc: stable@dpdk.org

Not sure about backporting this one, will let stable maintainers
reconsider this.


> Signed-off-by: Gaetan Rivet <grive@u256.net>
Acked-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>

> ---
>  lib/librte_pci/rte_pci.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/lib/librte_pci/rte_pci.c b/lib/librte_pci/rte_pci.c
> index d1ab6b414..e4ecdc32f 100644
> --- a/lib/librte_pci/rte_pci.c
> +++ b/lib/librte_pci/rte_pci.c
> @@ -35,6 +35,12 @@ get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
>         if (*in == '\0')
>                 return NULL;
>
> +       /* PCI field starting with spaces is forbidden.
> +        * Negative wrap-around is not reported as an error by strtoul.
> +        */
> +       if (*in == ' ' || *in == '-')
> +               return NULL;
> +
>         errno = 0;
>         val = strtoul(in, &end, 16);
>         if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
> @@ -70,6 +76,12 @@ pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
>         unsigned long val;
>         char *end;
>
> +       /* PCI id starting with spaces is forbidden.
> +        * Negative wrap-around is not reported as an error by strtoul.
> +        */
> +       if (*in == ' ' || *in == '-')
> +               return EINVAL;

Should be -EINVAL, fixed.

> +
>         errno = 0;
>         val = strtoul(in, &end, 16);
>         if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
> --
> 2.26.2
>

Applied, thanks.
  

Patch

diff --git a/lib/librte_pci/rte_pci.c b/lib/librte_pci/rte_pci.c
index d1ab6b414..e4ecdc32f 100644
--- a/lib/librte_pci/rte_pci.c
+++ b/lib/librte_pci/rte_pci.c
@@ -35,6 +35,12 @@  get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
 	if (*in == '\0')
 		return NULL;
 
+	/* PCI field starting with spaces is forbidden.
+	 * Negative wrap-around is not reported as an error by strtoul.
+	 */
+	if (*in == ' ' || *in == '-')
+		return NULL;
+
 	errno = 0;
 	val = strtoul(in, &end, 16);
 	if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
@@ -70,6 +76,12 @@  pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
 	unsigned long val;
 	char *end;
 
+	/* PCI id starting with spaces is forbidden.
+	 * Negative wrap-around is not reported as an error by strtoul.
+	 */
+	if (*in == ' ' || *in == '-')
+		return EINVAL;
+
 	errno = 0;
 	val = strtoul(in, &end, 16);
 	if (errno != 0 || end[0] != ':' || val > UINT16_MAX)