Skip to content

Commit dcf40ed

Browse files
jgunthorpejoergroedel
authored andcommitted
iommu: Make __iommu_group_set_domain() handle error unwind
Let's try to have a consistent and clear strategy for error handling during domain attach failures. There are two broad categories, the first is callers doing destruction and trying to set the domain back to a previously good domain. These cases cannot handle failure during destruction flows and must succeed, or at least avoid a UAF on the current group->domain which is likely about to be freed. Many of the drivers are well behaved here and will not hit the WARN_ON's or a UAF, but some are doing hypercalls/etc that can fail unpredictably and don't meet the expectations. The second case is attaching a domain for the first time in a failable context, failure should restore the attachment back to group->domain using the above unfailable operation. Have __iommu_group_set_domain_internal() execute a common algorithm that tries to achieve this, and in the worst case, would leave a device "detached" or assigned to a global blocking domain. This relies on some existing common driver behaviors where attach failure will also do detatch and true IOMMU_DOMAIN_BLOCK implementations that are not allowed to ever fail. Name the first case with __iommu_group_set_domain_nofail() to make it clear. Pull all the error handling and WARN_ON generation into __iommu_group_set_domain_internal(). Avoid the obfuscating use of __iommu_group_for_each_dev() and be more careful about what should happen during failures by only touching devices we've already touched. Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Tested-by: Heiko Stuebner <heiko@sntech.de> Tested-by: Niklas Schnelle <schnelle@linux.ibm.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/3-v5-1b99ae392328+44574-iommu_err_unwind_jgg@nvidia.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 3006b15 commit dcf40ed

1 file changed

Lines changed: 112 additions & 25 deletions

File tree

drivers/iommu/iommu.c

Lines changed: 112 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,26 @@ static int __iommu_attach_device(struct iommu_domain *domain,
101101
struct device *dev);
102102
static int __iommu_attach_group(struct iommu_domain *domain,
103103
struct iommu_group *group);
104+
105+
enum {
106+
IOMMU_SET_DOMAIN_MUST_SUCCEED = 1 << 0,
107+
};
108+
109+
static int __iommu_group_set_domain_internal(struct iommu_group *group,
110+
struct iommu_domain *new_domain,
111+
unsigned int flags);
104112
static int __iommu_group_set_domain(struct iommu_group *group,
105-
struct iommu_domain *new_domain);
113+
struct iommu_domain *new_domain)
114+
{
115+
return __iommu_group_set_domain_internal(group, new_domain, 0);
116+
}
117+
static void __iommu_group_set_domain_nofail(struct iommu_group *group,
118+
struct iommu_domain *new_domain)
119+
{
120+
WARN_ON(__iommu_group_set_domain_internal(
121+
group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED));
122+
}
123+
106124
static int iommu_create_device_direct_mappings(struct iommu_group *group,
107125
struct device *dev);
108126
static struct iommu_group *iommu_group_get_for_dev(struct device *dev);
@@ -2022,15 +2040,13 @@ EXPORT_SYMBOL_GPL(iommu_domain_free);
20222040
static void __iommu_group_set_core_domain(struct iommu_group *group)
20232041
{
20242042
struct iommu_domain *new_domain;
2025-
int ret;
20262043

20272044
if (group->owner)
20282045
new_domain = group->blocking_domain;
20292046
else
20302047
new_domain = group->default_domain;
20312048

2032-
ret = __iommu_group_set_domain(group, new_domain);
2033-
WARN(ret, "iommu driver failed to attach the default/blocking domain");
2049+
__iommu_group_set_domain_nofail(group, new_domain);
20342050
}
20352051

20362052
static int __iommu_attach_device(struct iommu_domain *domain,
@@ -2215,21 +2231,55 @@ int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
22152231
}
22162232
EXPORT_SYMBOL_GPL(iommu_attach_group);
22172233

2218-
static int iommu_group_do_set_platform_dma(struct device *dev, void *data)
2234+
static int __iommu_device_set_domain(struct iommu_group *group,
2235+
struct device *dev,
2236+
struct iommu_domain *new_domain,
2237+
unsigned int flags)
22192238
{
2220-
const struct iommu_ops *ops = dev_iommu_ops(dev);
2221-
2222-
if (!WARN_ON(!ops->set_platform_dma_ops))
2223-
ops->set_platform_dma_ops(dev);
2239+
int ret;
22242240

2241+
ret = __iommu_attach_device(new_domain, dev);
2242+
if (ret) {
2243+
/*
2244+
* If we have a blocking domain then try to attach that in hopes
2245+
* of avoiding a UAF. Modern drivers should implement blocking
2246+
* domains as global statics that cannot fail.
2247+
*/
2248+
if ((flags & IOMMU_SET_DOMAIN_MUST_SUCCEED) &&
2249+
group->blocking_domain &&
2250+
group->blocking_domain != new_domain)
2251+
__iommu_attach_device(group->blocking_domain, dev);
2252+
return ret;
2253+
}
22252254
return 0;
22262255
}
22272256

2228-
static int __iommu_group_set_domain(struct iommu_group *group,
2229-
struct iommu_domain *new_domain)
2257+
/*
2258+
* If 0 is returned the group's domain is new_domain. If an error is returned
2259+
* then the group's domain will be set back to the existing domain unless
2260+
* IOMMU_SET_DOMAIN_MUST_SUCCEED, otherwise an error is returned and the group's
2261+
* domains is left inconsistent. This is a driver bug to fail attach with a
2262+
* previously good domain. We try to avoid a kernel UAF because of this.
2263+
*
2264+
* IOMMU groups are really the natural working unit of the IOMMU, but the IOMMU
2265+
* API works on domains and devices. Bridge that gap by iterating over the
2266+
* devices in a group. Ideally we'd have a single device which represents the
2267+
* requestor ID of the group, but we also allow IOMMU drivers to create policy
2268+
* defined minimum sets, where the physical hardware may be able to distiguish
2269+
* members, but we wish to group them at a higher level (ex. untrusted
2270+
* multi-function PCI devices). Thus we attach each device.
2271+
*/
2272+
static int __iommu_group_set_domain_internal(struct iommu_group *group,
2273+
struct iommu_domain *new_domain,
2274+
unsigned int flags)
22302275
{
2276+
struct group_device *last_gdev;
2277+
struct group_device *gdev;
2278+
int result;
22312279
int ret;
22322280

2281+
lockdep_assert_held(&group->mutex);
2282+
22332283
if (group->domain == new_domain)
22342284
return 0;
22352285

@@ -2239,8 +2289,12 @@ static int __iommu_group_set_domain(struct iommu_group *group,
22392289
* platform specific behavior.
22402290
*/
22412291
if (!new_domain) {
2242-
__iommu_group_for_each_dev(group, NULL,
2243-
iommu_group_do_set_platform_dma);
2292+
for_each_group_device(group, gdev) {
2293+
const struct iommu_ops *ops = dev_iommu_ops(gdev->dev);
2294+
2295+
if (!WARN_ON(!ops->set_platform_dma_ops))
2296+
ops->set_platform_dma_ops(gdev->dev);
2297+
}
22442298
group->domain = NULL;
22452299
return 0;
22462300
}
@@ -2250,16 +2304,52 @@ static int __iommu_group_set_domain(struct iommu_group *group,
22502304
* domain. This switch does not have to be atomic and DMA can be
22512305
* discarded during the transition. DMA must only be able to access
22522306
* either new_domain or group->domain, never something else.
2253-
*
2254-
* Note that this is called in error unwind paths, attaching to a
2255-
* domain that has already been attached cannot fail.
22562307
*/
2257-
ret = __iommu_group_for_each_dev(group, new_domain,
2258-
iommu_group_do_attach_device);
2259-
if (ret)
2260-
return ret;
2308+
result = 0;
2309+
for_each_group_device(group, gdev) {
2310+
ret = __iommu_device_set_domain(group, gdev->dev, new_domain,
2311+
flags);
2312+
if (ret) {
2313+
result = ret;
2314+
/*
2315+
* Keep trying the other devices in the group. If a
2316+
* driver fails attach to an otherwise good domain, and
2317+
* does not support blocking domains, it should at least
2318+
* drop its reference on the current domain so we don't
2319+
* UAF.
2320+
*/
2321+
if (flags & IOMMU_SET_DOMAIN_MUST_SUCCEED)
2322+
continue;
2323+
goto err_revert;
2324+
}
2325+
}
22612326
group->domain = new_domain;
2262-
return 0;
2327+
return result;
2328+
2329+
err_revert:
2330+
/*
2331+
* This is called in error unwind paths. A well behaved driver should
2332+
* always allow us to attach to a domain that was already attached.
2333+
*/
2334+
last_gdev = gdev;
2335+
for_each_group_device(group, gdev) {
2336+
const struct iommu_ops *ops = dev_iommu_ops(gdev->dev);
2337+
2338+
/*
2339+
* If set_platform_dma_ops is not present a NULL domain can
2340+
* happen only for first probe, in which case we leave
2341+
* group->domain as NULL and let release clean everything up.
2342+
*/
2343+
if (group->domain)
2344+
WARN_ON(__iommu_device_set_domain(
2345+
group, gdev->dev, group->domain,
2346+
IOMMU_SET_DOMAIN_MUST_SUCCEED));
2347+
else if (ops->set_platform_dma_ops)
2348+
ops->set_platform_dma_ops(gdev->dev);
2349+
if (gdev == last_gdev)
2350+
break;
2351+
}
2352+
return ret;
22632353
}
22642354

22652355
void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
@@ -3176,16 +3266,13 @@ EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner);
31763266

31773267
static void __iommu_release_dma_ownership(struct iommu_group *group)
31783268
{
3179-
int ret;
3180-
31813269
if (WARN_ON(!group->owner_cnt || !group->owner ||
31823270
!xa_empty(&group->pasid_array)))
31833271
return;
31843272

31853273
group->owner_cnt = 0;
31863274
group->owner = NULL;
3187-
ret = __iommu_group_set_domain(group, group->default_domain);
3188-
WARN(ret, "iommu driver failed to attach the default domain");
3275+
__iommu_group_set_domain_nofail(group, group->default_domain);
31893276
}
31903277

31913278
/**

0 commit comments

Comments
 (0)