RE: Plan for /dev/ioasid RFC v2

From: Tian, Kevin
Date: Sun Jun 13 2021 - 23:09:40 EST


> From: Alex Williamson <alex.williamson@xxxxxxxxxx>
> Sent: Saturday, June 12, 2021 5:39 AM
>
> On Fri, 11 Jun 2021 00:58:35 +0000
> "Tian, Kevin" <kevin.tian@xxxxxxxxx> wrote:
>
> > Hi, Alex,
> >
> > > From: Alex Williamson <alex.williamson@xxxxxxxxxx>
> > > Sent: Thursday, June 10, 2021 11:39 PM
> > >
> > > On Wed, 9 Jun 2021 15:49:40 -0300
> > > Jason Gunthorpe <jgg@xxxxxxxxxx> wrote:
> > >
> > > > On Wed, Jun 09, 2021 at 10:27:22AM -0600, Alex Williamson wrote:
> > > >
> > > > > > > It is a kernel decision, because a fundamental task of the kernel is
> to
> > > > > > > ensure isolation between user-space tasks as good as it can. And if
> a
> > > > > > > device assigned to one task can interfer with a device of another
> task
> > > > > > > (e.g. by sending P2P messages), then the promise of isolation is
> > > broken.
> > > > > >
> > > > > > AIUI, the IOASID model will still enforce IOMMU groups, but it's not
> an
> > > > > > explicit part of the interface like it is for vfio. For example the
> > > > > > IOASID model allows attaching individual devices such that we have
> > > > > > granularity to create per device IOASIDs, but all devices within an
> > > > > > IOMMU group are required to be attached to an IOASID before they
> can
> > > be
> > > > > > used.
> > > >
> > > > Yes, thanks Alex
> > > >
> > > > > > It's not entirely clear to me yet how that last bit gets
> > > > > > implemented though, ie. what barrier is in place to prevent device
> > > > > > usage prior to reaching this viable state.
> > > >
> > > > The major security checkpoint for the group is on the VFIO side. We
> > > > must require the group before userspace can be allowed access to any
> > > > device registers. Obtaining the device_fd from the group_fd does this
> > > > today as the group_fd is the security proof.
> > > >
> > > > Actually, thinking about this some more.. If the only way to get a
> > > > working device_fd in the first place is to get it from the group_fd
> > > > and thus pass a group-based security check, why do we need to do
> > > > anything at the ioasid level?
> > > >
> > > > The security concept of isolation was satisfied as soon as userspace
> > > > opened the group_fd. What do more checks in the kernel accomplish?
> > >
> > > Opening the group is not the extent of the security check currently
> > > required, the group must be added to a container and an IOMMU model
> > > configured for the container *before* the user can get a devicefd.
> > > Each devicefd creates a reference to this security context, therefore
> > > access to a device does not exist without such a context.
> >
> > IIUC each device has a default domain when it's probed by iommu driver
> > at boot time. This domain includes an empty page table, implying that
> > device is already in a security context before it's probed by device driver.
>
> The default domain could be passthrough though, right?

Good point.

>
> > Now when this device is added to vfio, vfio creates another security
> > context through above sequence. This sequence requires the device to
> > switch from default security context to this new one, before it can be
> > accessed by user.
>
> This is true currently, we use group semantics with the type1 IOMMU
> backend to attach all devices in the group to a secure context,
> regardless of the default domain.
>
> > Then I wonder whether it's really necessary. As long as a device is in
> > a security context at any time, access to a device can be allowed. The
> > user itself should ensure that the access happens only after the device
> > creates a reference to the new security context that is desired by this
> > user.
> >
> > Then what does group really bring to us?
>
> By definition an IOMMU group is the smallest set of devices that we
> can consider isolated from all other devices. Therefore devices in a
> group are not necessarily isolated from each other. Therefore if any
> device within a group is not isolated, the group is not isolated. VFIO
> needs to know when it's safe to provide userspace access to the device,
> but the device isolation is dependent on the group isolation. The
> group is therefore part of this picture whether implicit or explicit.
>
> > With this new proposal we just need to make sure that a device cannot
> > be attached to any IOASID before all devices in its group are bound to
> > the IOASIDfd. If we want to start with a vfio-like policy, then all devices
> > in the group must be attached to the same IOASID. Or as Jason suggests,
> > they can attach to different IOASIDs (if in the group due to !ACS) if the
> > user wants, or have some devices attached while others detached since
> > both are in a security context anyway.
>
> But if it's the device attachment to the IOASID that provides the
> isolation and the user might attach a device to multiple IOASIDs within
> the same IOASIDfd, and presumably make changes to the mapping of device
> to IOASID dynamically, are we interrupting user access around each of
> those changes? How would vfio be able to track this, and not only
> track it per device, but for all devices in the group. Suggesting a
> user needs to explicitly attach every device in the group is also a
> semantic change versus existing vfio, where other devices in the group
> must only be considered to be in a safe state for the group to be
> usable.
>
> The default domain may indeed be a solution to the problem, but we need
> to enforce a secure default domain for all devices in the group. To me
> that suggests that binding the *group* to an IOASIDfd is the point at
> which device access becomes secure. VFIO should be able to consider
> that the IOASIDfd binding has taken over ownership of the DMA context
> for the device and it will always be either an empty, isolated, default
> domain or a user defined IOASID.

Yes, this is one way of enforcing the group security.

In the meantime, I'm thinking about another way whether group
security can be enforced in the iommu layer to relax the uAPI design.
If a device can be always blocked from accessing memory in the
IOMMU before it's bound to a driver or more specifically before
the driver moves it to a new security context, then there is no need
for VFIO to track whether IOASIDfd has taken over ownership of
the DMA context for all devices within a group.

But as you said this cannot be achieved via existing default domain
approach. So far a device is always attached to a domain:

- DOMAIN_IDENTITY: a default domain without DMA protection
- DOMAIN_DMA: a default domain with DMA protection via DMA
API and iommu core
- DOMAIN_UNMANAGED: a driver-created domain which is not
managed by iommu core.

The special sequence in current vfio group design is to mitigate
the 1st case, i.e. if a device is left in passthrough mode before
bound to VFIO it's definitely insecure to allow user to access it.
Then the sequence ensures that the user access is granted on it
only after all devices within a group switch to a security context.

Now if the new proposed scheme can be supported, a device
is always in a security context (block-dma) before it's switched
to a new security context and existing domain types should be
applied only in the new context when the device starts to do
DMAs. For VFIO case this switch happens explicitly when attaching
the device to an IOASID. For kernel driver it's implicit e.g. could
happen when the 1st DMA API call is received.

If this works I didn't see the need for vfio to keep the sequence.
VFIO still keeps group fd to claim ownership of all devices in a
group. Once it's done, vfio doesn't need to track the device attach
status and user access can be always granted regardless of
how the attach status changes. Moving a device from IOASID1
to IOASID2 involves detaching from IOASID1 (back to blocked
dma context) and then reattaching to IOASID2 (switch to a
new security context).

Following this direction even IOASIDfd doesn't need to verify
the group attach upon such guarantee from the iommu layer.
The devices within a group can be in different security contexts,
e.g. with some devices attached to GPA IOASID while others not
attached. In this way vfio userspace could choose to not attach
every device of a group to sustain the current semantics.

>
> Maybe the model relative to vfio is something like:
>
> 1. bind a group to an IOASIDfd
> VFIO_GROUP_BIND_IOASID_FD(groupfd, ioasidfd)
> 2. create an IOASID label for each device
> VFIO_DEVICE_SET_IOASID_LABEL(devicefd, device_ioasid_label)
>
> VFIO can open access to the device after step 1, the IOASIDfd takes
> responsibility for the device IOMMU context. After step 2, shouldn't
> the user switch to the IOASID uAPI? I don't see why vfio would be
> involved in attaching devices to specific IOASID contexts within the
> IOASIDfd at that point, we might need internal compatibility
> interfaces, but a native IOASID user should have all they need to
> attach device labels to IOASIDs using the IOASIDfd at this point.

In this proposal VFIO device driver is also responsible for PASID
virtualization since it's a per-device policy that only VFIO knows.
VFIO needs to provide PASID as routing information when doing
device bind. This is one open which hasn't been thoroughly
discussed in v1 and I'll have more clarification on this part in v2.

>
> We'll need to figure out what the release model looks like too. A
> group should hold a reference on the IOASIDfd and each open device
> should hold a reference on the group so that the isolation of the group
> cannot be broken while any device is open.
>
> > > This proposal has of course put the device before the group, which then
> > > makes it more difficult for vfio to retroactively enforce security.
> > >
> > > > Yes, we have the issue where some groups require all devices to use
> > > > the same IOASID, but once someone has the group_fd that is no longer
> a
> > > > security issue. We can fail VFIO_DEVICE_ATTACH_IOASID callss that
> > > > don't make sense.
> > >
> > > The groupfd only proves the user has an ownership claim to the devices,
> > > it does not itself prove that the devices are in an isolated context.
> > > Device access is not granted until that isolated context is configured.
> > >
> > > vfio owns the device, so it would make sense for vfio to enforce the
> > > security of device access only in a secure context, but how do we know
> > > a device is in a secure context?
> > >
> > > Is it sufficient to track the vfio device ioctls for attach/detach for
> > > an IOASID or will the user be able to manipulate IOASID configuration
> > > for a device directly via the IOASIDfd?
> > >
> > > What happens on detach? As we've discussed elsewhere in this thread,
> > > revoking access is more difficult than holding a reference to the
> > > secure context, but I'm under the impression that moving a device
> > > between IOASIDs could be standard practice in this new model. A device
> > > that's detached from a secure context, even temporarily, is a problem.
> > > Access to other devices in the same group as a device detached from a
> > > secure context is a problem.
> >
> > as long as the device is switched back to the default security context
> > after detach then it should be fine.
>
> So long as the default context is secure, and ideally if IOMMU context
> switches are atomic.

as long as every switch is to/from a block-dma context, then it should work. 😊

>
> > > > > > > > Groups should be primarily about isolation security, not about
> > > IOASID
> > > > > > > > matching.
> > > > > > >
> > > > > > > That doesn't make any sense, what do you mean by 'IOASID
> matching'?
> > > > > >
> > > > > > One of the problems with the vfio interface use of groups is that we
> > > > > > conflate the IOMMU group for both isolation and granularity. I
> think
> > > > > > what Jason is referring to here is that we still want groups to be the
> > > > > > basis of isolation, but we don't want a uAPI that presumes all
> devices
> > > > > > within the group must use the same IOASID.
> > > >
> > > > Yes, thanks again Alex
> > > >
> > > > > > For example, if a user owns an IOMMU group consisting of
> > > > > > non-isolated functions of a multi-function device, they should be
> > > > > > able to create a vIOMMU VM where each of those functions has its
> > > > > > own address space. That can't be done today, the entire group
> > > > > > would need to be attached to the VM under a PCIe-to-PCI bridge to
> > > > > > reflect the address space limitation imposed by the vfio group
> > > > > > uAPI model. Thanks,
> > > > >
> > > > > Hmm, likely discussed previously in these threads, but I can't come up
> > > > > with the argument that prevents us from making the BIND interface
> > > > > at the group level but the ATTACH interface at the device level? For
> > > > > example:
> > > > >
> > > > > - VFIO_GROUP_BIND_IOASID_FD
> > > > > - VFIO_DEVICE_ATTACH_IOASID
> > > > >
> > > > > AFAICT that makes the group ownership more explicit but still allows
> > > > > the device level IOASID granularity. Logically this is just an
> > > > > internal iommu_group_for_each_dev() in the BIND ioctl. Thanks,
> > > >
> > > > At a high level it sounds OK.
> > > >
> > > > However I think your above question needs to be answered - what do
> we
> > > > want to enforce on the iommu_fd and why?
> > > >
> > > > Also, this creates a problem with the device label idea, we still
> > > > need to associate each device_fd with a label, so your above sequence
> > > > is probably:
> > > >
> > > > VFIO_GROUP_BIND_IOASID_FD(group fd)
> > > > VFIO_BIND_IOASID_FD(device fd 1, device_label)
> > > > VFIO_BIND_IOASID_FD(device fd 2, device_label)
> > > > VFIO_DEVICE_ATTACH_IOASID(..)
> > > >
> > > > And then I think we are back to where I had started, we can trigger
> > > > whatever VFIO_GROUP_BIND_IOASID_FD does automatically as soon as
> all
> > > > of the devices in the group have been bound.
> > >
> > > How to label a device seems like a relatively mundane issue relative to
> > > ownership and isolated contexts of groups and devices. The label is
> > > essentially just creating an identifier to device mapping, where the
> > > identifier (label) will be used in the IOASID interface, right? As I
> >
> > Three usages in v2:
> >
> > 1) when reporting per-device capability/format info to user;
> > 2) when handling device-wide iotlb invalidation from user;
> > 3) when reporting device-specific fault data to user;
>
> As above, it seems more complete to me to move attach/detach of devices
> to IOASIDs using the labels as well. Thanks,
>

Thanks
Kevin