Skip to contentSkip to conversation

Drivers: hv: vmbus: Fix the error paths of channel setup

PATCH New Yuqiang Sun 2 participants 4 messages Nov 3

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover Drivers: hv: vmbus: Fix the error paths of channel setup

When setting up a channel fails, vmbus_add_channel_work() and vmbus_device_register() do not agree on who cleans up:

- vmbus_add_channel_kobj() drops the channel's last reference when it fails, and its callers then use the channel and drop it again (patch 1; this is what our static analysis reported, for the sub-channel path). Paths that fail before it put a kobject that was never initialized.

- When vmbus_device_register() fails, the device's release calls hv_process_channel_removal() on a channel that was not rescinded, which hits BUG_ON(!channel->rescind); vmbus_add_channel_work() would then clean the channel up a second time (patch 2). Patch 2 leaves the channel to vmbus_add_channel_work() when device_register() fails, and keeps the device when only the channel's sysfs entries fail, since a driver may be bound by then.

Changes in v3 (patch 2 only, after the Sashiko review of v2):
- vmbus_match() reads the channel under rcu_read_lock(), as it is freed with kfree_rcu().
- The channel no longer points to the device after device_register() fails and the device is freed.
- A channel of a device without a channels directory is left out of sysfs instead of being refused. v2 refused a sub-channel offered while the primary channel's device was still being registered, which could leave a driver waiting for it.
v2: https://lore.kernel.org/linux-hyperv/[email protected]/

Changes in v2 (patch 2 only, after the Sashiko review of v1):
- vmbus_match() skips a device whose channel pointer was cleared, which a concurrent driver_attach() can still see.
- Failures after device_register() (the channels directory, the primary channel's kobject and its sysfs group) no longer unregister the
device: a bound driver such as hv_sock, or one that opened sub-channels, can keep the channel past its remove callback unless the channel was rescinded.
v1: https://lore.kernel.org/linux-hyperv/[email protected]/

Testing: QEMU (x86_64, KVM, kvm=off with the hv-* enlightenments, vmbus-bridge and hv-balloon) with KASAN, hv_vmbus and hv_balloon as modules loaded from init after failslab is armed, on mainline d135ab453a74. failslab is limited to allocations under vmbus_device_register(), and fails the N-th one when the hv-balloon offer is processed. Without the series, every failure hits the BUG_ON. With it, for N = 1 to 95:
- 53 failures in device_register() leave the offer unbound with "unable to add child device object";
- 31 failures in the channels directory, the channel's kobject or its sysfs group leave the device registered and bound to hv_balloon, with the channel missing from sysfs; unbinding the driver and binding it again works;
- the other runs fail an allocation whose failure is handled, or none. No run reports anything (no BUG, KASAN, WARNING or refcount_t message). v2 and v3 give the same results.
The sub-channel paths are not covered: QEMU's VMBus offers no sub-channels, cannot rescind a channel, and hv_balloon cannot be unloaded.

The failures need an allocation failure while the guest processes a channel offer from the host.

Yuqiang Sun (2):
Drivers: hv: vmbus: Keep the channel when vmbus_add_channel_kobj()
fails
Drivers: hv: vmbus: Do not remove the channel when device registration
fails

 drivers/hv/channel_mgmt.c | 12 +++--
 drivers/hv/hyperv_vmbus.h |  1 +
 drivers/hv/vmbus_drv.c    | 97 +++++++++++++++++++++++++--------------
 3 files changed, 71 insertions(+), 39 deletions(-)

base-commit: d135ab453a7400a34930bdda1ef4bdc8acfac80e

Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 1/2 Drivers: hv: vmbus: Keep the channel when vmbus_add_channel_kobj() fails

The channel is freed by the release of its kobject, and free_channel() drops that reference. Since commit 8bc69f86328e ("Drivers: hv: vmbus:
Fix memory leak in vmbus_add_channel_kobj"), vmbus_add_channel_kobj() also drops it when it fails, as kobject_init_and_add() asks for. That frees the channel, and both callers then go on with it:
vmbus_add_channel_work() unlinks a sub-channel, unmaps and releases its relid and calls free_channel(), a use after free and a second kobject_put(); for a primary channel, vmbus_device_register() unwinds through vmbus_device_release(), which removes the channel too.

The kobject is also only initialized in vmbus_add_channel_kobj(), so the error paths before it, for instance a vmbus_device_create() failure in vmbus_add_channel_work(), call free_channel() on a kobject that was never initialized: kobject_put() warns and does nothing, so the channel is leaked, and removing the attribute group from a kobject that is not in sysfs warns as well.

Initialize the kobject when the channel is allocated, so that free_channel() is the one place that drops its reference on every path. vmbus_add_channel_kobj() then only adds it, and leaves the reference to its callers when it fails. Remove the attribute group only if the kobject was added.

Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")
Fixes 8bc69f86328e ("Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj")

---

Notes:
The bug was found with kbound, a research static analysis prototype. The patch, the changelog and the test setup were written with the help of an AI coding assistant (Claude); I have reviewed them and tested them as described in the cover letter.

 drivers/hv/channel_mgmt.c |  5 +++--
 drivers/hv/hyperv_vmbus.h |  1 +
 drivers/hv/vmbus_drv.c    | 25 ++++++++++++++++++-------
 3 files changed, 22 insertions(+), 9 deletions(-)
drivers/hv/channel_mgmt.c +3 -2
index c42def04ac41..cb1d8d76928f 100644
@@ -367,6 +367,7 @@ static struct vmbus_channel *alloc_channel(void)
 		     vmbus_on_event, (unsigned long)channel);
 
 	hv_ringbuffer_pre_init(channel);
+	vmbus_init_channel_kobj(channel);
 
 	return channel;
 }
@@ -663,8 +664,8 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)
 			mutex_unlock(&vmbus_connection.channel_mutex);
 			cpus_read_unlock();
 			/*
-			 * Don't call free_channel(), because newchannel->kobj
-			 * is not initialized yet.
+			 * Don't call free_channel(): newchannel was never
+			 * added, and its kobject holds nothing yet.
 			 */
 			kfree(newchannel);
 			WARN_ON_ONCE(1);
drivers/hv/hyperv_vmbus.h +1 -0
index 33923621a5a3..7865a519dd7f 100644
@@ -408,6 +408,7 @@ struct hv_device *vmbus_device_create(const guid_t *type,
 
 int vmbus_device_register(struct hv_device *child_device_obj);
 void vmbus_device_unregister(struct hv_device *device_obj);
+void vmbus_init_channel_kobj(struct vmbus_channel *channel);
 int vmbus_add_channel_kobj(struct hv_device *device_obj,
 			   struct vmbus_channel *channel);
 
drivers/hv/vmbus_drv.c +18 -7
index 4692687cdbf3..fc0f16f51266 100644
@@ -2084,6 +2084,15 @@ int hv_remove_ring_sysfs(struct vmbus_channel *channel)
 }
 EXPORT_SYMBOL_GPL(hv_remove_ring_sysfs);
 
+/*
+ * vmbus_init_channel_kobj - initialize the kobject that holds the channel's
+ * lifetime; free_channel() drops its reference
+ */
+void vmbus_init_channel_kobj(struct vmbus_channel *channel)
+{
+	kobject_init(&channel->kobj, &vmbus_chan_ktype);
+}
+
 /*
  * vmbus_add_channel_kobj - setup a sub-directory under device/channels
  */
@@ -2094,13 +2103,14 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
 	u32 relid = channel->offermsg.child_relid;
 	int ret;
 
+	/*
+	 * On failure the kobject keeps its reference: the callers' error
+	 * paths drop it in free_channel(), which also frees the channel.
+	 */
 	kobj->kset = dev->channels_kset;
-	ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
-				   "%u", relid);
-	if (ret) {
-		kobject_put(kobj);
+	ret = kobject_add(kobj, NULL, "%u", relid);
+	if (ret)
 		return ret;
-	}
 
 	ret = sysfs_create_group(kobj, &vmbus_chan_group);
 
@@ -2109,7 +2119,6 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
 		 * The calling functions' error handling paths will cleanup the
 		 * empty channel directory.
 		 */
-		kobject_put(kobj);
 		dev_err(device, "Unable to set up channel sysfs files\n");
 		return ret;
 	}
@@ -2124,7 +2133,9 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
  */
 void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
 {
-	sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
+	/* A channel that failed before vmbus_add_channel_kobj() has none */
+	if (channel->kobj.state_in_sysfs)
+		sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
 }
 
 /*
Signature · 1 line
2.53.0
#2 lore raw

PATCH v3 2/2 Drivers: hv: vmbus: Do not remove the channel when device registration fails

In reply to Yuqiang Sun

When vmbus_device_register() fails, it drops the device it was setting up, and the device's release, vmbus_device_release(), calls hv_process_channel_removal() on the channel. That function is for channels the host has rescinded and starts with BUG_ON(!channel->rescind), so the kernel stops there. Without the BUG_ON, it would unmap and release the relid, unlink the channel and free it, and then the error path of vmbus_add_channel_work() would do all of that again.

When device_register() fails, clear the device's channel pointer before dropping the device, and only remove the channel in vmbus_device_release() if there is one: vmbus_add_channel_work() cleans it up, after clearing the channel's pointer to the freed device. A concurrent driver_attach() can still match the device while it is being removed, so make vmbus_match() skip a device without a channel, and look at the channel under rcu_read_lock(), as it is freed with kfree_rcu().

The later failures, creating the channels directory or the primary channel's kobject, come after device_register() may have bound a driver, and a driver can hold on to the channel past its remove callback unless the channel was rescinded (hv_sock's connection, or the sub-channels it opened). So do not unregister the device for them: its channel is then just missing from sysfs, as it is while the device is being set up. A sub-channel of a device without a channels directory is left out of sysfs too. This also applies to a sub-channel offered while its device is still being registered, before the directory is created, which was added to the top of /sys until now.

Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")

---

Notes:
The bug was found with kbound, a research static analysis prototype. The patch, the changelog and the test setup were written with the help of an AI coding assistant (Claude); I have reviewed them and tested them as described in the cover letter.

Changes in v3: vmbus_match() reads the channel under rcu_read_lock(), the channel no longer points to the freed device, and a sub-channel of a device without a channels directory is left out of sysfs instead of being refused (Sashiko review of v2).

Changes in v2: vmbus_match() skips a device without a channel, and the failures after device_register() keep the device instead of unregistering it (Sashiko review of v1).

 drivers/hv/channel_mgmt.c |  7 ++--
 drivers/hv/vmbus_drv.c    | 72 ++++++++++++++++++++++++---------------
 2 files changed, 49 insertions(+), 30 deletions(-)
drivers/hv/channel_mgmt.c +4 -3
index cb1d8d76928f..1fa232e86fce 100644
@@ -554,15 +554,16 @@ static void vmbus_add_channel_work(struct work_struct *work)
 	 * method.
 	 *
 	 * If vmbus_device_register() fails, the 'device_obj' is freed in
-	 * vmbus_device_release() as called by device_unregister() in the
-	 * error path of vmbus_device_register(). In the outside error
-	 * path, there's no need to free it.
+	 * vmbus_device_release() as called by put_device() in the error
+	 * path of vmbus_device_register(). In the outside error path,
+	 * there's no need to free it, but the channel must not point to it.
 	 */
 	ret = vmbus_device_register(newchannel->device_obj);
 
 	if (ret != 0) {
 		pr_err("unable to add child device object (relid %d)\n",
 			newchannel->offermsg.child_relid);
+		newchannel->device_obj = NULL;
 		goto err_deq_chan;
 	}
 
drivers/hv/vmbus_drv.c +45 -27

123 lines not on this page · raw message

Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH v3 2/2] Drivers: hv: vmbus: Do not remove the channel when device registration fails

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] hv: vmbus: channel UAF and double removal on vmbus_add_channel_work() error - [High] hv: vmbus…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] hv: vmbus: channel UAF and double removal on vmbus_add_channel_work() error
- [High] hv: vmbus: leaked CPU mask and suspend counter in vmbus_add_channel_work() --

commit 99d3a552529c5941e934071a1f2968acea94e54e
Author: Yuqiang Sun <[email protected]>

Drivers: hv: vmbus: Do not remove the channel when device registration fails

This commit avoids calling hv_process_channel_removal() from vmbus_device_release() when device_register() fails by clearing the device's channel pointer and letting vmbus_add_channel_work() clean up the channel. It also keeps the device registered if sysfs channel setup fails after device_register() succeeds.

Quoted 13 lines
drivers/hv/channel_mgmt.c +2 -3
index cb1d8d76928ff..1fa232e86fce2 100644
@@ -554,15 +554,16 @@ static void vmbus_add_channel_work(struct work_struct *work)
 	 * method.
 	 *
 	 * If vmbus_device_register() fails, the 'device_obj' is freed in
-	 * vmbus_device_release() as called by device_unregister() in the
-	 * error path of vmbus_device_register(). In the outside error
-	 * path, there's no need to free it.
+	 * vmbus_device_release() as called by put_device() in the error
+	 * path of vmbus_device_register(). In the outside error path,

drivers/hv/channel_mgmt.c static void vmbus_add_channel_work(struct work_struct *work) · line 568

Quoted 10 lines
drivers/hv/channel_mgmt.c +2 -0
+	 * there's no need to free it, but the channel must not point to it.
 	 */
 	ret = vmbus_device_register(newchannel->device_obj);
 
 	if (ret != 0) {
 		pr_err("unable to add child device object (relid %d)\n",
 			newchannel->offermsg.child_relid);
+		newchannel->device_obj = NULL;
 		goto err_deq_chan;
 	}

[Severity: High]
As reported by Sashiko on v2 [1], can jumping to err_deq_chan in vmbus_add_channel_work() race with a concurrent vmbus_onoffer_rescind() and cause a use-after-free on channel or a double channel removal?

In vmbus_process_offer(), newchannel is linked onto chn_list or sc_list, mapped via vmbus_channel_map_relid(), and offer_in_progress is decremented before vmbus_add_channel_work() is queued. If a rescind offer arrives while vmbus_add_channel_work() is running, vmbus_onoffer_rescind() looks up the channel, sets channel->rescind_ref = true and channel->rescind = true, and waits for probe_done without holding a kobject reference or an RCU read lock:

drivers/hv/channel_mgmt.c:vmbus_onoffer_rescind() {
	...
	while (READ_ONCE(channel->probe_done) == false) {
		msleep(1);
	}

	if (channel->device_obj) {
		...
	} else if (channel->primary_channel != NULL) {
		mutex_lock(&vmbus_connection.channel_mutex);
		if (channel->state == CHANNEL_OPEN_STATE) {
			hv_process_channel_removal(channel);
		}
		...
}

When vmbus_device_register() fails (or when vmbus_device_create() or vmbus_add_channel_kobj() fails), err_deq_chan sets newchannel->probe_done = true, unlinks and unmaps newchannel, and calls free_channel(newchannel) -> kobject_put() -> vmbus_chan_release() -> kfree_rcu(channel, rcu):

drivers/hv/channel_mgmt.c:vmbus_add_channel_work() {
	...
err_deq_chan:
	mutex_lock(&vmbus_connection.channel_mutex);
	newchannel->probe_done = true;

	if (primary_channel == NULL)
		list_del(&newchannel->listentry);
	else
		list_del(&newchannel->sc_list);

	vmbus_channel_unmap_relid(newchannel);
	mutex_unlock(&vmbus_connection.channel_mutex);

	vmbus_release_relid(newchannel->offermsg.child_relid);
	free_channel(newchannel);
}

Because vmbus_onoffer_rescind() is sleeping in msleep(1), the RCU grace period can finish before vmbus_onoffer_rescind() wakes up and dereferences channel->probe_done, channel->device_obj, and channel->primary_channel.

In addition, on weakly ordered architectures, newchannel->device_obj = NULL and newchannel->probe_done = true are plain stores separated only by mutex_lock(&vmbus_connection.channel_mutex) (an acquire barrier, not a release barrier), while vmbus_onoffer_rescind() reads READ_ONCE(channel->probe_done) and channel->device_obj without holding channel_mutex or using load-acquire. Could vmbus_onoffer_rescind() observe probe_done == true while still seeing the stale non-NULL channel->device_obj pointer to the freed hv_device?

Finally, for a sub-channel failing in vmbus_add_channel_kobj(), newchannel->state was already set to CHANNEL_OPEN_STATE at the start of vmbus_add_channel_work(). When vmbus_onoffer_rescind() wakes up and enters the channel->primary_channel != NULL branch, wouldn't it see channel->state == CHANNEL_OPEN_STATE and call hv_process_channel_removal(channel) a second time after err_deq_chan has already unlinked, unmapped, and freed it?

Quoted 4 lines
drivers/hv/vmbus_drv.c +0 -0
index fc0f16f512669..b41ce24e200c8 100644

[ ... ]

Quoted 8 lines
drivers/hv/vmbus_drv.c +1 -3
@@ -979,9 +994,15 @@ static void vmbus_device_release(struct device *device)
 
 	hv_debug_rm_dev_dir(hv_dev);
 
-	mutex_lock(&vmbus_connection.channel_mutex);
-	hv_process_channel_removal(channel);
-	mutex_unlock(&vmbus_connection.channel_mutex);
+	/*

drivers/hv/vmbus_drv.c static void vmbus_device_release(struct device *device) · line 1007

Quoted 10 lines
drivers/hv/vmbus_drv.c +8 -0
+	 * No channel if vmbus_device_register() failed: the channel was not
+	 * rescinded, and vmbus_add_channel_work() cleans it up.
+	 */
+	if (channel) {
+		mutex_lock(&vmbus_connection.channel_mutex);
+		hv_process_channel_removal(channel);
+		mutex_unlock(&vmbus_connection.channel_mutex);
+	}
 	kfree(hv_dev);
 }

[Severity: High]
As reported by Sashiko on v2 [1], when vmbus_device_release() skips hv_process_channel_removal() and leaves cleanup to err_deq_chan in vmbus_add_channel_work(), does err_deq_chan leak the allocated CPU bit in hv_numa_map and the nr_chan_close_on_suspend counter?

Before queuing vmbus_add_channel_work(), vmbus_process_offer() allocates target_cpu and increments nr_chan_close_on_suspend:

drivers/hv/channel_mgmt.c:vmbus_process_offer() {
	...
	init_vp_index(newchannel);

	/* Remember the channels that should be cleaned up upon suspend. */
	if (is_hvsock_channel(newchannel) || is_sub_channel(newchannel))
		atomic_inc(&vmbus_connection.nr_chan_close_on_suspend);
	...
}

Unlike hv_process_channel_removal(), err_deq_chan in vmbus_add_channel_work() does not call hv_clear_allocated_cpu(newchannel->target_cpu) when hv_is_perf_channel(newchannel) is true.

It also unmaps the channel's relid via vmbus_channel_unmap_relid(newchannel) without decrementing vmbus_connection.nr_chan_close_on_suspend (via check_ready_for_suspend_event()) when is_hvsock_channel(newchannel) or is_sub_channel(newchannel) is true. Because the relid is unmapped, even a later rescind from the host will see relid2channel() == NULL in vmbus_onoffer_rescind() and return early. Wouldn't that leave nr_chan_close_on_suspend permanently elevated and cause a subsequent vmbus_bus_suspend() to hang indefinitely in wait_for_completion(&vmbus_connection.ready_for_suspend_event)?

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2