[PATCH] x86/mm: special-case IOMMU (un)mapping failure path in _get_page_type()

Jan Beulich posted 1 patch 3 weeks, 6 days ago
[PATCH] x86/mm: special-case IOMMU (un)mapping failure path in _get_page_type()
Posted by Jan Beulich 3 weeks, 6 days ago
The error path put in place to address XSA-291 was broken by the re-work
done for XSA-299: With PTF_partial_set not passed in (since PGT_partial is
clear for a page undergoing a type change) and the page not having
PGT_validated set just yet, _put_page_type() would fail its consistency
check for non-dying domains. Introduce yet another flag to indicate use of
the function from _get_page_type()'s recovery path.

Fixes: c40b33d72630 ("x86/mm: Don't drop a type ref unless you held a ref to begin with")
Reported-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Roger Pau Monné <roger@xenproject.org>

--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -1088,6 +1088,7 @@ get_page_from_l1e(
  * more information.
  */
 #define PTF_partial_set           (1 << 0)
+#define PTF_recovery              (1 << 1)
 #define PTF_preemptible           (1 << 2)
 #define PTF_defer                 (1 << 3)
 #define PTF_retain_ref_on_restart (1 << 4)
@@ -2812,15 +2813,21 @@ static int _put_page_type(struct page_in
          * *not* fully validated, continuing would almost certainly
          * open up a security hole.  An exception to this is during
          * domain destruction, where PGT_validated can be dropped
-         * without dropping a type ref.
+         * without dropping a type ref.  Another exception is on error
+         * paths from _get_page_type(), where the type ref was already
+         * put in place, but PGT_validated isn't set yet (page
+         * validation still pending).
          *
-         * If the latter, do nothing unless type PGT_partial is set.
-         * If it is set, the type count must be 1.
+         * In the domain destruction case, do nothing unless PGT_partial
+         * is set.  If it is set, the type count must be 1.
+         *
+         * In the _get_page_type() error path case operate normally,
+         * again also including the type count sanity check.
          */
-        if ( !(flags & PTF_partial_set) )
+        if ( !(flags & (PTF_partial_set | PTF_recovery)) )
             BUG_ON((x & PGT_partial) ||
                    !((x & PGT_validated) || page_get_owner(page)->is_dying));
-        else if ( !(x & PGT_partial) )
+        else if ( (flags & PTF_partial_set) && !(x & PGT_partial) )
             return 0;
         else
             BUG_ON((x & PGT_count_mask) != 1);
@@ -3097,7 +3104,7 @@ static int _get_page_type(struct page_in
 
             if ( unlikely(rc) )
             {
-                _put_page_type(page, 0, NULL);
+                _put_page_type(page, PTF_recovery, NULL);
                 goto out;
             }
         }