mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yi Liu <yi.l.liu@intel.com>
To: Jason Gunthorpe <jgg@nvidia.com>
Cc: "Tian, Kevin" <kevin.tian@intel.com>,
	Nicolin Chen <nicolinc@nvidia.com>,
	"joro@8bytes.org" <joro@8bytes.org>,
	Lu Baolu <baolu.lu@linux.intel.com>,
	Will Deacon <will@kernel.org>,
	Robin Murphy <robin.murphy@arm.com>,
	Jean-Philippe Brucker <jpb@kernel.org>,
	"iommu@lists.linux.dev" <iommu@lists.linux.dev>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v1 3/4] iommu: Unpublish the old attach handle before the group replace callback
Date: Fri, 30 Oct 2026 16:22:11 +0800	[thread overview]
Message-ID: <943304cd-112d-4bd8-ad3a-f70359f15aca@intel.com> (raw)
In-Reply-To: <20261029184544.GF3734323@nvidia.com>

On 10/30/26 02:45, Jason Gunthorpe wrote:
> On Thu, Oct 29, 2026 at 07:00:18PM +0800, Yi Liu wrote:
>> On 10/29/26 15:25, Tian, Kevin wrote:
>>>> From: Nicolin Chen <nicolinc@nvidia.com>
>>>> Sent: Wednesday, September 30, 2026 5:52 AM
>>>>
>>>> iommu_replace_group_handle() looks like it hides the detaching handle ahead
>>>> of the driver callback. But in fact, xa_reserve() calls xa_cmpxchg(), which
>>>> stores only when the current entry matches @old=NULL. For a replace, there
>>>> must be an entry sitting there by definition (i.e. @old cannot be NULL), so
>>>> the compare fails, XA_ZERO_ENTRY is never written, and the call decays into
>>>> a plain read.
>>>>
>>>> Therefore, the old handle stays published during
>>>> __iommu_group_set_domain()
>>>> and only gets replaced after the call. This creates a window, during which
>>>> an asynchronous fault path might hit UAF:
>>>>
>>>>     core, holding group->mutex          fault path, no group->mutex
>>>>     ==========================          ===========================
>>>>
>>>>     xa_reserve()                    ,-- old handle stays published
>>>>       no-op, the slot is occupied   |
>>>>     __iommu_group_set_domain()      |
>>>>       driver attach/detach ops      |
>>>>           iopf_queue_flush_dev()    |
>>>>                                     |   iommu_attach_handle_get()
>>>>                                     |     reads old_handle
>>>>     xa_store(new entry)             `-- window closes
>>>>     mutex_unlock()
>>>>     kfree(old_handle)  [caller]
>>>>                                     UAF: old_handle->domain->iopf_handler()
>>>>
>>>> Replace the xa_reserve() with xa_store(XA_ZERO_ENTRY) to unpublish the old
>>>> handle.
>>>>
>>>> Fixes: 8519e689834a ("iommu: Extend domain attach group with handle
>>>> support")
>>>> Cc: stable@vger.kernel.org
>>>> Assisted-by: LLM
>>>> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
>>>
>>> Reviewed-by: Kevin Tian <kevin.tian@intel.com>
>>>
>>> btw I just realized that neither old code nor this fix can actually support
>>> replacement with lossless fault handling, but I don't recall whether it
>>> was the original goal (then destroyed by a following change) or intended
>>> to be a future enhancement...
>>
>> There was a related discussion[1]. With this patch, a page request in
>> the replace window gets IOMMU_PAGE_RESP_INVALID instead of being
>> serviced - spec only says re-issuing the same PRG won't succeed unless
>> the mapping changes, it doesn't forbid retry. So I think this is just a
>> more conservative instance of the failure case Jason already called OK
>> in [1], not a new hazard.
>>
>> [1]
>> https://lore.kernel.org/linux-iommu/20241105151042.GC458827@nvidia.com/
> 
> I called using either the old or new translation OK, using neither
> seems suboptimal. So returning IOMMU_PAGE_RESP_INVALID doesn't sound
> ideal..

I see. maybe we can have a interim state for the replace window. This
state responds PageRequests with success for the device. The device
shall retry DMA and re-trigger PageRequests again. Once the replacement
is done, PageRequests should be forwarded to the correct domain and get
resolved.

Regards,
Yi Liu

  parent reply	other threads:[~2026-10-30  8:13 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-29 21:51 [PATCH v1 0/4] iommu: Fix attach handle publication ordering Nicolin Chen
2026-09-29 21:51 ` [PATCH v1 1/4] iommu: Unpublish the attach handle before the group detach callback Nicolin Chen
2026-10-29  7:18   ` Tian, Kevin
2026-09-29 21:51 ` [PATCH v1 2/4] iommu: Unpublish the attach handle before the PASID " Nicolin Chen
2026-10-29  7:18   ` Tian, Kevin
2026-09-29 21:51 ` [PATCH v1 3/4] iommu: Unpublish the old attach handle before the group replace callback Nicolin Chen
2026-10-29  7:25   ` Tian, Kevin
2026-10-29 11:00     ` Yi Liu
2026-10-29 18:45       ` Jason Gunthorpe
2026-10-30  8:03         ` Tian, Kevin
2026-10-30  8:22         ` Yi Liu [this message]
2026-10-30 15:21           ` Jason Gunthorpe
2026-09-29 21:51 ` [PATCH v1 4/4] iommu: Unpublish the old attach handle before the PASID " Nicolin Chen
2026-10-29  7:26   ` Tian, Kevin
2026-10-28 18:58 ` [PATCH v1 0/4] iommu: Fix attach handle publication ordering Nicolin Chen
2026-10-29 11:00 ` Yi Liu

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=943304cd-112d-4bd8-ad3a-f70359f15aca@intel.com \
    --to=yi.l.liu@intel.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=jpb@kernel.org \
    --cc=kevin.tian@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nicolinc@nvidia.com \
    --cc=robin.murphy@arm.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

all inboxes | Powered by JetHome®