From a1a47b5592def0262c4cebf9c53bdec56c4db672 Mon Sep 17 00:00:00 2001 From: Seth Howell Date: Tue, 9 Jan 2018 14:39:00 -0700 Subject: [PATCH] nvmf/subsystem.c: add checks for valid utf-8 chars: The NVMe spec states that nqn names are to be encoded in utf-8. The prefixes of all nqn's are already required to be ascii by virtue of their structure so they are already valid utf-8, but the user specified strings should be checked for valid utf-8 strings. Change-Id: I20090d366e93e98af4932eaa120d4edb6e512206 Signed-off-by: Seth Howell Reviewed-on: https://review.gerrithub.io/394118 Tested-by: SPDK Automated Test System Reviewed-by: Ben Walker Reviewed-by: Jim Harris --- lib/nvmf/subsystem.c | 13 +++++++++++++ test/unit/lib/nvmf/subsystem.c/subsystem_ut.c | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/nvmf/subsystem.c b/lib/nvmf/subsystem.c index 9c62721882..5f0066ac59 100644 --- a/lib/nvmf/subsystem.c +++ b/lib/nvmf/subsystem.c @@ -44,6 +44,7 @@ #include "spdk_internal/bdev.h" #include "spdk_internal/log.h" +#include "spdk_internal/utf.h" #include @@ -67,6 +68,7 @@ spdk_nvmf_valid_nqn(const char *nqn) size_t len; uuid_t uuid_value; uint i; + int bytes_consumed; uint domain_label_length; char *reverse_domain_end; uint reverse_domain_end_index; @@ -198,6 +200,17 @@ spdk_nvmf_valid_nqn(const char *nqn) } } } + + i = reverse_domain_end_index + 1; + while (i < len) { + bytes_consumed = utf8_valid(&nqn[i], &nqn[len]); + if (bytes_consumed <= 0) { + SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only valid utf-8.\n", nqn); + return false; + } + + i += bytes_consumed; + } return true; } diff --git a/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c b/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c index 7d0b898c68..67c9fae291 100644 --- a/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c +++ b/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c @@ -323,6 +323,18 @@ nvmf_test_create_subsystem(void) CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn); spdk_nvmf_subsystem_destroy(subsystem); + /* Invalid name user string contains an invalid utf-8 character */ + strncpy(nqn, "nqn.2016-06.io.spdk:\xFFsubsystem1", sizeof(nqn)); + subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0); + SPDK_CU_ASSERT_FATAL(subsystem == NULL); + + /* Valid name with non-ascii but valid utf-8 characters */ + strncpy(nqn, "nqn.2016-06.io.spdk:\xe1\x8a\x88subsystem1\xca\x80", sizeof(nqn)); + subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0); + SPDK_CU_ASSERT_FATAL(subsystem != NULL); + CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn); + spdk_nvmf_subsystem_destroy(subsystem); + /* Invalid uuid (too long) */ strncpy(nqn, "nqn.2014-08.org.nvmexpress:uuid:11111111-aaaa-bbdd-FFEE-123456789abcdef", sizeof(nqn));