From: Jason Gunthorpe <jgg@ziepe.ca>
To: Robin Murphy <robin.murphy@arm.com>
Cc: joro@8bytes.org, will@kernel.org, iommu@lists.linux.dev,
linux-arm-kernel@lists.infradead.org, m.szyprowski@samsung.com,
heiko@sntech.de, jernej.skrabec@gmail.com,
thierry.reding@gmail.com, vdumpa@nvidia.com
Subject: Re: [PATCH 8/8] iommu: Improve map/unmap sanity checks
Date: Thu, 14 Sep 2023 13:48:25 -0300 [thread overview]
Message-ID: <ZQM5WRzyQkk7sq+t@ziepe.ca> (raw)
In-Reply-To: <e37085dc-cac4-b7f4-8c2e-e51e56a66887@arm.com>
On Thu, Sep 14, 2023 at 03:23:46PM +0100, Robin Murphy wrote:
> > > > > diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> > > > > index c83f2e4c56f5..391bcae4d02d 100644
> > > > > --- a/drivers/iommu/iommu.c
> > > > > +++ b/drivers/iommu/iommu.c
> > > > > @@ -2427,12 +2427,12 @@ static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
> > > > > phys_addr_t orig_paddr = paddr;
> > > > > int ret = 0;
> > > > > - if (unlikely(!ops->map_pages || domain->pgsize_bitmap == 0UL))
> > > > > - return -ENODEV;
> > > > > -
> > > > > if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
> > > > > return -EINVAL;
> > > >
> > > > Why isn't this a WARN_ON? The caller is clearly lost its mind if it
> > > > is calling map on a non paging domain..
> > >
> > > Sure it's a dumb thing to do, however I don't think we can reasonably say
> > > without question that an external caller being dumb represents an unexpected
> > > and serious loss of internal consistency.
> >
> > WARN_ON is not just for "serious loss of internal consistency" it
> > should be use in all places where invariant are violated. We can't
> > guess why the caller is using this wrong (most likely it is UAF or
> > memory corruption), but if this fires something definately has gone
> > wrong with the kernel.
>
> Has it? It's not *functionally* incorrect to obtain a valid domain by
> calling iommu_get_domain_for_dev(), pass that domain to iommu_map(), and
> handle any failure returned.
This is not a theoretical question - does any in-kernel code actually
do that and expect it to work? I didn't notice any..
It doesn't matter that someone *could*, our task is not to make an
overly general API. We can, and should, have tight invarients because
it allows discovery of more bug classes, and prevents "creative" use
of APIs.
> is calling iommu_iova_to_phys() on non-paging domains, and we have to
> support that, because callers are dumb, and "callers aren't dumb" is not a
> realistic invariant which we can uphold.
Someone does that? :(
> > eg if syzkaller somehow hits this we want the WARN_ON so it reports
> > it.
>
> But then an "IOMMU bug" is reported to us, and we say "yeah, that's not
> ours, that's some code somewhere down in the middle of that callstack being
> dumb", and then what?
The report gets handed off to the part of the callstack that is making
the problem.
> iommu_map() can already fail for numerous reasons that may or may not
> represent a bug in the caller, like the IOVA or PA being out-of-range, or
> the IOVA already being mapped, or whatever.
IMHO some of those certainly could be WARN too, depends what you think
the API should be.
eg iommufd and vfio both are designed to never double map/use out of
aperture IOVA/etc, if they do it is a significant bug and a WARN_ON
would be welcomed.
> another reason to fail (in, as far as we're concerned, an entirely safe and
> manageable way). Callers have to be prepared to handle failure, but it's up
> to them how unexpected or serious it is; we can't second-guess them.
We can always convert things into error codes, but that can also hide
bugs if no existing caller needs that functionality. IMHO it is always
better to have tighter invarients, and be noisy when they fail. This
gives us better understandablity, bug discoverability and discourages
people from introducing new code doing crazy things.
Jason
next prev parent reply other threads:[~2023-09-14 16:48 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-12 16:18 [PATCH 0/8] iommu: Clean up map/unmap ops Robin Murphy
2023-09-12 16:18 ` [PATCH 1/8] iommu/exynos: Update to {map,unmap}_pages Robin Murphy
2023-09-12 16:18 ` [PATCH 2/8] iommu/omap: " Robin Murphy
2023-09-12 16:18 ` [PATCH 3/8] iommu/rockchip: " Robin Murphy
2023-09-12 16:18 ` [PATCH 4/8] iommu/sun50i: " Robin Murphy
2023-09-12 16:18 ` [PATCH 5/8] iommu/tegra-gart: " Robin Murphy
2023-09-12 16:18 ` [PATCH 6/8] iommu/tegra-smmu: " Robin Murphy
2023-09-12 16:18 ` [PATCH 7/8] iommu: Retire map/unmap ops Robin Murphy
2023-09-13 14:40 ` Jason Gunthorpe
2023-09-12 16:18 ` [PATCH 8/8] iommu: Improve map/unmap sanity checks Robin Murphy
2023-09-13 14:39 ` Jason Gunthorpe
2023-09-13 18:46 ` Robin Murphy
2023-09-14 12:48 ` Jason Gunthorpe
2023-09-14 14:23 ` Robin Murphy
2023-09-14 16:48 ` Jason Gunthorpe [this message]
2023-09-19 12:18 ` Robin Murphy
2023-09-22 17:28 ` Jason Gunthorpe
2023-09-25 10:05 ` [PATCH 0/8] iommu: Clean up map/unmap ops Joerg Roedel
2023-09-25 15:33 ` Jernej Škrabec
2023-09-26 9:23 ` Joerg Roedel
2023-09-26 12:01 ` Robin Murphy
2023-09-26 14:57 ` Joerg Roedel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZQM5WRzyQkk7sq+t@ziepe.ca \
--to=jgg@ziepe.ca \
--cc=heiko@sntech.de \
--cc=iommu@lists.linux.dev \
--cc=jernej.skrabec@gmail.com \
--cc=joro@8bytes.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=m.szyprowski@samsung.com \
--cc=robin.murphy@arm.com \
--cc=thierry.reding@gmail.com \
--cc=vdumpa@nvidia.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox