Skip to contentSkip to conversation

x86/mm: fix potential race conditions in map_pages_to_xen().

PATCH In review Yu Zhang 3 participants 6 messages Nov 14, 2017

lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

PATCH v3 1/2 x86/mm: fix potential race conditions in map_pages_to_xen().

From: Min He <[email protected]>

In map_pages_to_xen(), a L2 page table entry may be reset to point to a superpage, and its corresponding L1 page table need be freed in such scenario, when these L1 page table entries are mapping to consecutive page frames and having the same mapping flags.

However, variable pl1e is not protected by the lock before L1 page table is enumerated. A race condition may happen if this code path is invoked simultaneously on different CPUs.

For example, pl1e value on CPU0 may hold an obsolete value, pointing to a page which has just been freed on CPU1. Besides, before this page is reused, it will still be holding the old PTEs, referencing consecutive page frames. Consequently the free_xen_pagetable(l2e_to_l1e(ol2e)) will be triggered on CPU0, resulting the unexpected free of a normal page.

This patch fixes the above problem by protecting the pl1e with the lock.

Also, there're other potential race conditions. For instance, the L2/L3 entry may be modified concurrently on different CPUs, by routines such as map_pages_to_xen(), modify_xen_mappings() etc. To fix this, this patch will check the _PAGE_PRESENT and _PAGE_PSE flags, after the spinlock is obtained, for the corresponding L2/L3 entry.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

Changes in v3:
According to comments from Jan Beulich:
- use local variable instead of dereference pointer to pte to check flag.
- also check the _PAGE_PRESENT for L2E/L3E.
Others:
- Commit message changes.

Changes in v2:
According to comments from Jan Beulich:
- check PSE of pl2e and pl3e, and skip the re-consolidation if set.
- commit message changes, e.g. add "From :" tag etc.
- code style changes.
- introduce a seperate patch to resolve the similar issue in modify_xen_mappings().

---
 xen/arch/x86/mm.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)
xen/arch/x86/mm.c +34 -2
index a20fdca..1697be9 100644
@@ -4844,9 +4844,29 @@ int map_pages_to_xen(
             {
                 unsigned long base_mfn;
 
-                pl1e = l2e_to_l1e(*pl2e);
                 if ( locking )
                     spin_lock(&map_pgdir_lock);
+
+                ol2e = *pl2e;
+                /*
+                 * L2E may be already cleared, or set to a superpage, by
+                 * concurrent paging structure modifications on other CPUs.
+                 */
+                if ( !(l2e_get_flags(ol2e) & _PAGE_PRESENT) )
+                {
+                    if ( locking )
+                        spin_unlock(&map_pgdir_lock);
+                    continue;
+                }
+
+                if ( l2e_get_flags(ol2e) & _PAGE_PSE )
+                {
+                    if ( locking )
+                        spin_unlock(&map_pgdir_lock);
+                    goto check_l3;
+                }
+
+                pl1e = l2e_to_l1e(ol2e);
                 base_mfn = l1e_get_pfn(*pl1e) & ~(L1_PAGETABLE_ENTRIES - 1);
                 for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++, pl1e++ )
                     if ( (l1e_get_pfn(*pl1e) != (base_mfn + i)) ||
@@ -4854,7 +4874,6 @@ int map_pages_to_xen(
                         break;
                 if ( i == L1_PAGETABLE_ENTRIES )
                 {
-                    ol2e = *pl2e;
                     l2e_write_atomic(pl2e, l2e_from_pfn(base_mfn,
                                                         l1f_to_lNf(flags)));
                     if ( locking )
@@ -4880,7 +4899,20 @@ int map_pages_to_xen(
 
             if ( locking )
                 spin_lock(&map_pgdir_lock);
+
             ol3e = *pl3e;
+            /*
+             * L3E may be already cleared, or set to a superpage, by
+             * concurrent paging structure modifications on other CPUs.
+             */
+            if ( !(l3e_get_flags(ol3e) & _PAGE_PRESENT) ||
+                (l3e_get_flags(ol3e) & _PAGE_PSE) )
+            {
+                if ( locking )
+                    spin_unlock(&map_pgdir_lock);
+                continue;
+            }
+
             pl2e = l3e_to_l2e(ol3e);
             base_mfn = l2e_get_pfn(*pl2e) & ~(L2_PAGETABLE_ENTRIES *
                                               L1_PAGETABLE_ENTRIES - 1);
Signature · 7 lines
2.5.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#1 lore raw

PATCH v3 2/2 x86/mm: fix a potential race condition in modify_xen_mappings().

In modify_xen_mappings(), a L1/L2 page table shall be freed, if all entries of this page table are empty. Corresponding L2/L3 PTE will need be cleared in such scenario.

However, concurrent paging structure modifications on different CPUs may cause the L2/L3 PTEs to be already be cleared or set to reference a superpage.

Therefore the logic to enumerate the L1/L2 page table and to reset the corresponding L2/L3 PTE need to be protected with spinlock. And the _PAGE_PRESENT and _PAGE_PSE flags need be checked after the lock is obtained.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

Changes in v3:
According to comments from Jan Beulich:
- indent the label by one space;
- also check the _PAGE_PSE for L2E/L3E.
Others:
- commit message changes.

---
 xen/arch/x86/mm.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
xen/arch/x86/mm.c +45 -0
index 1697be9..64ccd70 100644
@@ -5111,6 +5111,27 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
              */
             if ( (nf & _PAGE_PRESENT) || ((v != e) && (l1_table_offset(v) != 0)) )
                 continue;
+            if ( locking )
+                spin_lock(&map_pgdir_lock);
+
+            /*
+             * L2E may be already cleared, or set to a superpage, by
+             * concurrent paging structure modifications on other CPUs.
+             */
+            if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )
+            {
+                if ( locking )
+                    spin_unlock(&map_pgdir_lock);
+                goto check_l3;
+            }
+
+            if ( l2e_get_flags(*pl2e) & _PAGE_PSE )
+            {
+                if ( locking )
+                    spin_unlock(&map_pgdir_lock);
+                continue;
+            }
+
             pl1e = l2e_to_l1e(*pl2e);
             for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
                 if ( l1e_get_intpte(pl1e[i]) != 0 )
@@ -5119,11 +5140,16 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
             {
                 /* Empty: zap the L2E and free the L1 page. */
                 l2e_write_atomic(pl2e, l2e_empty());
+                if ( locking )
+                    spin_unlock(&map_pgdir_lock);
                 flush_area(NULL, FLUSH_TLB_GLOBAL); /* flush before free */
                 free_xen_pagetable(pl1e);
             }
+            else if ( locking )
+                spin_unlock(&map_pgdir_lock);
         }
 
+ check_l3:
         /*
          * If we are not destroying mappings, or not done with the L3E,
          * skip the empty&free check.
@@ -5131,6 +5157,21 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
         if ( (nf & _PAGE_PRESENT) ||
              ((v != e) && (l2_table_offset(v) + l1_table_offset(v) != 0)) )
             continue;
+        if ( locking )
+            spin_lock(&map_pgdir_lock);
+
+        /*
+         * L3E may be already cleared, or set to a superpage, by
+         * concurrent paging structure modifications on other CPUs.
+         */
+        if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ||
+              (l3e_get_flags(*pl3e) & _PAGE_PSE) )
+        {
+            if ( locking )
+                spin_unlock(&map_pgdir_lock);
+            continue;
+        }
+
         pl2e = l3e_to_l2e(*pl3e);
         for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ )
             if ( l2e_get_intpte(pl2e[i]) != 0 )
@@ -5139,9 +5180,13 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
         {
             /* Empty: zap the L3E and free the L2 page. */
             l3e_write_atomic(pl3e, l3e_empty());
+            if ( locking )
+                spin_unlock(&map_pgdir_lock);
             flush_area(NULL, FLUSH_TLB_GLOBAL); /* flush before free */
             free_xen_pagetable(pl2e);
         }
+        else if ( locking )
+            spin_unlock(&map_pgdir_lock);
     }
 
     flush_area(NULL, FLUSH_TLB_GLOBAL);
Signature · 7 lines
2.5.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v3 2/2
#2 lore raw

Re: [PATCH v3 for-4.10 2/2] x86/mm: fix a potential race condition in modify_xen_mappings().

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 14.11.17 at 07:53, <[email protected]> wrote:

In modify_xen_mappings(), a L1/L2 page table shall be freed, if all entries of this page table are empty. Corresponding L2/L3 PTE will need be cleared in such scenario.

However, concurrent paging structure modifications on different CPUs may cause the L2/L3 PTEs to be already be cleared or set to reference a superpage.

Quoted 6 lines

Therefore the logic to enumerate the L1/L2 page table and to reset the corresponding L2/L3 PTE need to be protected with spinlock. And the _PAGE_PRESENT and _PAGE_PSE flags need be checked after the lock is obtained.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v3 1/2
#3 lore raw
In reply to Yu Zhang
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 14.11.17 at 07:53, <[email protected]> wrote:

From: Min He <[email protected]>

In map_pages_to_xen(), a L2 page table entry may be reset to point to a superpage, and its corresponding L1 page table need be freed in such scenario, when these L1 page table entries are mapping to consecutive page frames and having the same mapping flags.

However, variable pl1e is not protected by the lock before L1 page table is enumerated. A race condition may happen if this code path is invoked simultaneously on different CPUs.

For example, pl1e value on CPU0 may hold an obsolete value, pointing to a page which has just been freed on CPU1. Besides, before this page is reused, it will still be holding the old PTEs, referencing consecutive page frames. Consequently the free_xen_pagetable(l2e_to_l1e(ol2e)) will be triggered on CPU0, resulting the unexpected free of a normal page.

This patch fixes the above problem by protecting the pl1e with the lock.

Also, there're other potential race conditions. For instance, the L2/L3 entry may be modified concurrently on different CPUs, by routines such as map_pages_to_xen(), modify_xen_mappings() etc. To fix this, this patch will check the _PAGE_PRESENT and _PAGE_PSE flags, after the spinlock is obtained, for the corresponding L2/L3 entry.

Quoted 3 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw

Hi,

Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 14.11.17 at 07:53, <[email protected]> wrote:

From: Min He <[email protected]>

In map_pages_to_xen(), a L2 page table entry may be reset to point to a superpage, and its corresponding L1 page table need be freed in such scenario, when these L1 page table entries are mapping to consecutive page frames and having the same mapping flags.

However, variable pl1e is not protected by the lock before L1 page table is enumerated. A race condition may happen if this code path is invoked simultaneously on different CPUs.

For example, pl1e value on CPU0 may hold an obsolete value, pointing to a page which has just been freed on CPU1. Besides, before this page is reused, it will still be holding the old PTEs, referencing consecutive page frames. Consequently the free_xen_pagetable(l2e_to_l1e(ol2e)) will be triggered on CPU0, resulting the unexpected free of a normal page.

This patch fixes the above problem by protecting the pl1e with the lock.

Also, there're other potential race conditions. For instance, the L2/L3 entry may be modified concurrently on different CPUs, by routines such as map_pages_to_xen(), modify_xen_mappings() etc. To fix this, this patch will check the _PAGE_PRESENT and _PAGE_PSE flags, after the spinlock is obtained, for the corresponding L2/L3 entry.

Quoted 5 lines
Quoted 3 lines

Please try to have a cover letter in the future when you have multiple patches. This will make easier to give comments/release-ack for the all the patches. Anyway for the 2 patches:

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#5 lore raw
Quoted 29 lines · Julien Grall

Hi,

Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 14.11.17 at 07:53, <[email protected]> wrote:

From: Min He <[email protected]>

In map_pages_to_xen(), a L2 page table entry may be reset to point to a superpage, and its corresponding L1 page table need be freed in such scenario, when these L1 page table entries are mapping to consecutive page frames and having the same mapping flags.

However, variable pl1e is not protected by the lock before L1 page table is enumerated. A race condition may happen if this code path is invoked simultaneously on different CPUs.

For example, pl1e value on CPU0 may hold an obsolete value, pointing to a page which has just been freed on CPU1. Besides, before this page is reused, it will still be holding the old PTEs, referencing consecutive page frames. Consequently the free_xen_pagetable(l2e_to_l1e(ol2e)) will be triggered on CPU0, resulting the unexpected free of a normal page.

This patch fixes the above problem by protecting the pl1e with the lock.

Also, there're other potential race conditions. For instance, the L2/L3 entry may be modified concurrently on different CPUs, by routines such as map_pages_to_xen(), modify_xen_mappings() etc. To fix this, this patch will check the _PAGE_PRESENT and _PAGE_PSE flags, after the spinlock is obtained, for the corresponding L2/L3 entry.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Please try to have a cover letter in the future when you have multiple patches. This will make easier to give comments/release-ack for the all the patches. Anyway for the 2 patches:

Oh, got it. Thanks for the suggestion. :-)

Yu

Quoted 5 lines

Cheers,

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel