From f92411c4dad6176a51d6c262a03532953cc68d0a Mon Sep 17 00:00:00 2001 From: Tomasz Zawadzki Date: Thu, 9 Mar 2023 11:15:16 +0100 Subject: [PATCH] lib/nvme_tcp: check destination port before parsing address nvme_tcp_parse_addr() uses getaddrinfo() to parse the address. Depending on the system behavior of this function differs. On FreeBSD the port is verified not to be exceeding 65535 for IPv4, meanwhile Linux does not check it at this point. test_nvme_tcp_qpair_connect_sock() UT was attempting to test the code path that is moved in this patch, but on FreeBSD was encountering failure during getaddrinfo() with different error code. This patch moves the destination port check before parsing addresses to take the same path regardless of the system used. Fixes #2936 Change-Id: I271e8c32e07a15dcf0e0ee7e90dd174c96b18858 Signed-off-by: Tomasz Zawadzki Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/17095 Reviewed-by: Aleksey Marchuk Reviewed-by: Jim Harris Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins --- lib/nvme/nvme_tcp.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/nvme/nvme_tcp.c b/lib/nvme/nvme_tcp.c index 8e36ccc2d..bc3800198 100644 --- a/lib/nvme/nvme_tcp.c +++ b/lib/nvme/nvme_tcp.c @@ -1890,6 +1890,13 @@ nvme_tcp_qpair_connect_sock(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpai memset(&dst_addr, 0, sizeof(dst_addr)); + port = spdk_strtol(ctrlr->trid.trsvcid, 10); + if (port <= 0 || port >= INT_MAX) { + SPDK_ERRLOG("Invalid port: %s\n", ctrlr->trid.trsvcid); + rc = -1; + return rc; + } + SPDK_DEBUGLOG(nvme, "trsvcid is %s\n", ctrlr->trid.trsvcid); rc = nvme_tcp_parse_addr(&dst_addr, family, ctrlr->trid.traddr, ctrlr->trid.trsvcid); if (rc != 0) { @@ -1906,13 +1913,6 @@ nvme_tcp_qpair_connect_sock(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpai } } - port = spdk_strtol(ctrlr->trid.trsvcid, 10); - if (port <= 0 || port >= INT_MAX) { - SPDK_ERRLOG("Invalid port: %s\n", ctrlr->trid.trsvcid); - rc = -1; - return rc; - } - sock_impl_name = ctrlr->opts.psk[0] ? "ssl" : NULL; SPDK_DEBUGLOG(nvme, "sock_impl_name is %s\n", sock_impl_name);