Re: [PATCH 02/11] driver core: Set DMA ownership during driver bind/unbind

From: Greg Kroah-Hartman
Date: Mon Nov 15 2021 - 01:59:36 EST


On Mon, Nov 15, 2021 at 10:05:43AM +0800, Lu Baolu wrote:
> This extends really_probe() to allow checking for dma ownership conflict
> during the driver binding process. By default, the DMA_OWNER_KERNEL is
> claimed for the bound driver before calling its .probe() callback. If this
> operation fails (e.g. the iommu group of the target device already has the
> DMA_OWNER_USER set), the binding process is aborted to avoid breaking the
> security contract for devices in the iommu group.
>
> Without this change, the vfio driver has to listen to a bus BOUND_DRIVER
> event and then BUG_ON() in case of dma ownership conflict. This leads to
> bad user experience since careless driver binding operation may crash the
> system if the admin overlooks the group restriction.
>
> Aside from bad design, this leads to a security problem as a root user,
> even with lockdown=integrity, can force the kernel to BUG.
>
> Driver may set a new flag (suppress_auto_claim_dma_owner) to disable auto
> claim in the binding process. Examples include kernel drivers (pci_stub,
> PCI bridge drivers, etc.) which don't trigger DMA at all thus can be safely
> exempted in DMA ownership check and userspace framework drivers (vfio/vdpa
> etc.) which need to manually claim DMA_OWNER_USER when assigning a device
> to userspace.
>
> Suggested-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
> Link: https://lore.kernel.org/linux-iommu/20210922123931.GI327412@xxxxxxxxxx/
> Link: https://lore.kernel.org/linux-iommu/20210928115751.GK964074@xxxxxxxxxx/
> Signed-off-by: Lu Baolu <baolu.lu@xxxxxxxxxxxxxxx>
> ---
> include/linux/device/driver.h | 7 ++++++-
> drivers/base/dd.c | 12 ++++++++++++
> 2 files changed, 18 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/device/driver.h b/include/linux/device/driver.h
> index a498ebcf4993..25d39c64c4d9 100644
> --- a/include/linux/device/driver.h
> +++ b/include/linux/device/driver.h
> @@ -54,6 +54,10 @@ enum probe_type {
> * @owner: The module owner.
> * @mod_name: Used for built-in modules.
> * @suppress_bind_attrs: Disables bind/unbind via sysfs.
> + * @suppress_auto_claim_dma_owner: Disable auto claiming of kernel DMA owner.
> + * Drivers which don't require DMA or want to manually claim the
> + * owner type (e.g. userspace driver frameworks) could set this
> + * flag.
> * @probe_type: Type of the probe (synchronous or asynchronous) to use.
> * @of_match_table: The open firmware table.
> * @acpi_match_table: The ACPI match table.
> @@ -99,7 +103,8 @@ struct device_driver {
> struct module *owner;
> const char *mod_name; /* used for built-in modules */
>
> - bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
> + bool suppress_bind_attrs:1; /* disables bind/unbind via sysfs */
> + bool suppress_auto_claim_dma_owner:1;

Can a bool be a bitfield? Is that valid C?

And why is that even needed?

> enum probe_type probe_type;
>
> const struct of_device_id *of_match_table;
> diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> index 68ea1f949daa..ab3333351f19 100644
> --- a/drivers/base/dd.c
> +++ b/drivers/base/dd.c
> @@ -28,6 +28,7 @@
> #include <linux/pm_runtime.h>
> #include <linux/pinctrl/devinfo.h>
> #include <linux/slab.h>
> +#include <linux/iommu.h>
>
> #include "base.h"
> #include "power/power.h"
> @@ -566,6 +567,12 @@ static int really_probe(struct device *dev, struct device_driver *drv)
> goto done;
> }
>
> + if (!drv->suppress_auto_claim_dma_owner) {
> + ret = iommu_device_set_dma_owner(dev, DMA_OWNER_KERNEL, NULL);
> + if (ret)
> + return ret;
> + }
> +

This feels wrong to be doing it in the driver core, why doesn't the bus
that cares about this handle it instead?

You just caused all drivers in the kernel today to set and release this
ownership, as none set this flag. Shouldn't it be the other way around?

And again, why not in the bus that cares?

You only have problems with 1 driver out of thousands, this feels wrong
to abuse the driver core this way for just that one.

thanks,

greg k-h