Skip to contentSkip to conversation

x86/HVM: Properly handle SMAP check in certain cases

PATCH In review Feng Wu 3 participants 16 messages Jan 8, 2001 – Jul 29, 2014

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

v3 cover x86/HVM: Properly handle SMAP check in certain cases

This patch set fixs a issue found by Sander Eikelenboom. Here is the log when this issue occurs:

(d2) Booting from Hard Disk...
(d2) Booting from 0000:7c00
(XEN) irq.c:380: Dom1 callback via changed to Direct Vector 0xf3
(XEN) irq.c:380: Dom2 callback via changed to Direct Vector 0xf3
(XEN) Segment register inaccessible for d1v0
(XEN) (If you see this outside of debugging activity, please report to [email protected])

And here is the Xen call trace:
(XEN) [<ffff82d0801dc9c5>] vmx_get_segment_register+0x4d/0x422 (XEN) [<ffff82d0801f4415>] guest_walk_tables_3_levels+0x189/0x520 (XEN) [<ffff82d0802204a8>] hap_p2m_ga_to_gfn_3_levels+0x158/0x2c2 (XEN) [<ffff82d08022062e>] hap_gva_to_gfn_3_levels+0x1c/0x1e (XEN) [<ffff82d0801ec215>] paging_gva_to_gfn+0xb8/0xce (XEN) [<ffff82d0801ba88d>] __hvm_copy+0x87/0x354
(XEN) [<ffff82d0801bac7c>] hvm_copy_to_guest_virt_nofault+0x1e/0x20 (XEN) [<ffff82d0801bace5>] copy_to_user_hvm+0x67/0x87 (XEN) [<ffff82d08016237c>] update_runstate_area+0x98/0xfb (XEN) [<ffff82d0801623f0>] _update_runstate_area+0x11/0x39 (XEN) [<ffff82d0801634db>] context_switch+0x10c3/0x10fa (XEN) [<ffff82d080126a19>] schedule+0x5a8/0x5da
(XEN) [<ffff82d0801297f9>] __do_softirq+0x81/0x8c
(XEN) [<ffff82d080129852>] do_softirq+0x13/0x15
(XEN) [<ffff82d08015f70a>] idle_loop+0x67/0x77

We need get guest's SS register via hvm_get_segment_register() to do the SMAP checking, however, in these two cases, we cannot do it that way since it is between setting 'current' and reloading the VMCS context for it. As an alternative, here we treat these accesses as implicit supervisor mode access, hence SMAP checking is always need.

V2:
Remove ' VCPUOP_enable_smap_check_vcpu_time_memory_area' hypercall, hence always do the SMAP checking for the secondary system time.

V3:
- Add smap_policy_change() to change the smap policy, which will returen the old value.
- Use enum to define the smap policy.
- Drop 'Case SMAP_CHECK_DISABLED' in guest_walk_tables(), and add 'ASSERT(v->arch.smap_check_policy == SMAP_CHECK_DISABLED)' in the default case instead.

Feng Wu (2):
x86/hvm: Always do SMAP check when updating runstate_guest(v) x86/hvm: Always do SMAP check when updating secondary system time for
guest

 xen/arch/x86/domain.c        | 26 ++++++++++++++++++++++----
 xen/arch/x86/mm/guest_walk.c | 39 ++++++++++++++++++++++++++-------------
 xen/arch/x86/time.c          | 10 +++++++++-
 xen/include/asm-x86/domain.h | 19 +++++++++++++++++--
 4 files changed, 74 insertions(+), 20 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH v3 1/2 x86/hvm: Always do SMAP check when updating runstate_guest(v)

In the current implementation, we honor the guest's CPL and AC to determain whether do the SMAP check or not for runstate_guest(v). However, this doesn't work. The VMCS feild is invalid when we try to get geust's SS by hvm_get_segment_register(), since the right VMCS has not beed loaded for the current VCPU.

In this patch, we always do the SMAP check when updating runstate_guest(v) for the guest when SMAP is enabled by it.

---
 xen/arch/x86/domain.c        | 26 ++++++++++++++++++++++----
 xen/arch/x86/mm/guest_walk.c | 39 ++++++++++++++++++++++++++-------------
 xen/include/asm-x86/domain.h | 17 ++++++++++++++++-
 3 files changed, 64 insertions(+), 18 deletions(-)
xen/arch/x86/domain.c +22 -4
index e896210..cfb165c 100644
@@ -180,6 +180,14 @@ void dump_pageframe_info(struct domain *d)
     spin_unlock(&d->page_alloc_lock);
 }
 
+smap_check_policy_t smap_policy_change(struct vcpu *v,
+    smap_check_policy_t new_policy)
+{
+    smap_check_policy_t old_policy = v->arch.smap_check_policy;
+    v->arch.smap_check_policy = new_policy;
+    return old_policy;
+}
+
 /*
  * The hole may be at or above the 44-bit boundary, so we need to determine
  * the total bit count until reaching 32 significant (not squashed out) bits
@@ -1349,22 +1357,32 @@ static void paravirt_ctxt_switch_to(struct vcpu *v)
 }
 
 /* Update per-VCPU guest runstate shared memory area (if registered). */
-bool_t update_runstate_area(const struct vcpu *v)
+bool_t update_runstate_area(struct vcpu *v)
 {
+    bool_t rc;
+    smap_check_policy_t saved_policy;
+
     if ( guest_handle_is_null(runstate_guest(v)) )
         return 1;
 
+    saved_policy = smap_policy_change(v, SMAP_CHECK_ENABLED);
+
     if ( has_32bit_shinfo(v->domain) )
     {
         struct compat_vcpu_runstate_info info;
 
         XLAT_vcpu_runstate_info(&info, &v->runstate);
         __copy_to_guest(v->runstate_guest.compat, &info, 1);
+        smap_policy_change(v, saved_policy);
         return 1;
     }
-
-    return __copy_to_guest(runstate_guest(v), &v->runstate, 1) !=
-           sizeof(v->runstate);
+    else
+    {
+        rc = __copy_to_guest(runstate_guest(v), &v->runstate, 1) !=
+             sizeof(v->runstate);
+        smap_policy_change(v, saved_policy);
+        return rc;
+    }
 }
 
 static void _update_runstate_area(struct vcpu *v)
xen/arch/x86/mm/guest_walk.c +26 -13
index bb38fda..20b9cbc 100644
@@ -164,25 +164,38 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
         struct segment_register seg;
         const struct cpu_user_regs *regs = guest_cpu_user_regs();
 
-        hvm_get_segment_register(v, x86_seg_ss, &seg);
-
         /* SMEP: kernel-mode instruction fetches from user-mode mappings
          * should fault.  Unlike NX or invalid bits, we're looking for _all_
          * entries in the walk to have _PAGE_USER set, so we need to do the
          * whole walk as if it were a user-mode one and then invert the answer. */
         smep =  hvm_smep_enabled(v) && (pfec & PFEC_insn_fetch);
 
-        /*
-         * SMAP: kernel-mode data accesses from user-mode mappings should fault
-         * A fault is considered as a SMAP violation if the following
-         * conditions come true:
-         *   - X86_CR4_SMAP is set in CR4
-         *   - A user page is accessed
-         *   - CPL = 3 or X86_EFLAGS_AC is clear
-         *   - Page fault in kernel mode
-         */
-        smap = hvm_smap_enabled(v) &&
-               ((seg.attr.fields.dpl == 3) || !(regs->eflags & X86_EFLAGS_AC));
+        switch ( v->arch.smap_check_policy )
+        {
+        case SMAP_CHECK_HONOR_CPL_AC:
+            hvm_get_segment_register(v, x86_seg_ss, &seg);
+
+            /*
+             * SMAP: kernel-mode data accesses from user-mode mappings
+             * should fault.
+             * A fault is considered as a SMAP violation if the following
+             * conditions come true:
+             *   - X86_CR4_SMAP is set in CR4
+             *   - A user page is accessed
+             *   - CPL = 3 or X86_EFLAGS_AC is clear
+             *   - Page fault in kernel mode
+             */
+            smap = hvm_smap_enabled(v) &&
+                   ((seg.attr.fields.dpl == 3) ||
+                   !(regs->eflags & X86_EFLAGS_AC));
+            break;
+        case SMAP_CHECK_ENABLED:
+            smap = hvm_smap_enabled(v);
+            break;
+        default:
+            ASSERT(v->arch.smap_check_policy == SMAP_CHECK_DISABLED);
+            break;
+        }
     }
 
     if ( smep || smap )
xen/include/asm-x86/domain.h +16 -1
index abf55fb..6751ff2 100644
@@ -382,6 +382,12 @@ struct pv_vcpu
     struct vcpu_time_info pending_system_time;
 };
 
+typedef enum {
+    SMAP_CHECK_HONOR_CPL_AC = 0,  /* honor the guest's CPL and AC */
+    SMAP_CHECK_ENABLED,           /* enable the check */
+    SMAP_CHECK_DISABLED,          /* disable the check */
+} smap_check_policy_t;
+
 struct arch_vcpu
 {
     /*
@@ -446,13 +452,22 @@ struct arch_vcpu
 
     /* A secondary copy of the vcpu time info. */
     XEN_GUEST_HANDLE(vcpu_time_info_t) time_info_guest;
+
+    /*
+     * The SMAP check policy when updating runstate_guest(v) and the
+     * secondary system time.
+     */
+    smap_check_policy_t smap_check_policy;
 } __cacheline_aligned;
 
+smap_check_policy_t smap_policy_change(struct vcpu *v,
+                                       smap_check_policy_t new_policy);
+
 /* Shorthands to improve code legibility. */
 #define hvm_vmx         hvm_vcpu.u.vmx
 #define hvm_svm         hvm_vcpu.u.svm
 
-bool_t update_runstate_area(const struct vcpu *);
+bool_t update_runstate_area(struct vcpu *);
 bool_t update_secondary_system_time(const struct vcpu *,
                                     struct vcpu_time_info *);
 
Signature · 1 line
1.8.3.1
author
#12 lore raw

PATCH v3 2/2 x86/hvm: Always do SMAP check when updating secondary system time for guest

In reply to Feng Wu

In this patch, we always do the SMAP check when updating secondary system time for the guest when SMAP is enabled by it.

---
 xen/arch/x86/time.c          | 10 +++++++++-
 xen/include/asm-x86/domain.h |  2 +-
 2 files changed, 10 insertions(+), 2 deletions(-)
xen/arch/x86/time.c +9 -1
index a4e1656..e4627f3 100644
@@ -821,17 +821,23 @@ static void __update_vcpu_system_time(struct vcpu *v, int force)
         v->arch.pv_vcpu.pending_system_time = _u;
 }
 
-bool_t update_secondary_system_time(const struct vcpu *v,
+bool_t update_secondary_system_time(struct vcpu *v,
                                     struct vcpu_time_info *u)
 {
     XEN_GUEST_HANDLE(vcpu_time_info_t) user_u = v->arch.time_info_guest;
+    smap_check_policy_t saved_policy;
 
     if ( guest_handle_is_null(user_u) )
         return 1;
 
+    saved_policy = smap_policy_change(v, SMAP_CHECK_ENABLED);
+
     /* 1. Update userspace version. */
     if ( __copy_field_to_guest(user_u, u, version) == sizeof(u->version) )
+    {
+        smap_policy_change(v, saved_policy);
         return 0;
+    }
     wmb();
     /* 2. Update all other userspace fields. */
     __copy_to_guest(user_u, u, 1);
@@ -840,6 +846,8 @@ bool_t update_secondary_system_time(const struct vcpu *v,
     u->version = version_update_end(u->version);
     __copy_field_to_guest(user_u, u, version);
 
+    smap_policy_change(v, saved_policy);
+
     return 1;
 }
 
xen/include/asm-x86/domain.h +1 -1
index 6751ff2..531d40e 100644
@@ -468,7 +468,7 @@ smap_check_policy_t smap_policy_change(struct vcpu *v,
 #define hvm_svm         hvm_vcpu.u.svm
 
 bool_t update_runstate_area(struct vcpu *);
-bool_t update_secondary_system_time(const struct vcpu *,
+bool_t update_secondary_system_time(struct vcpu *,
                                     struct vcpu_time_info *);
 
 void vcpu_show_execution_state(struct vcpu *);
Signature · 1 line
1.8.3.1
#14 lore raw
In reply to Feng Wu

On Tue, Jan 09, 2001 at 06:52:05AM +0800, Feng Wu wrote:

Hello Feng.. your clock seems to be messed up :) 2001..

-- Pasi

Quoted 65 lines

This patch set fixs a issue found by Sander Eikelenboom. Here is the log when this issue occurs:

(d2) Booting from Hard Disk...
(d2) Booting from 0000:7c00
(XEN) irq.c:380: Dom1 callback via changed to Direct Vector 0xf3
(XEN) irq.c:380: Dom2 callback via changed to Direct Vector 0xf3
(XEN) Segment register inaccessible for d1v0
(XEN) (If you see this outside of debugging activity, please report to [email protected])

And here is the Xen call trace:
(XEN) [<ffff82d0801dc9c5>] vmx_get_segment_register+0x4d/0x422 (XEN) [<ffff82d0801f4415>] guest_walk_tables_3_levels+0x189/0x520 (XEN) [<ffff82d0802204a8>] hap_p2m_ga_to_gfn_3_levels+0x158/0x2c2 (XEN) [<ffff82d08022062e>] hap_gva_to_gfn_3_levels+0x1c/0x1e (XEN) [<ffff82d0801ec215>] paging_gva_to_gfn+0xb8/0xce (XEN) [<ffff82d0801ba88d>] __hvm_copy+0x87/0x354
(XEN) [<ffff82d0801bac7c>] hvm_copy_to_guest_virt_nofault+0x1e/0x20 (XEN) [<ffff82d0801bace5>] copy_to_user_hvm+0x67/0x87 (XEN) [<ffff82d08016237c>] update_runstate_area+0x98/0xfb (XEN) [<ffff82d0801623f0>] _update_runstate_area+0x11/0x39 (XEN) [<ffff82d0801634db>] context_switch+0x10c3/0x10fa (XEN) [<ffff82d080126a19>] schedule+0x5a8/0x5da
(XEN) [<ffff82d0801297f9>] __do_softirq+0x81/0x8c
(XEN) [<ffff82d080129852>] do_softirq+0x13/0x15
(XEN) [<ffff82d08015f70a>] idle_loop+0x67/0x77

We need get guest's SS register via hvm_get_segment_register() to do the SMAP checking, however, in these two cases, we cannot do it that way since it is between setting 'current' and reloading the VMCS context for it. As an alternative, here we treat these accesses as implicit supervisor mode access, hence SMAP checking is always need.

V2:
Remove ' VCPUOP_enable_smap_check_vcpu_time_memory_area' hypercall, hence always do the SMAP checking for the secondary system time.

V3:
- Add smap_policy_change() to change the smap policy, which will returen the old value.
- Use enum to define the smap policy.
- Drop 'Case SMAP_CHECK_DISABLED' in guest_walk_tables(), and add 'ASSERT(v->arch.smap_check_policy == SMAP_CHECK_DISABLED)' in the default case instead.

Feng Wu (2):
x86/hvm: Always do SMAP check when updating runstate_guest(v) x86/hvm: Always do SMAP check when updating secondary system time for
guest

 xen/arch/x86/domain.c        | 26 ++++++++++++++++++++++----
 xen/arch/x86/mm/guest_walk.c | 39 ++++++++++++++++++++++++++-------------
 xen/arch/x86/time.c          | 10 +++++++++-
 xen/include/asm-x86/domain.h | 19 +++++++++++++++++--
 4 files changed, 74 insertions(+), 20 deletions(-)
Signature · 7 lines
1.8.3.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
author
#15 lore raw
Quoted 11 lines

-----Original Message-----
From: Pasi Kärkkäinen [mailto:[email protected]]
Sent: Tuesday, July 29, 2014 3:03 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [Xen-devel] [PATCH v3 0/2] x86/HVM: Properly handle SMAP check in certain cases

Quoted 4 lines

On Tue, Jan 09, 2001 at 06:52:05AM +0800, Feng Wu wrote:

Hello Feng.. your clock seems to be messed up :) 2001..

Hmmm, thanks for notifying this. since this is a new HW platform, every time I reboot it, its clock will go back to 2001. Sorry I forget to set it to the correct time.

Thanks,
Feng

Quoted 72 lines

-- Pasi

Quoted 9 lines

This patch set fixs a issue found by Sander Eikelenboom. Here is the log when this issue occurs:

(d2) Booting from Hard Disk...
(d2) Booting from 0000:7c00
(XEN) irq.c:380: Dom1 callback via changed to Direct Vector 0xf3 (XEN) irq.c:380: Dom2 callback via changed to Direct Vector 0xf3 (XEN) Segment register inaccessible for d1v0
(XEN) (If you see this outside of debugging activity, please report to

[email protected])

Quoted 37 lines

And here is the Xen call trace:
(XEN) [<ffff82d0801dc9c5>] vmx_get_segment_register+0x4d/0x422 (XEN) [<ffff82d0801f4415>] guest_walk_tables_3_levels+0x189/0x520 (XEN) [<ffff82d0802204a8>] hap_p2m_ga_to_gfn_3_levels+0x158/0x2c2 (XEN) [<ffff82d08022062e>] hap_gva_to_gfn_3_levels+0x1c/0x1e (XEN) [<ffff82d0801ec215>] paging_gva_to_gfn+0xb8/0xce (XEN) [<ffff82d0801ba88d>] __hvm_copy+0x87/0x354
(XEN) [<ffff82d0801bac7c>] hvm_copy_to_guest_virt_nofault+0x1e/0x20 (XEN) [<ffff82d0801bace5>] copy_to_user_hvm+0x67/0x87 (XEN) [<ffff82d08016237c>] update_runstate_area+0x98/0xfb (XEN) [<ffff82d0801623f0>] _update_runstate_area+0x11/0x39 (XEN) [<ffff82d0801634db>] context_switch+0x10c3/0x10fa (XEN) [<ffff82d080126a19>] schedule+0x5a8/0x5da
(XEN) [<ffff82d0801297f9>] __do_softirq+0x81/0x8c
(XEN) [<ffff82d080129852>] do_softirq+0x13/0x15
(XEN) [<ffff82d08015f70a>] idle_loop+0x67/0x77

We need get guest's SS register via hvm_get_segment_register() to do the SMAP checking, however, in these two cases, we cannot do it that way since it is between setting 'current' and reloading the VMCS context for it. As an alternative, here we treat these accesses as implicit supervisor mode access, hence SMAP checking is always need.

V2:
Remove ' VCPUOP_enable_smap_check_vcpu_time_memory_area' hypercall, hence always do the SMAP checking for the secondary system time.

V3:
- Add smap_policy_change() to change the smap policy, which will returen the old value.
- Use enum to define the smap policy.
- Drop 'Case SMAP_CHECK_DISABLED' in guest_walk_tables(), and add 'ASSERT(v->arch.smap_check_policy == SMAP_CHECK_DISABLED)' in the default case instead.

Quoted 3 lines

Feng Wu (2):
x86/hvm: Always do SMAP check when updating runstate_guest(v) x86/hvm: Always do SMAP check when updating secondary system time

for

Quoted 4 lines

guest

 xen/arch/x86/domain.c        | 26 ++++++++++++++++++++++----
 xen/arch/x86/mm/guest_walk.c | 39

++++++++++++++++++++++++++-------------

Quoted 12 lines
 xen/arch/x86/time.c          | 10 +++++++++-
 xen/include/asm-x86/domain.h | 19 +++++++++++++++++--
 4 files changed, 74 insertions(+), 20 deletions(-)

--
1.8.3.1

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

#2 lore raw

Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

In reply to Feng Wu
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 08.01.01 at 23:52, <[email protected]> wrote:
xen/arch/x86/domain.c +6 -1
-bool_t update_runstate_area(const struct vcpu *v)
+bool_t update_runstate_area(struct vcpu *v)
 {
+    bool_t rc;
+    smap_check_policy_t saved_policy;
+
     if ( guest_handle_is_null(runstate_guest(v)) )
         return 1;
 
+    saved_policy = smap_policy_change(v, SMAP_CHECK_ENABLED);
+
     if ( has_32bit_shinfo(v->domain) )
     {
         struct compat_vcpu_runstate_info info;

xen/arch/x86/domain.c static void paravirt_ctxt_switch_to(struct vcpu *v) · line 1379

Quoted 10 lines
xen/arch/x86/domain.c +2 -3
 
         XLAT_vcpu_runstate_info(&info, &v->runstate);
         __copy_to_guest(v->runstate_guest.compat, &info, 1);
+        smap_policy_change(v, saved_policy);
         return 1;
     }
-
-    return __copy_to_guest(runstate_guest(v), &v->runstate, 1) !=
-           sizeof(v->runstate);
+    else

Pointless else. Remember, in the previous version you used "goto", which I asked you to avoid by using "else". Since you opted for retaining the "return" above in this version, the need for the "else" went away. The alternative would now be to convert the "return" above to "rc = 1" and move the return below out of the "else" scope.

Quoted 8 lines
xen/arch/x86/mm/guest_walk.c +8 -0
+        switch ( v->arch.smap_check_policy )
+        {
+        case SMAP_CHECK_HONOR_CPL_AC:
+            hvm_get_segment_register(v, x86_seg_ss, &seg);
+
+            /*
+             * SMAP: kernel-mode data accesses from user-mode mappings
+             * should fault.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 190

Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +10 -0
+             * A fault is considered as a SMAP violation if the following
+             * conditions come true:
+             *   - X86_CR4_SMAP is set in CR4
+             *   - A user page is accessed
+             *   - CPL = 3 or X86_EFLAGS_AC is clear
+             *   - Page fault in kernel mode
+             */
+            smap = hvm_smap_enabled(v) &&
+                   ((seg.attr.fields.dpl == 3) ||
+                   !(regs->eflags & X86_EFLAGS_AC));

Indentation.

xen/include/asm-x86/domain.h struct pv_vcpu · line 386

Quoted 2 lines
xen/include/asm-x86/domain.h +2 -0
+typedef enum {
+    SMAP_CHECK_HONOR_CPL_AC = 0,  /* honor the guest's CPL and AC */

Pointless " = 0".

xen/include/asm-x86/domain.h struct pv_vcpu · line 389

Quoted 3 lines
xen/include/asm-x86/domain.h +3 -0
+    SMAP_CHECK_ENABLED,           /* enable the check */
+    SMAP_CHECK_DISABLED,          /* disable the check */
+} smap_check_policy_t;

Perhaps worth adding the __packed__ attribute here to get this back down to a byte ...

xen/include/asm-x86/domain.h struct arch_vcpu · line 461

Quoted 11 lines
xen/include/asm-x86/domain.h +6 -0
@@ -446,13 +452,22 @@ struct arch_vcpu
 
     /* A secondary copy of the vcpu time info. */
     XEN_GUEST_HANDLE(vcpu_time_info_t) time_info_guest;
+
+    /*
+     * The SMAP check policy when updating runstate_guest(v) and the
+     * secondary system time.
+     */
+    smap_check_policy_t smap_check_policy;
 } __cacheline_aligned;

... and try to find a better place (current padding) in this structure?

Jan

#13 lore raw

Re: [PATCH v3 2/2] x86/hvm: Always do SMAP check when updating secondary system time for guest

In reply to Feng Wu

xen/arch/x86/time.c static void __update_vcpu_system_time(struct vcpu *v, int force) · line 828

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 08.01.01 at 23:52, <[email protected]> wrote:
xen/arch/x86/time.c +2 -1
-bool_t update_secondary_system_time(const struct vcpu *v,
+bool_t update_secondary_system_time(struct vcpu *v,
                                     struct vcpu_time_info *u)
 {
     XEN_GUEST_HANDLE(vcpu_time_info_t) user_u = v->arch.time_info_guest;
+    smap_check_policy_t saved_policy;

Actually, here as much as in patch 1 - "saved_policy" isn't the best name as it doesn't make clear this is the smap policy. "smap_policy" would seem better to me, just in case something else (but similar) turns up in the future.

Jan

author
#3 lore raw

Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

In reply to Jan Beulich
Quoted 27 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 29, 2014 3:19 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 08.01.01 at 23:52, <[email protected]> wrote:
xen/arch/x86/domain.c +6 -1
-bool_t update_runstate_area(const struct vcpu *v)
+bool_t update_runstate_area(struct vcpu *v)
 {
+    bool_t rc;
+    smap_check_policy_t saved_policy;
+
     if ( guest_handle_is_null(runstate_guest(v)) )
         return 1;
 
+    saved_policy = smap_policy_change(v, SMAP_CHECK_ENABLED);
+
     if ( has_32bit_shinfo(v->domain) )
     {
         struct compat_vcpu_runstate_info info;
Quoted 15 lines

xen/arch/x86/domain.c static void paravirt_ctxt_switch_to(struct vcpu *v) · line 1379

Quoted 9 lines
         XLAT_vcpu_runstate_info(&info, &v->runstate);
         __copy_to_guest(v->runstate_guest.compat, &info, 1);
xen/arch/x86/domain.c +2 -3
+        smap_policy_change(v, saved_policy);
         return 1;
     }
-
-    return __copy_to_guest(runstate_guest(v), &v->runstate, 1) !=
-           sizeof(v->runstate);
+    else

Pointless else. Remember, in the previous version you used "goto", which I asked you to avoid by using "else". Since you opted for retaining the "return" above in this version, the need for the "else" went away. The alternative would now be to convert the "return" above to "rc = 1" and move the return below out of the "else" scope.

Sorry, It was my negligence. This 'else' is pretty pointless:)

Quoted 21 lines

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,

Quoted 8 lines
xen/arch/x86/mm/guest_walk.c +7 -0
+        switch ( v->arch.smap_check_policy )
+        {
+        case SMAP_CHECK_HONOR_CPL_AC:
+            hvm_get_segment_register(v, x86_seg_ss, &seg);
+
+            /*
+             * SMAP: kernel-mode data accesses from user-mode

mappings

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 190

Quoted 11 lines
xen/arch/x86/mm/guest_walk.c +11 -0
+             * should fault.
+             * A fault is considered as a SMAP violation if the following
+             * conditions come true:
+             *   - X86_CR4_SMAP is set in CR4
+             *   - A user page is accessed
+             *   - CPL = 3 or X86_EFLAGS_AC is clear
+             *   - Page fault in kernel mode
+             */
+            smap = hvm_smap_enabled(v) &&
+                   ((seg.attr.fields.dpl == 3) ||
+                   !(regs->eflags & X86_EFLAGS_AC));
Quoted 1 line

Indentation.

Sorry, I don't find any indentation issue here. Do you mean "((seg.attr.fields.dpl == 3) ||" and "!(regs->eflags & X86_EFLAGS_AC));" are not indented with "hvm_smap_enabled(v) &&"? In fact they are in good indentation. Maybe it is the display that make it look like in wrong indentation?

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+typedef enum {
+    SMAP_CHECK_HONOR_CPL_AC = 0,  /* honor the guest's CPL and AC

*/

Quoted 8 lines

Pointless " = 0".

xen/include/asm-x86/domain.h struct pv_vcpu · line 389

Quoted 3 lines
xen/include/asm-x86/domain.h +3 -0
+    SMAP_CHECK_ENABLED,           /* enable the check */
+    SMAP_CHECK_DISABLED,          /* disable the check */
+} smap_check_policy_t;

Perhaps worth adding the __packed__ attribute here to get this back down to a byte ...

Good idea!

Thanks,
Feng

Quoted 16 lines

xen/include/asm-x86/domain.h struct arch_vcpu · line 461

Quoted 12 lines
xen/include/asm-x86/domain.h +6 -0
@@ -446,13 +452,22 @@ struct arch_vcpu
 
     /* A secondary copy of the vcpu time info. */
     XEN_GUEST_HANDLE(vcpu_time_info_t) time_info_guest;
+
+    /*
+     * The SMAP check policy when updating runstate_guest(v) and the
+     * secondary system time.
+     */
+    smap_check_policy_t smap_check_policy;
 } __cacheline_aligned;

... and try to find a better place (current padding) in this structure?

Jan

author
#9 lore raw

Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

In reply to Jan Beulich
Quoted 80 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 29, 2014 3:19 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 08.01.01 at 23:52, <[email protected]> wrote:
xen/arch/x86/domain.c +6 -1
-bool_t update_runstate_area(const struct vcpu *v)
+bool_t update_runstate_area(struct vcpu *v)
 {
+    bool_t rc;
+    smap_check_policy_t saved_policy;
+
     if ( guest_handle_is_null(runstate_guest(v)) )
         return 1;
 
+    saved_policy = smap_policy_change(v, SMAP_CHECK_ENABLED);
+
     if ( has_32bit_shinfo(v->domain) )
     {
         struct compat_vcpu_runstate_info info;

xen/arch/x86/domain.c static void paravirt_ctxt_switch_to(struct vcpu *v) · line 1379

Quoted 10 lines
xen/arch/x86/domain.c +2 -3
 
         XLAT_vcpu_runstate_info(&info, &v->runstate);
         __copy_to_guest(v->runstate_guest.compat, &info, 1);
+        smap_policy_change(v, saved_policy);
         return 1;
     }
-
-    return __copy_to_guest(runstate_guest(v), &v->runstate, 1) !=
-           sizeof(v->runstate);
+    else

Pointless else. Remember, in the previous version you used "goto", which I asked you to avoid by using "else". Since you opted for retaining the "return" above in this version, the need for the "else" went away. The alternative would now be to convert the "return" above to "rc = 1" and move the return below out of the "else" scope.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,

Quoted 7 lines
xen/arch/x86/mm/guest_walk.c +7 -0
+        switch ( v->arch.smap_check_policy )
+        {
+        case SMAP_CHECK_HONOR_CPL_AC:
+            hvm_get_segment_register(v, x86_seg_ss, &seg);
+
+            /*
+             * SMAP: kernel-mode data accesses from user-mode

mappings

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 190

Quoted 11 lines
xen/arch/x86/mm/guest_walk.c +11 -0
+             * should fault.
+             * A fault is considered as a SMAP violation if the following
+             * conditions come true:
+             *   - X86_CR4_SMAP is set in CR4
+             *   - A user page is accessed
+             *   - CPL = 3 or X86_EFLAGS_AC is clear
+             *   - Page fault in kernel mode
+             */
+            smap = hvm_smap_enabled(v) &&
+                   ((seg.attr.fields.dpl == 3) ||
+                   !(regs->eflags & X86_EFLAGS_AC));

Indentation.

Review

Quoted 2 lines
(quoted diff) +2 -0
+typedef enum {
+    SMAP_CHECK_HONOR_CPL_AC = 0,  /* honor the guest's CPL and AC

*/

Pointless " = 0".

xen/include/asm-x86/domain.h struct pv_vcpu · line 389

Quoted 3 lines
xen/include/asm-x86/domain.h +3 -0
+    SMAP_CHECK_ENABLED,           /* enable the check */
+    SMAP_CHECK_DISABLED,          /* disable the check */
+} smap_check_policy_t;

Perhaps worth adding the __packed__ attribute here to get this back down to a byte ...

Quoted 2 lines
(quoted diff) +0 -0
@@ -446,13 +452,22 @@ struct arch_vcpu
 
Quoted 11 lines

xen/include/asm-x86/domain.h struct arch_vcpu · line 461

Quoted 9 lines
xen/include/asm-x86/domain.h +6 -0
     /* A secondary copy of the vcpu time info. */
     XEN_GUEST_HANDLE(vcpu_time_info_t) time_info_guest;
+
+    /*
+     * The SMAP check policy when updating runstate_guest(v) and the
+     * secondary system time.
+     */
+    smap_check_policy_t smap_check_policy;
 } __cacheline_aligned;

... and try to find a better place (current padding) in this structure?

Seems I cannot find a padding place in struct arch_vcpu, neither in struct hvm_vcpu.

Thanks,
Feng

Quoted 2 lines

Jan

#4 lore raw

Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

In reply to Wu, Feng
Quoted 23 lines
Quoted 23 lines
Quoted 1 line · On 29.07.14 at 09:54, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 08.01.01 at 23:52, <[email protected]> wrote:
xen/arch/x86/mm/guest_walk.c +7 -0
+        switch ( v->arch.smap_check_policy )
+        {
+        case SMAP_CHECK_HONOR_CPL_AC:
+            hvm_get_segment_register(v, x86_seg_ss, &seg);
+
+            /*
+             * SMAP: kernel-mode data accesses from user-mode

mappings

Quoted 11 lines
xen/arch/x86/mm/guest_walk.c +11 -0
+             * should fault.
+             * A fault is considered as a SMAP violation if the following
+             * conditions come true:
+             *   - X86_CR4_SMAP is set in CR4
+             *   - A user page is accessed
+             *   - CPL = 3 or X86_EFLAGS_AC is clear
+             *   - Page fault in kernel mode
+             */
+            smap = hvm_smap_enabled(v) &&
+                   ((seg.attr.fields.dpl == 3) ||
+                   !(regs->eflags & X86_EFLAGS_AC));
Quoted 7 lines
Quoted 1 line

Indentation.

Sorry, I don't find any indentation issue here. Do you mean "((seg.attr.fields.dpl == 3) ||" and "!(regs->eflags & X86_EFLAGS_AC));" are not indented with "hvm_smap_enabled(v) &&"? In fact they are in good indentation. Maybe it is the display that make it look
like in wrong indentation?

No, I specifically checked in a mono-spaced font. The parentheses around the || expression require its right side to be indented by one more space than the very opening parenthesis.

Jan

#10 lore raw

Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

In reply to Wu, Feng
Quoted 17 lines
Quoted 5 lines
Quoted 1 line · On 29.07.14 at 10:13, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 08.01.01 at 23:52, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -446,13 +452,22 @@ struct arch_vcpu
 
Quoted 11 lines

xen/include/asm-x86/domain.h struct arch_vcpu · line 461

Quoted 9 lines
xen/include/asm-x86/domain.h +6 -0
     /* A secondary copy of the vcpu time info. */
     XEN_GUEST_HANDLE(vcpu_time_info_t) time_info_guest;
+
+    /*
+     * The SMAP check policy when updating runstate_guest(v) and the
+     * secondary system time.
+     */
+    smap_check_policy_t smap_check_policy;
 } __cacheline_aligned;

... and try to find a better place (current padding) in this structure?

Quoted 2 lines

Seems I cannot find a padding place in struct arch_vcpu, neither in struct hvm_vcpu.

Here's the tail of struct arch_vcpu:

    uint64_t xcr0_accum;
    /* This variable determines whether nonlazy extended state has been used,
     * and thus should be saved/restored. */
    bool_t nonlazy_xstate_used;

    struct vmce vmce;

    struct paging_vcpu paging;

    uint32_t gdbsx_vcpu_event;

    /* A secondary copy of the vcpu time info. */
    XEN_GUEST_HANDLE(vcpu_time_info_t) time_info_guest;
} __cacheline_aligned;

Obviously there are gaps after both nonlazy_xstate_used and gdbsx_vcpu_event.

Jan

author
#11 lore raw

Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 49 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 29, 2014 4:25 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 17 lines
Quoted 5 lines
Quoted 1 line · On 29.07.14 at 10:13, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 08.01.01 at 23:52, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -446,13 +452,22 @@ struct arch_vcpu
 
Quoted 11 lines

xen/include/asm-x86/domain.h struct arch_vcpu · line 461

Quoted 9 lines
xen/include/asm-x86/domain.h +6 -0
     /* A secondary copy of the vcpu time info. */
     XEN_GUEST_HANDLE(vcpu_time_info_t) time_info_guest;
+
+    /*
+     * The SMAP check policy when updating runstate_guest(v) and the
+     * secondary system time.
+     */
+    smap_check_policy_t smap_check_policy;
 } __cacheline_aligned;

... and try to find a better place (current padding) in this structure?

Quoted 2 lines

Seems I cannot find a padding place in struct arch_vcpu, neither in struct hvm_vcpu.

Here's the tail of struct arch_vcpu:

    uint64_t xcr0_accum;
    /* This variable determines whether nonlazy extended state has been
used,
     * and thus should be saved/restored. */
    bool_t nonlazy_xstate_used;

    struct vmce vmce;

    struct paging_vcpu paging;

    uint32_t gdbsx_vcpu_event;

    /* A secondary copy of the vcpu time info. */
    XEN_GUEST_HANDLE(vcpu_time_info_t) time_info_guest;
} __cacheline_aligned;
Quoted 4 lines

Obviously there are gaps after both nonlazy_xstate_used and gdbsx_vcpu_event.

Jan

Okay, I got your point. Thanks a lot!

Thanks,
Feng

author
#5 lore raw

Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

In reply to Jan Beulich
Quoted 42 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 29, 2014 4:23 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 23 lines
Quoted 23 lines
Quoted 1 line · On 29.07.14 at 09:54, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 08.01.01 at 23:52, <[email protected]> wrote:
xen/arch/x86/mm/guest_walk.c +7 -0
+        switch ( v->arch.smap_check_policy )
+        {
+        case SMAP_CHECK_HONOR_CPL_AC:
+            hvm_get_segment_register(v, x86_seg_ss, &seg);
+
+            /*
+             * SMAP: kernel-mode data accesses from user-mode

mappings

Quoted 11 lines
xen/arch/x86/mm/guest_walk.c +11 -0
+             * should fault.
+             * A fault is considered as a SMAP violation if the following
+             * conditions come true:
+             *   - X86_CR4_SMAP is set in CR4
+             *   - A user page is accessed
+             *   - CPL = 3 or X86_EFLAGS_AC is clear
+             *   - Page fault in kernel mode
+             */
+            smap = hvm_smap_enabled(v) &&
+                   ((seg.attr.fields.dpl == 3) ||
+                   !(regs->eflags & X86_EFLAGS_AC));
Quoted 7 lines
Quoted 1 line

Indentation.

Sorry, I don't find any indentation issue here. Do you mean "((seg.attr.fields.dpl == 3) ||" and "!(regs->eflags & X86_EFLAGS_AC));" are not indented with "hvm_smap_enabled(v) &&"? In fact they are in good indentation. Maybe it is the display that make it look
like in wrong indentation?

Quoted 3 lines

No, I specifically checked in a mono-spaced font. The parentheses around the || expression require its right side to be indented by one more space than the very opening parenthesis.

In this case, do you mean I need to add another space before "!(regs->eflags & X86_EFLAGS_AC))"?

Thanks,
Feng

Quoted 2 lines

Jan

#6 lore raw

Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 43 lines
Quoted 43 lines
Quoted 1 line · On 29.07.14 at 10:42, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 29, 2014 4:23 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 30 lines
Quoted 23 lines
Quoted 1 line · On 29.07.14 at 09:54, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 08.01.01 at 23:52, <[email protected]> wrote:
xen/arch/x86/mm/guest_walk.c +7 -0
+        switch ( v->arch.smap_check_policy )
+        {
+        case SMAP_CHECK_HONOR_CPL_AC:
+            hvm_get_segment_register(v, x86_seg_ss, &seg);
+
+            /*
+             * SMAP: kernel-mode data accesses from user-mode

mappings

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 190

Quoted 11 lines
xen/arch/x86/mm/guest_walk.c +11 -0
+             * should fault.
+             * A fault is considered as a SMAP violation if the following
+             * conditions come true:
+             *   - X86_CR4_SMAP is set in CR4
+             *   - A user page is accessed
+             *   - CPL = 3 or X86_EFLAGS_AC is clear
+             *   - Page fault in kernel mode
+             */
+            smap = hvm_smap_enabled(v) &&
+                   ((seg.attr.fields.dpl == 3) ||
+                   !(regs->eflags & X86_EFLAGS_AC));
Quoted 1 line

Indentation.

Sorry, I don't find any indentation issue here. Do you mean "((seg.attr.fields.dpl == 3) ||" and "!(regs->eflags & X86_EFLAGS_AC));" are not indented with "hvm_smap_enabled(v) &&"? In fact they are in good indentation. Maybe it is the display that make it look
like in wrong indentation?

Quoted 6 lines
Quoted 3 lines

No, I specifically checked in a mono-spaced font. The parentheses around the || expression require its right side to be indented by one more space than the very opening parenthesis.

In this case, do you mean I need to add another space before "!(regs->eflags & X86_EFLAGS_AC))"?

Exactly.

Jan

author
#7 lore raw

Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 60 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 29, 2014 5:05 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 22 lines
Quoted 22 lines
Quoted 1 line · On 29.07.14 at 10:42, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 29, 2014 4:23 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 10 lines
Quoted 10 lines
Quoted 1 line · On 29.07.14 at 09:54, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 08.01.01 at 23:52, <[email protected]> wrote:
xen/arch/x86/mm/guest_walk.c +7 -0
+        switch ( v->arch.smap_check_policy )
+        {
+        case SMAP_CHECK_HONOR_CPL_AC:
+            hvm_get_segment_register(v, x86_seg_ss, &seg);
+
+            /*
+             * SMAP: kernel-mode data accesses from user-mode
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

mappings

Quoted 2 lines
(quoted diff) +2 -0
+             * should fault.
+             * A fault is considered as a SMAP violation if the

following

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
xen/arch/x86/mm/guest_walk.c +9 -0
+             * conditions come true:
+             *   - X86_CR4_SMAP is set in CR4
+             *   - A user page is accessed
+             *   - CPL = 3 or X86_EFLAGS_AC is clear
+             *   - Page fault in kernel mode
+             */
+            smap = hvm_smap_enabled(v) &&
+                   ((seg.attr.fields.dpl == 3) ||
+                   !(regs->eflags & X86_EFLAGS_AC));
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Indentation.

Sorry, I don't find any indentation issue here. Do you mean "((seg.attr.fields.dpl == 3) ||" and "!(regs->eflags & X86_EFLAGS_AC));" are not indented with "hvm_smap_enabled(v) &&"? In fact they are in

good

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

indentation. Maybe it is the display that make it look like in wrong indentation?

No, I specifically checked in a mono-spaced font. The parentheses around the || expression require its right side to be indented by one more space than the very opening parenthesis.

Quoted 5 lines
Quoted 1 line

In this case, do you mean I need to add another space before "!(regs->eflags

&

Quoted 1 line

X86_EFLAGS_AC))"?

Exactly.

Good to know this rule! BTW, is there some documents or something like that to tell what is the correct coding style for Xen?

Thanks,
Feng

Quoted 2 lines

Jan

#8 lore raw

Re: [PATCH v3 1/2] x86/hvm: Always do SMAP check when updating runstate_guest(v)

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 29.07.14 at 11:07, <[email protected]> wrote:

Good to know this rule! BTW, is there some documents or something like that to tell
what is the correct coding style for Xen?

Sure, ./CODING_STYLE. But it doesn't cover each and every detail, and simply matching surrounding code style is often a good first shot.

Jan