Skip to contentSkip to conversation

nvme-tcp: use correct network namespace to resolve addresses

PATCH New Hannes Reinecke 2 participants 2 messages Nov 26 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nvme-tcp: use correct network namespace to resolve addresses

Resolving network addresses should use the current network namespace; the passed-in addresses might not exist in the default network namespace. And while at use dev_get_by_name_rcu() with proper rcu locks to avoid a possible crash on network namespace changes.

---
 drivers/nvme/host/tcp.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/nvme/host/tcp.c +11 -6
index c3149a9151f5..34a8bc2c5af0 100644
@@ -3308,6 +3308,7 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev,
 	ctrl->ctrl.queue_count = NVME_TCP_OPT_NR_QUEUES(opts);
 	ctrl->ctrl.sqsize = opts->queue_size - 1;
 	ctrl->ctrl.kato = opts->kato;
+	ctrl->net = get_net(current->nsproxy->net_ns);
 
 	INIT_DELAYED_WORK(&ctrl->connect_work,
 			nvme_tcp_reconnect_ctrl_work);
@@ -3324,7 +3325,7 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev,
 		opts->mask |= NVMF_OPT_TRSVCID;
 	}
 
-	ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
+	ret = inet_pton_with_scope(ctrl->net, AF_UNSPEC,
 			opts->traddr, opts->trsvcid, &ctrl->addr);
 	if (ret) {
 		pr_err("malformed address passed: %s:%s\n",
@@ -3333,7 +3334,7 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev,
 	}
 
 	if (opts->mask & NVMF_OPT_HOST_TRADDR) {
-		ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
+		ret = inet_pton_with_scope(ctrl->net, AF_UNSPEC,
 			opts->host_traddr, NULL, &ctrl->src_addr);
 		if (ret) {
 			pr_err("malformed src address passed: %s\n",
@@ -3343,8 +3344,13 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev,
 	}
 
 	if (opts->mask & NVMF_OPT_HOST_IFACE) {
-		if (!__dev_get_by_name(current->nsproxy->net_ns,
-				       opts->host_iface)) {
+		struct net_device *found;
+
+		rcu_read_lock();
+		found = dev_get_by_name_rcu(ctrl->net,
+					    opts->host_iface);
+		rcu_read_unlock();
+		if (!found) {
 			pr_err("invalid interface passed: %s\n",
 			       opts->host_iface);
 			ret = -ENODEV;
@@ -3367,16 +3373,15 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev,
 	for (i = 0; i < ctrl->nr_allocated_queues; i++)
 		nvme_tcp_init_queue(ctrl, i);
 
-	ctrl->net = get_net(current->nsproxy->net_ns);
 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
 	if (ret)
 		goto out_kfree_queues;
 
 	return ctrl;
 out_kfree_queues:
-	put_net(ctrl->net);
 	kfree(ctrl->queues);
 out_free_ctrl:
+	put_net(ctrl->net);
 	kfree(ctrl);
 	return ERR_PTR(ret);
 }
Signature · 1 line
2.51.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] nvme-tcp: missing Fixes trailer for nvme_tcp_alloc_ctrl() netns and RCU fixes --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] nvme-tcp: missing Fixes trailer for nvme_tcp_alloc_ctrl() netns and RCU fixes
--

commit a9e44348b8fe94c523f1cf508bd4bb1ce6079725
Author: Hannes Reinecke <[email protected]>

Quoted 8 lines

nvme-tcp: use correct network namespace to resolve addresses

Resolving network addresses should use the current network namespace; the passed-in addresses might not exist in the default network namespace. And while at use dev_get_by_name_rcu() with proper rcu locks to avoid a possible crash on network namespace changes.

[Severity: Low]
Resolving opts->traddr and opts->host_traddr against init_net dates back to 3f2304f8c6d6 ("nvme-tcp: add NVMe over TCP host driver"), and the unlocked __dev_get_by_name() call was introduced in 8b43ced64d2b ("nvme-tcp: use __dev_get_by_name instead dev_get_by_name for OPT_HOST_IFACE"). Should this include:

Fixes 3f2304f8c6d6 ("nvme-tcp: add NVMe over TCP host driver")
Fixes 8b43ced64d2b ("nvme-tcp: use __dev_get_by_name instead dev_get_by_name for OPT_HOST_IFACE")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1