From f49b586666d37ba80c75f5691901d9ab9b36dbe7 Mon Sep 17 00:00:00 2001 From: Martin Belanger Date: Mon, 30 Nov 2026 12:13:51 -0500 Subject: [PATCH] libnvme: reject a hostname in a TID without a transport An exclusion entry without "transport=" kept a hostname in traddr or host-traddr. Such an entry never matches, since connect TIDs hold numeric addresses, but "nvme exclusion list" showed it as valid and "nvme exclusion add" accepted it. Without a transport, accept only a numeric IP or an FC WWN pair. Signed-off-by: Martin Belanger --- Documentation/nvme-exclusion-list.txt | 4 +- libnvme/src/nvme/tid.c | 87 ++++++++++++++++++--------- libnvme/src/nvme/tid.h | 5 +- libnvme/tests/exclusion.c | 1 + libnvme/tests/test-tid.c | 29 +++++++++ 5 files changed, 93 insertions(+), 33 deletions(-) diff --git a/Documentation/nvme-exclusion-list.txt b/Documentation/nvme-exclusion-list.txt index d63cdde5c6..3a3e8a9107 100644 --- a/Documentation/nvme-exclusion-list.txt +++ b/Documentation/nvme-exclusion-list.txt @@ -19,8 +19,8 @@ the entries of that one list. An entry that can never match is marked '# invalid: never matches'. An entry is invalid when it uses an unknown key, a token without '=', an -empty value, or a hostname where the transport requires a numeric -address. Add --verbose to log the reason for each one. +empty value, or a hostname instead of a numeric address. Add --verbose +to log the reason for each one. This command is read-only and does not require root. diff --git a/libnvme/src/nvme/tid.c b/libnvme/src/nvme/tid.c index 343cbf894a..8cb66a9448 100644 --- a/libnvme/src/nvme/tid.c +++ b/libnvme/src/nvme/tid.c @@ -120,6 +120,50 @@ static bool fc_wwn_is_valid(const char *in) return (size_t)len == strlen(in); } +static int sanitize_ip_addr(char **addr) +{ + char *canon; + int rc; + + if (!*addr) + return 0; + + rc = canon_ip(*addr, &canon); + if (rc) + return rc; + + free(*addr); + *addr = canon; + + return 0; +} + +static int sanitize_fc_addr(char *addr) +{ + if (!addr) + return 0; + + fc_wwn_normalize(addr); + + return fc_wwn_is_valid(addr) ? 0 : -EINVAL; +} + +/* + * An address of a partial TID with no transport, e.g. an exclusion entry: + * accept a numeric IP or an FC WWN pair. A hostname is rejected, since it + * never matches a connect TID. + */ +static int sanitize_any_addr(char **addr) +{ + int rc; + + rc = sanitize_ip_addr(addr); + if (rc != -EINVAL) + return rc; + + return sanitize_fc_addr(*addr); +} + /* * Sanitize the addressing fields once the transport is known. IP * transports: canonicalize a numeric traddr/host_traddr via canon_ip(); a @@ -133,45 +177,30 @@ static bool fc_wwn_is_valid(const char *in) */ static int tid_sanitize_addr(struct libnvmf_tid *t) { - char *canon; int rc; - if (!t->transport) - return 0; - - if (streq(t->transport, "fc")) { - if (t->traddr) - fc_wwn_normalize(t->traddr); - if (t->host_traddr) - fc_wwn_normalize(t->host_traddr); - - if (t->traddr && !fc_wwn_is_valid(t->traddr)) - return -EINVAL; - if (t->host_traddr && !fc_wwn_is_valid(t->host_traddr)) - return -EINVAL; - return 0; - } - - if (!streq(t->transport, "tcp") && !streq(t->transport, "rdma")) - return 0; - - if (t->traddr) { - rc = canon_ip(t->traddr, &canon); + if (!t->transport) { + rc = sanitize_any_addr(&t->traddr); if (rc) return rc; - free(t->traddr); - t->traddr = canon; + return sanitize_any_addr(&t->host_traddr); } - if (t->host_traddr) { - rc = canon_ip(t->host_traddr, &canon); + if (streq(t->transport, "fc")) { + rc = sanitize_fc_addr(t->traddr); if (rc) return rc; - free(t->host_traddr); - t->host_traddr = canon; + return sanitize_fc_addr(t->host_traddr); } - return 0; + if (!streq(t->transport, "tcp") && !streq(t->transport, "rdma")) + return 0; + + rc = sanitize_ip_addr(&t->traddr); + if (rc) + return rc; + + return sanitize_ip_addr(&t->host_traddr); } __shr_public int libnvmf_tid_from_fields( diff --git a/libnvme/src/nvme/tid.h b/libnvme/src/nvme/tid.h index bcc15eb060..a6911e44d7 100644 --- a/libnvme/src/nvme/tid.h +++ b/libnvme/src/nvme/tid.h @@ -27,10 +27,11 @@ struct libnvme_global_ctx; * Convenience constructor. NULL fields are stored as NULL. For an IP * transport (tcp, rdma) a numeric traddr/host_traddr is canonicalized; a * hostname is rejected -- resolving it is the caller's job, not libnvme's. + * Without a transport, an address must be a numeric IP or an FC WWN pair. * * Return: 0 on success (*@out is the allocated TID); -EINVAL if @out is - * NULL or traddr/host_traddr is not numeric on an IP transport; -ENOMEM on - * allocation failure. + * NULL or traddr/host_traddr is not numeric on an IP transport or without + * a transport; -ENOMEM on allocation failure. */ int libnvmf_tid_from_fields(const char *transport, const char *traddr, const char *trsvcid, const char *subsysnqn, diff --git a/libnvme/tests/exclusion.c b/libnvme/tests/exclusion.c index c97b9dfa94..b8346772fd 100644 --- a/libnvme/tests/exclusion.c +++ b/libnvme/tests/exclusion.c @@ -387,6 +387,7 @@ static bool test_malformed_entry(struct libnvme_global_ctx *ctx) "nqn=", "subsysnqn=nqn.example", "transport=tcp;traddr=dc.example.com", + "traddr=dc.example.com", }; struct libnvmf_tid *tid; char path[512]; diff --git a/libnvme/tests/test-tid.c b/libnvme/tests/test-tid.c index fc7998a996..622d8e35e7 100644 --- a/libnvme/tests/test-tid.c +++ b/libnvme/tests/test-tid.c @@ -688,6 +688,35 @@ static bool test_tid_sanitize(void) pass &= p; libnvmf_tid_free(t); + /* + * Without a transport (a partial TID, e.g. an exclusion entry), an + * address must still be a numeric IP or an FC WWN pair: a hostname + * would never match a connect TID. + */ + p = libnvmf_tid_parse(ctx, "traddr=array.example.com", &t) == -EINVAL && + t == NULL; + CHECK(p, "no transport: hostname traddr rejected"); + pass &= p; + libnvmf_tid_free(t); + + p = libnvmf_tid_parse(ctx, "host-traddr=myhost", &t) == -EINVAL && + t == NULL; + CHECK(p, "no transport: hostname host_traddr rejected"); + pass &= p; + libnvmf_tid_free(t); + + p = libnvmf_tid_parse(ctx, "traddr=::ffff:10.0.0.5", &t) == 0 && + streq(libnvmf_tid_get_traddr(t), "10.0.0.5"); + CHECK(p, "no transport: numeric traddr canonicalized"); + pass &= p; + libnvmf_tid_free(t); + + p = libnvmf_tid_parse(ctx, "traddr=nn-0x1,pn-0x2", &t) == 0 && + streq(libnvmf_tid_get_traddr(t), "nn-0x1:pn-0x2"); + CHECK(p, "no transport: fc traddr normalized"); + pass &= p; + libnvmf_tid_free(t); + /* loop has no addressing shape at all: truly untouched. */ libnvmf_tid_from_fields("loop", "anything goes", NULL, "nqn.t", NULL, NULL, NULL, NULL, &t);