From patchwork Fri Mar 27 08:09:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olivier Matz X-Patchwork-Id: 67259 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 33316A057C; Fri, 27 Mar 2020 09:10:49 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id EB3A31C030; Fri, 27 Mar 2020 09:10:47 +0100 (CET) Received: from proxy.6wind.com (host.76.145.23.62.rev.coltfrance.com [62.23.145.76]) by dpdk.org (Postfix) with ESMTP id 3DBB71C028; Fri, 27 Mar 2020 09:10:46 +0100 (CET) Received: from glumotte.dev.6wind.com. (unknown [10.16.0.195]) by proxy.6wind.com (Postfix) with ESMTP id 0B76E3B1DF3; Fri, 27 Mar 2020 09:10:46 +0100 (CET) From: Olivier Matz To: wangyunjian@huawei.com Cc: dev@dpdk.org, jerry.lilijun@huawei.com, olivier.matz@6wind.com, stable@dpdk.org, xudingke@huawei.com Date: Fri, 27 Mar 2020 09:09:53 +0100 Message-Id: <20200327080955.19571-2-olivier.matz@6wind.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200327080955.19571-1-olivier.matz@6wind.com> References: <1584592680-14000-1-git-send-email-wangyunjian@huawei.com> <20200327080955.19571-1-olivier.matz@6wind.com> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH v2 1/3] tests/kvargs: fix to consider empty elements as valid 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 elements passed to the kvargs parser are silently ignored. Examples of valid strings: "" "," ",,,,,,key=val,,,," Fix the unit tests to conform to this behavior. Note: the test_invalid_kvargs() function is currently broken, which explain why the tests were not failing. It is fixed in the next commit. Fixes: e495f5435524 ("kvargs: add test case in app/test") Cc: stable@dpdk.org Signed-off-by: Olivier Matz --- app/test/test_kvargs.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/app/test/test_kvargs.c b/app/test/test_kvargs.c index a42056f36..d3db88a57 100644 --- a/app/test/test_kvargs.c +++ b/app/test/test_kvargs.c @@ -142,7 +142,7 @@ static int test_valid_kvargs(void) valid_keys = valid_keys_list; kvlist = rte_kvargs_parse(args, valid_keys); if (kvlist == NULL) { - printf("rte_kvargs_parse() error"); + printf("rte_kvargs_parse() error\n"); goto fail; } if (strcmp(kvlist->pairs[0].value, "[0,1]") != 0) { @@ -157,6 +157,40 @@ static int test_valid_kvargs(void) } rte_kvargs_free(kvlist); + /* test using empty string (it is valid) */ + args = ""; + kvlist = rte_kvargs_parse(args, NULL); + if (kvlist == NULL) { + printf("rte_kvargs_parse() error\n"); + goto fail; + } + if (rte_kvargs_count(kvlist, NULL) != 0) { + printf("invalid count value\n"); + goto fail; + } + rte_kvargs_free(kvlist); + + /* test using empty elements (it is valid) */ + args = "foo=1,,check=value2,,"; + kvlist = rte_kvargs_parse(args, NULL); + if (kvlist == NULL) { + printf("rte_kvargs_parse() error\n"); + goto fail; + } + if (rte_kvargs_count(kvlist, NULL) != 2) { + printf("invalid count value\n"); + goto fail; + } + if (rte_kvargs_count(kvlist, "foo") != 1) { + printf("invalid count value for 'foo'\n"); + goto fail; + } + if (rte_kvargs_count(kvlist, "check") != 1) { + printf("invalid count value for 'check'\n"); + goto fail; + } + rte_kvargs_free(kvlist); + return 0; fail: @@ -179,7 +213,6 @@ static int test_invalid_kvargs(void) const char *args_list[] = { "wrong-key=x", /* key not in valid_keys_list */ "foo=1,foo=", /* empty value */ - "foo=1,,foo=2", /* empty key/value */ "foo=1,foo", /* no value */ "foo=1,=2", /* no key */ "foo=[1,2", /* no closing bracket in value */ From patchwork Fri Mar 27 08:09:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olivier Matz X-Patchwork-Id: 67261 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 BB9A5A057C; Fri, 27 Mar 2020 09:11:08 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 197E71C0B1; Fri, 27 Mar 2020 09:10:51 +0100 (CET) Received: from proxy.6wind.com (host.76.145.23.62.rev.coltfrance.com [62.23.145.76]) by dpdk.org (Postfix) with ESMTP id 4042B1C029; Fri, 27 Mar 2020 09:10:46 +0100 (CET) Received: from glumotte.dev.6wind.com. (unknown [10.16.0.195]) by proxy.6wind.com (Postfix) with ESMTP id 19E7E3B1DF4; Fri, 27 Mar 2020 09:10:46 +0100 (CET) From: Olivier Matz To: wangyunjian@huawei.com Cc: dev@dpdk.org, jerry.lilijun@huawei.com, olivier.matz@6wind.com, stable@dpdk.org, xudingke@huawei.com Date: Fri, 27 Mar 2020 09:09:54 +0100 Message-Id: <20200327080955.19571-3-olivier.matz@6wind.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200327080955.19571-1-olivier.matz@6wind.com> References: <1584592680-14000-1-git-send-email-wangyunjian@huawei.com> <20200327080955.19571-1-olivier.matz@6wind.com> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH v2 2/3] tests/kvargs: fix check of invalid cases 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 return was not properly placed, and only the first test case was validated. Fixes: e495f5435524 ("kvargs: add test case in app/test") Cc: stable@dpdk.org Signed-off-by: Olivier Matz --- app/test/test_kvargs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/test/test_kvargs.c b/app/test/test_kvargs.c index d3db88a57..f823b771f 100644 --- a/app/test/test_kvargs.c +++ b/app/test/test_kvargs.c @@ -230,8 +230,8 @@ static int test_invalid_kvargs(void) rte_kvargs_free(kvlist); goto fail; } - return 0; } + return 0; fail: printf("while processing <%s>", *args); From patchwork Fri Mar 27 08:09:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olivier Matz X-Patchwork-Id: 67262 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 31C59A057C; Fri, 27 Mar 2020 09:11:18 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id A3C7C1C0BC; Fri, 27 Mar 2020 09:10:52 +0100 (CET) Received: from proxy.6wind.com (host.76.145.23.62.rev.coltfrance.com [62.23.145.76]) by dpdk.org (Postfix) with ESMTP id 4B0B31C02E; Fri, 27 Mar 2020 09:10:46 +0100 (CET) Received: from glumotte.dev.6wind.com. (unknown [10.16.0.195]) by proxy.6wind.com (Postfix) with ESMTP id 27CCB3B1DF5; Fri, 27 Mar 2020 09:10:46 +0100 (CET) From: Olivier Matz To: wangyunjian@huawei.com Cc: dev@dpdk.org, jerry.lilijun@huawei.com, olivier.matz@6wind.com, stable@dpdk.org, xudingke@huawei.com Date: Fri, 27 Mar 2020 09:09:55 +0100 Message-Id: <20200327080955.19571-4-olivier.matz@6wind.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200327080955.19571-1-olivier.matz@6wind.com> References: <1584592680-14000-1-git-send-email-wangyunjian@huawei.com> <20200327080955.19571-1-olivier.matz@6wind.com> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH v2 3/3] kvargs: fix a heap buffer overflow when parsing list 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" From: Yunjian Wang When the input string is "key=[", the ending '\0' is replaced by a ',', leading to a heap buffer overflow. Check the content of ctx1 to avoid this problem. Fixes: cc0579f2339a ("kvargs: support list value") Cc: stable@dpdk.org Signed-off-by: Yunjian Wang Signed-off-by: Olivier Matz --- app/test/test_kvargs.c | 1 + lib/librte_kvargs/rte_kvargs.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/app/test/test_kvargs.c b/app/test/test_kvargs.c index f823b771f..2a2dae43a 100644 --- a/app/test/test_kvargs.c +++ b/app/test/test_kvargs.c @@ -217,6 +217,7 @@ static int test_invalid_kvargs(void) "foo=1,=2", /* no key */ "foo=[1,2", /* no closing bracket in value */ ",=", /* also test with a smiley */ + "foo=[", /* no value in list and no closing bracket */ NULL }; const char **args; const char *valid_keys_list[] = { "foo", "check", NULL }; diff --git a/lib/librte_kvargs/rte_kvargs.c b/lib/librte_kvargs/rte_kvargs.c index d39332999..1d815dcd9 100644 --- a/lib/librte_kvargs/rte_kvargs.c +++ b/lib/librte_kvargs/rte_kvargs.c @@ -50,6 +50,8 @@ rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params) /* Find the end of the list. */ while (str[strlen(str) - 1] != ']') { /* Restore the comma erased by strtok_r(). */ + if (ctx1[0] == '\0') + return -1; /* no closing bracket */ str[strlen(str)] = ','; /* Parse until next comma. */ str = strtok_r(NULL, RTE_KVARGS_PAIRS_DELIM, &ctx1);