From patchwork Wed May 13 10:47:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ga=C3=ABtan_Rivet?= X-Patchwork-Id: 70160 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 616F9A034F; Wed, 13 May 2020 12:48:07 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 78AD61D51C; Wed, 13 May 2020 12:48:03 +0200 (CEST) Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by dpdk.org (Postfix) with ESMTP id ED51F1D15C; Wed, 13 May 2020 12:47:59 +0200 (CEST) X-Originating-IP: 86.246.31.132 Received: from inocybe.home (lfbn-idf2-1-566-132.w86-246.abo.wanadoo.fr [86.246.31.132]) (Authenticated sender: grive@u256.net) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id AC29C1C0003; Wed, 13 May 2020 10:47:59 +0000 (UTC) From: Gaetan Rivet To: dev@dpdk.org Cc: stable@dpdk.org Date: Wed, 13 May 2020 12:47:50 +0200 Message-Id: <20200513104751.46466-2-grive@u256.net> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200513104751.46466-1-grive@u256.net> References: <20200513104751.46466-1-grive@u256.net> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH v1 1/2] pci: fix allowing underflow when parsing PCI id X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" 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 Acked-by: Darek Stojaczyk Acked-by: Darek Stojaczyk --- 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; + errno = 0; val = strtoul(in, &end, 16); if (errno != 0 || end[0] != ':' || val > UINT16_MAX) From patchwork Wed May 13 10:47:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ga=C3=ABtan_Rivet?= X-Patchwork-Id: 70161 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id BB1C3A034F; Wed, 13 May 2020 12:48:14 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id D4E6A1D536; Wed, 13 May 2020 12:48:04 +0200 (CEST) Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by dpdk.org (Postfix) with ESMTP id 3475D1D40F for ; Wed, 13 May 2020 12:48:00 +0200 (CEST) X-Originating-IP: 86.246.31.132 Received: from inocybe.home (lfbn-idf2-1-566-132.w86-246.abo.wanadoo.fr [86.246.31.132]) (Authenticated sender: grive@u256.net) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id 1AE371C0003 for ; Wed, 13 May 2020 10:47:59 +0000 (UTC) From: Gaetan Rivet To: dev@dpdk.org Date: Wed, 13 May 2020 12:47:51 +0200 Message-Id: <20200513104751.46466-3-grive@u256.net> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200513104751.46466-1-grive@u256.net> References: <20200513104751.46466-1-grive@u256.net> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH v1 2/2] pci: explain how empty strings are rejected in DBDF X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Empty strings are forbidden as input to rte_pci_addr_parse(). It is explicitly enforced in BDF parsing as parsing the bus field will immediately fail. The related check is commented. It is implicitly enforced in DBDF parsing, as the domain would be parsed to 0 without error, but the check `end[0] != ':'` afterward will return -EINVAL. Enforcing consistency between parsers by reading the code is not helped by this property being implicit. Add a comment to explain. Signed-off-by: Gaetan Rivet Acked-by: Darek Stojaczyk Acked-by: Darek Stojaczyk --- lib/librte_pci/rte_pci.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/librte_pci/rte_pci.c b/lib/librte_pci/rte_pci.c index e4ecdc32f..60e6fbae7 100644 --- a/lib/librte_pci/rte_pci.c +++ b/lib/librte_pci/rte_pci.c @@ -84,6 +84,10 @@ pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr) errno = 0; val = strtoul(in, &end, 16); + /* Empty string is not an error for strtoul, but the check + * end[0] != ':' + * will detect the issue. + */ if (errno != 0 || end[0] != ':' || val > UINT16_MAX) return -EINVAL; dev_addr->domain = (uint16_t)val;