Skip to contentSkip to conversation

net/ceph/messenger: support IPv6 Link-Local with scope identifier

PATCH Superseded Max Kellermann 2 participants 3 messages Jul 8 – Aug 12 Ceph common code (libceph)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 net/ceph/messenger: support IPv6 Link-Local with scope identifier

Implement ceph_pton() using inet_pton_with_scope(). This minimal patch preserves the ceph_pton() API and thus requires copying the parameter to a null-terminated string.

---
v1->v2:
- check "ipend" before dereferencing it

v2->v3:
- fix indentation
- use the caller's network namespace instead of init_ns
- stop parsing at the port separator

---
 net/ceph/messenger.c | 45 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 36 insertions(+), 9 deletions(-)
net/ceph/messenger.c +36 -9
index 34b3097b4c7b..21dfff074e7d 100644
@@ -1221,19 +1221,46 @@ void ceph_addr_set_port(struct ceph_entity_addr *addr, int p)
 static int ceph_pton(const char *str, size_t len, struct ceph_entity_addr *addr,
 		char delim, const char **ipend)
 {
-	memset(&addr->in_addr, 0, sizeof(addr->in_addr));
+	const char *delim_p;
+	char *copy;
+	struct sockaddr_storage stor = { 0 };
+	int ret;
 
-	if (in4_pton(str, len, (u8 *)&((struct sockaddr_in *)&addr->in_addr)->sin_addr.s_addr, delim, ipend)) {
-		put_unaligned(AF_INET, &addr->in_addr.ss_family);
-		return 0;
-	}
+	delim_p = memchr(str, delim, len);
+	if (delim_p)
+		/* delimiter was found - stop parsing there */
+		len = delim_p - str;
 
-	if (in6_pton(str, len, (u8 *)&((struct sockaddr_in6 *)&addr->in_addr)->sin6_addr.s6_addr, delim, ipend)) {
-		put_unaligned(AF_INET6, &addr->in_addr.ss_family);
-		return 0;
+	delim_p = memchr(str, ':', len);
+	if (delim_p && !memchr(delim_p + 1, ':', str + len - delim_p - 1))
+		/* a single colon was found - stop parsing there
+		 * because the caller wants to parse the port
+		 * number
+		 */
+		len = delim_p - str;
+
+	/* the input string might not be null terminated, so copy it
+	 * to a null-terminated string
+	 */
+	copy = kstrndup(str, len, GFP_NOFS);
+	if (!copy)
+		return -ENOMEM;
+
+	ret = inet_pton_with_scope(current->nsproxy->net_ns, AF_UNSPEC, copy, NULL, &stor);
+	kfree(copy);
+
+	if (!ret) {
+		/* ceph_entity_addr might be misaligned, so we have to
+		 * parse to a stack variable first
+		 */
+		memcpy(&addr->in_addr, &stor, sizeof(stor));
+
+		/* return the end of the parsed portion to the caller on success */
+		if (ipend)
+			*ipend = str + len;
 	}
 
-	return -EINVAL;
+	return ret;
 }
 
 /*
Signature · 1 line
2.47.3
maintainer
#1 lore raw

Hi Max,

NACK for now. Thanks for the patch, but I have some concerns that need a v2:

1. [critical] messenger.c:1259: scope_id leaks into wire identity, breaking handshake inet_pton_with_scope() stores the local interface index in sin6_scope_id. The full sockaddr_storage — including sin6_scope_id — is copied into ceph_entity_addr.in_addr, which is then wire-encoded and memcmp'd during both v1 and v2 handshakes. The server will have scope_id=0 in its advertised address, so the comparison will always fail for link-local
addresses with a scope. The interface index is also
namespace-local, so it won't match across hosts even if both sides set one.
Suggested fix: Zero out sin6_scope_id in the canonical ceph_entity_addr after the memcpy. Keep the scope only in the sockaddr passed to kernel_connect(), or store it in a side field.
2. [minor] messenger.c:1334: Allocation failure masked by DNS fallback ceph_pton() can return -ENOMEM from kstrndup(), but ceph_parse_server_name() treats any non-zero return as "not an IP literal" and falls through to ceph_dns_resolve_name(). This silently turns OOM into a spurious DNS lookup.
Suggested fix: Fall back to DNS only on -EINVAL; propagate -ENOMEM directly.
3. [nit] messenger.c:1247: GFP_NOFS in non-filesystem context kstrndup(str, len, GFP_NOFS) is used during mount option parsing
— process context, no filesystem locks held. GFP_KERNEL would be more appropriate.

scope_id from inet_pton_with_scope() is copied into the canonical ceph_entity_addr, breaking v1 and v2 handshake identity comparisons for link-local addresses

Signature · 1 line
Alex Markuze
#2 lore raw
Quoted 2 lines · Alex Markuze

1. [critical] messenger.c:1259: scope_id leaks into wire identity, breaking handshake inet_pton_with_scope() stores the local interface index in

Uh-oh! The Ceph protocol indeed copies struct sockaddr as-is on the wire. Is that portable?

Any way to work around this mess is ugly. I decided to fix that by wrapping the memcmp() in a function that clears the scope id. That requires no extra storage, and it's still possible to see the scope id in log messages (though ceph_pr_addr() still ignores the scope id currently).

Quoted 11 lines

2. [minor] messenger.c:1334: Allocation failure masked by DNS fallback ceph_pton() can return -ENOMEM from kstrndup(), but ceph_parse_server_name() treats any non-zero return as "not an IP literal" and falls through to ceph_dns_resolve_name(). This silently turns OOM into a spurious DNS lookup. Suggested fix: Fall back to DNS only on -EINVAL; propagate -ENOMEM directly.
3. [nit] messenger.c:1247: GFP_NOFS in non-filesystem context kstrndup(str, len, GFP_NOFS) is used during mount option parsing — process context, no filesystem locks held. GFP_KERNEL would be more appropriate.

All three fixed in v4.

Signature · 8 lines
Max Kellermann
Principal Architect
Hosting Technology

cm4all | Im Mediapark 6a | 50670 Köln | Germany
General information about the company can be found here:
https://www.cm4all.com/impressum
A member of the IONOS Group