x86/pv: Fixes to guest_io_okay()
cover x86/pv: Fixes to guest_io_okay()
Needs to be used in combination with Jan's "x86/PV: simplify (and thus correct) guest accessor functions" to provide correct behaviour. See patch 2.
Andrew Cooper (2):
x86/pv: Rework guest_io_okay() to return X86EMUL_* x86/pv: Handle #PF correctly when reading the IO permission bitmap
xen/arch/x86/pv/emul-priv-op.c | 63 +++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 28 deletions(-)
Signature · 1 line
2.39.5
PATCH 1/2 x86/pv: Rework guest_io_okay() to return X86EMUL_*
In order to fix a bug with guest_io_okay() (subsequent patch), rework guest_io_okay() to take in an emulation context, and return X86EMUL_* rather than a boolean.
For the failing case, take the opporunity to inject #GP explicitly, rather than returning X86EMUL_UNHANDLEABLE. There is a logical difference between "we know what this is, and it's #GP", vs "we don't know what this is".
There is no change in practice as emulation is the final step on general #GP resolution, but returning X86EMUL_UNHANDLEABLE would be a latent bug if a subsequent action were to appear.
No practical change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/pv/emul-priv-op.c | 36 ++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 13 deletions(-)
xen/arch/x86/pv/emul-priv-op.c +23 -13
@@ -156,14 +156,16 @@ static bool iopl_ok(const struct vcpu *v, const struct cpu_user_regs *regs) }/* Has the guest requested sufficient permission for this I/O access? */ -static bool guest_io_okay (unsigned int port,unsigned int bytes, -struct vcpu *v,struct cpu_user_regs *regs) +static int guest_io_okay (unsigned int port,unsigned int bytes, +struct x86_emulate_ctxt *ctxt) { +struct cpu_user_regs *regs = ctxt->regs; +struct vcpu *v = current;/* If in user mode, switch to kernel mode just to read I/O bitmap. */ const bool user_mode = !(v->arch.flags & TF_kernel_mode);if (iopl_ok (v, regs) ) -return true; +return X86EMUL_OKAY ;if ( (port + bytes) <= v->arch.pv.iobmp_limit ) {
@@ -190,10 +192,12 @@ static bool guest_io_okay(unsigned int port, unsigned int bytes,toggle_guest_pt (v);if ( (x.mask & (((1 << bytes) -1 ) << (port &7 ))) ==0 ) -return true; +return X86EMUL_OKAY ; } -return false; +x86_emul_hw_exception (X86_EXC_GP ,0 , ctxt); + +return X86EMUL_EXCEPTION ; }/* Has the administrator granted sufficient permission for this I/O access? */
@@ -353,12 +357,14 @@ static int cf_check read_io( struct priv_op_ctxt *poc = container_of(ctxt, struct priv_op_ctxt, ctxt);struct vcpu *curr = current;struct domain *currd = current->domain; +int rc;/* INS must not come here. */ ASSERT ((ctxt->opcode & ~9 ) ==0xe4 ); -if ( !guest_io_okay (port, bytes, curr, ctxt->regs) ) -return X86EMUL_UNHANDLEABLE ; + rc =guest_io_okay (port, bytes, ctxt); +if ( rc !=X86EMUL_OKAY ) +return rc; poc->bpmatch =check_guest_io_breakpoint (curr, port, bytes);
@@ -458,12 +464,14 @@ static int cf_check write_io( struct priv_op_ctxt *poc = container_of(ctxt, struct priv_op_ctxt, ctxt);struct vcpu *curr = current;struct domain *currd = current->domain; +int rc;/* OUTS must not come here. */ ASSERT ((ctxt->opcode & ~9 ) ==0xe6 ); -if ( !guest_io_okay (port, bytes, curr, ctxt->regs) ) -return X86EMUL_UNHANDLEABLE ; + rc =guest_io_okay (port, bytes, ctxt); +if ( rc !=X86EMUL_OKAY ) +return rc; poc->bpmatch =check_guest_io_breakpoint (curr, port, bytes);
@@ -612,8 +620,9 @@ static int cf_check rep_ins( *reps =0 ; -if ( !guest_io_okay (port, bytes_per_rep, curr, ctxt->regs) ) -return X86EMUL_UNHANDLEABLE ; + rc =guest_io_okay (port, bytes_per_rep, ctxt); +if ( rc !=X86EMUL_OKAY ) +return rc; rc =read_segment (x86_seg_es, &sreg, ctxt);if ( rc !=X86EMUL_OKAY )
@@ -678,8 +687,9 @@ static int cf_check rep_outs( *reps =0 ; -if ( !guest_io_okay (port, bytes_per_rep, curr, ctxt->regs) ) -return X86EMUL_UNHANDLEABLE ; + rc =guest_io_okay (port, bytes_per_rep, ctxt); +if ( rc !=X86EMUL_OKAY ) +return rc; rc =read_segment (seg, &sreg, ctxt);if ( rc !=X86EMUL_OKAY )
Signature · 1 line
2.39.5
Re: [PATCH 1/2] x86/pv: Rework guest_io_okay() to return X86EMUL_*
Quoted 8 lines · Andrew Cooper
In order to fix a bug with
guest_io_okay()(subsequent patch), reworkguest_io_okay()to take in an emulation context, and return X86EMUL_* rather than a boolean.For the failing case, take the opporunity to inject #GP explicitly, rather than returning
X86EMUL_UNHANDLEABLE. There is a logical difference between "we know what this is, and it's #GP", vs "we don't know what this is".
Quoted 5 lines
There is no change in practice as emulation is the final step on general #GP resolution, but returning
X86EMUL_UNHANDLEABLEwould be a latent bug if a subsequent action were to appear.No practical change.
I think there is a small functional difference, see below.
xen/arch/x86/pv/emul-priv-op.c static bool iopl_ok(const struct vcpu *v, const struct cpu_user_regs *regs) · line 162
Quoted 12 lines
xen/arch/x86/pv/emul-priv-op.c +3
-2@@ -156,14 +156,16 @@ static bool iopl_ok(const struct vcpu *v, const struct cpu_user_regs *regs) }/* Has the guest requested sufficient permission for this I/O access? */ -static bool guest_io_okay (unsigned int port,unsigned int bytes, -struct vcpu *v,struct cpu_user_regs *regs) +static int guest_io_okay (unsigned int port,unsigned int bytes, +struct x86_emulate_ctxt *ctxt) { +struct cpu_user_regs *regs = ctxt->regs;
const?
xen/arch/x86/pv/emul-priv-op.c static bool guest_io_okay(unsigned int port, unsigned int bytes, · line 198
Quoted 10 lines
xen/arch/x86/pv/emul-priv-op.c +2
-2@@ -190,10 +192,12 @@ static bool guest_io_okay(unsigned int port, unsigned int bytes,toggle_guest_pt (v);if ( (x.mask & (((1 << bytes) -1 ) << (port &7 ))) ==0 ) -return true; +return X86EMUL_OKAY ; } -return false; +x86_emul_hw_exception (X86_EXC_GP ,0 , ctxt);
do_general_protection() has
/* Pass on GPF as is. */
pv_inject_hw_exception(X86_EXC_GP, regs->error_code);which may make a difference in case the insn changes under our feet. The new behavior may even be deemed better, but the description then would want to admit to this slight functional change. Then
Jan
Re: [PATCH 1/2] x86/pv: Rework guest_io_okay() to return X86EMUL_*
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
xen/arch/x86/pv/emul-priv-op.c +1
-1@@ -190,10 +192,12 @@ static bool guest_io_okay(unsigned int port, unsigned int bytes,toggle_guest_pt (v);if ( (x.mask & (((1 << bytes) -1 ) << (port &7 ))) ==0 ) -return true; +return X86EMUL_OKAY ; }
Quoted 8 lines
xen/arch/x86/pv/emul-priv-op.c static bool guest_io_okay(unsigned int port, unsigned int bytes, · line 198
Quoted 2 lines
xen/arch/x86/pv/emul-priv-op.c +1
-1-return false; +x86_emul_hw_exception (X86_EXC_GP ,0 , ctxt);
do_general_protection()has/* Pass on GPF as is. */ pv_inject_hw_exception(X86_EXC_GP, regs->error_code);which may make a difference in case the insn changes under our feet.
It would make a difference if we chose to raise #GP[non-0].
But, see how the call to pv_emulate_privileged_op() is guarded on error_code == 0.
Prior X86EMUL_UNHANDLEABLE can't ever have raised anything other than #GP[0], (excusing cases of memory corruption in regs->error_code).
So, there is not a change in behaviour, even if the reason why is less-than-obvious.
~Andrew
Re: [PATCH 1/2] x86/pv: Rework guest_io_okay() to return X86EMUL_*
Quoted 17 lines · Andrew Cooper
Quoted 16 lines · Jan Beulich
xen/arch/x86/pv/emul-priv-op.c static bool guest_io_okay(unsigned int port, unsigned int bytes, · line 198
Quoted 10 lines · Andrew Cooper
xen/arch/x86/pv/emul-priv-op.c +2
-2@@ -190,10 +192,12 @@ static bool guest_io_okay(unsigned int port, unsigned int bytes,toggle_guest_pt (v);if ( (x.mask & (((1 << bytes) -1 ) << (port &7 ))) ==0 ) -return true; +return X86EMUL_OKAY ; } -return false; +x86_emul_hw_exception (X86_EXC_GP ,0 , ctxt);
do_general_protection()has/* Pass on GPF as is. */ pv_inject_hw_exception(X86_EXC_GP, regs->error_code);
Quoted 6 lines
Quoted 1 line
which may make a difference in case the insn changes under our feet.
It would make a difference if we chose to raise #GP[non-0].
But, see how the call to
pv_emulate_privileged_op()is guarded onerror_code== 0.
Oh, good point - I overlooked that.
Quoted 5 lines
Prior
X86EMUL_UNHANDLEABLEcan't ever have raised anything other than #GP[0], (excusing cases of memory corruption inregs->error_code).So, there is not a change in behaviour, even if the reason why is less-than-obvious.
I agree then.
Jan
PATCH 2/2 x86/pv: Handle #PF correctly when reading the IO permission bitmap
In reply to Andrew CooperThe switch statement in guest_io_okay() is a very expensive way of pre-initialising x with ~0, and performing a partial read into it.
However, the logic isn't correct either.
In a real TSS, the CPU always reads two bytes (like here), and any TSS limit violation turns silently into no-access. But, in-limit accesses trigger #PF as usual. AMD document this property explicitly, and while Intel don't (so far as I can tell), they do behave consistently with AMD.
Switch from __copy_from_guest_offset() to __copy_from_guest_pv(), like everything else in this file. This removes code generation setting up copy_from_user_hvm() (in the likely path even), and safety LFENCEs from evaluate_nospec().
Change the logic to raise #PF if __copy_from_guest_pv() fails, rather than disallowing the IO port access. This brings the behaviour better in line with normal x86.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
RFC: Should we make the boundary check be (port + bytes + 8)? That would be more correct, but liable to break unsuspecting VMs. Maybe we should just comment our way out of it.
This needs to be combined with Jan's "x86/PV: simplify (and thus correct) guest accessor functions" to function completely correctly. From XTF testing:
This series on its own:
--- Xen Test Framework ---
Environment: PV 64bit (Long mode 4 levels)
Test pv-emul-cr2
Error: %cr2 expected 0x3000, got 0x2fff
Test result: ERROR
This series plus Jan's fix:
--- Xen Test Framework ---
Environment: PV 64bit (Long mode 4 levels)
Test pv-emul-cr2
Test result: SUCCESS
Interestingly, the test also does an INW without an output parameter straddling that page boundary, and it does reliably get 0x3000 even without the accessor fix.
--- xen/arch/x86/pv/emul-priv-op.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)
xen/arch/x86/pv/emul-priv-op.c +12 -15
@@ -169,29 +169,26 @@ static int guest_io_okay(unsigned int port, unsigned int bytes,if ( (port + bytes) <= v->arch.pv.iobmp_limit ) { -union {uint8_t bytes[2 ];uint16_t mask; } x; +const void *__user addr = v->arch.pv.iobmp.p + (port >>3 ); +uint16_t mask; +int rc; -/* -* Grab permission bytes from guest space. Inaccessible bytes are -* read as 0xff (no access allowed). -*/ +/* Grab permission bytes from guest space. */ if ( user_mode )toggle_guest_pt (v); -switch (__copy_from_guest_offset (x.bytes, v->arch.pv.iobmp, - port>>3 ,2 ) ) - { -default : x.bytes[0 ] = ~0 ; -/* fallthrough */ -case 1 : x.bytes[1 ] = ~0 ; -/* fallthrough */ -case 0 :break ; - } + rc =__copy_from_guest_pv (&mask, addr,2 );if ( user_mode )toggle_guest_pt (v); -if ( (x.mask & (((1 << bytes) -1 ) << (port &7 ))) ==0 ) +if ( rc ) + { +x86_emul_pagefault (0 , (unsigned long )addr + bytes - rc, ctxt); +return X86EMUL_EXCEPTION ; + } + +if ( (mask & (((1 << bytes) -1 ) << (port &7 ))) ==0 )return X86EMUL_OKAY ; }
Signature · 1 line
2.39.5
Re: [PATCH 2/2] x86/pv: Handle #PF correctly when reading the IO permission bitmap
Quoted 3 lines · Andrew Cooper
RFC: Should we make the boundary check be (port + bytes + 8)? That would be more correct, but liable to break unsuspecting VMs. Maybe we should just comment our way out of it.
What would the "+ 8" be intended to express? (I take it you mean ...
xen/arch/x86/pv/emul-priv-op.c static intguest_io_okay(unsigned int port, unsigned int bytes, · line 170
Quoted 5 lines
xen/arch/x86/pv/emul-priv-op.c +0
-0@@ -169,29 +169,26 @@ static intguest_io_okay(unsigned int port, unsigned int bytes,if ( (port + bytes) <= v->arch.pv.iobmp_limit )
... this check, which looks correct to me as is. In particular with the "+ 8" there would appear to be no way to access ports at the very top of the 64k range anymore, as PHYSDEVOP_set_iobitmap handling caps nr_ports at 64k. IOW I think "commenting our way out of it" is the only possible approach.)
With or without such a comment added
Jan
Re: [PATCH 2/2] x86/pv: Handle #PF correctly when reading the IO permission bitmap
Quoted 10 lines · Jan Beulich
Quoted 3 lines · Andrew Cooper
RFC: Should we make the boundary check be (port + bytes + 8)? That would be more correct, but liable to break unsuspecting VMs. Maybe we should just comment our way out of it.
What would the "+ 8" be intended to express? (I take it you mean ...
Quoted 4 lines
xen/arch/x86/pv/emul-priv-op.c +0
-0@@ -169,29 +169,26 @@ static intguest_io_okay(unsigned int port, unsigned int bytes,
Quoted 6 lines
xen/arch/x86/pv/emul-priv-op.c static int guest_io_okay(unsigned int port, unsigned int bytes, · line 170
Quoted 1 line
xen/arch/x86/pv/emul-priv-op.c +0
-0if ( (port + bytes) <= v->arch.pv.iobmp_limit )... this check, which looks correct to me as is. In particular with the "+ 8" there would appear to be no way to access ports at the very top of the 64k range anymore, as
PHYSDEVOP_set_iobitmaphandling capsnr_portsat 64k. IOW I think "commenting our way out of it" is the only possible approach.)
This is actually a horrid corner case, different to what I was thinking yesterday.
In a real TSS, the CPU doesn't read past the TR limit. Such accesses are turned into a permission failure.
But in Xen, guest_io_okay() does read one byte past iobmp_limit.
Previously, we'd consume what was there if it was accessible, or declare a permissions failure if it was inaccessible. Now, we'll throw #PF back to the kernel with %cr2 beyond the the limit.
It's not OK for Xen to be reading past the given limit; it's not how real CPUs behave. Part of the problem is that, even since it's introduction in 013351bd7 (2006), the public interface was named nr_ports while Xen's internal field was called iobmp_limit.
In terms of how it's used these days in PV guests:
Linux points the bitmap into it's real TSS, and sets nr_ports to either 0 or 65536 depending on whether the userspace task is permitted to use IO ports or not.
NetBSD doesn't seem to use PHYSDEVOP_set_iobitmap at all, and only uses PHYSDEVOP_set_iopl.
MiniOS uses neither.
I think Xen's internal variable wants renaming to not be a limit, and the comment for PHYSDEVOP_set_iobitmap wants extending to note that, like a real TSS, Xen might read one byte beyond the end of the bitmap.
I think this wants doing in a separate patch. Thoughts?
Quoted 2 lines
With or without such a comment added
Reviewed-by Jan Beulich <[email protected]>
Thanks.
~Andrew
Re: [PATCH 2/2] x86/pv: Handle #PF correctly when reading the IO permission bitmap
Quoted 5 lines · Andrew Cooper
I think Xen's internal variable wants renaming to not be a limit, and the comment for
PHYSDEVOP_set_iobitmapwants extending to note that, like a real TSS, Xen might read one byte beyond the end of the bitmap.I think this wants doing in a separate patch. Thoughts?
That's probably going to be best.
Jan
[PATCH 3/2] x86/pv: Rename pv.iobmp_limit to iobmp_nr and clarify behaviour
In reply to Andrew CooperEver since it's introduction in commit 013351bd7ab3 ("Define new event-channel and physdev hypercalls"), the public interface was named nr_ports while the internal field was called iobmp_limit.
Rename the intenral field to iobmp_nr to match the public interface, and clarify that, when nonzero, Xen will read 2 bytes.
There isn't a perfect parallel with a real TSS, but iobmp_nr being 0 is the paravirt "no IOPB" case, and it is important that no read occurs in this case.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/include/asm/domain.h | 2 +- xen/arch/x86/physdev.c | 2 +- xen/arch/x86/pv/emul-priv-op.c | 7 ++++++- xen/include/public/physdev.h | 3 +++ 4 files changed, 11 insertions(+), 3 deletions(-)
xen/arch/x86/include/asm/domain.h +1 -1
@@ -573,7 +573,7 @@ struct pv_vcpu/* I/O-port access bitmap. */ XEN_GUEST_HANDLE (uint8) iobmp;/* Guest kernel vaddr of the bitmap. */ -unsigned int iobmp_limit;/* Number of ports represented in the bitmap. */ +unsigned int iobmp_nr;/* Number of ports represented in the bitmap. */ #define IOPL (val) MASK_INSR(val, X86_EFLAGS_IOPL)unsigned int iopl;/* Current IOPL for this VCPU, shifted left by * 12 to match the eflags register. */
xen/arch/x86/physdev.c +1 -1
@@ -436,7 +436,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)#else guest_from_compat_handle (curr->arch.pv.iobmp, set_iobitmap.bitmap);#endif - curr->arch.pv.iobmp_limit = set_iobitmap.nr_ports; + curr->arch.pv.iobmp_nr = set_iobitmap.nr_ports;break ; }
xen/arch/x86/pv/emul-priv-op.c +6 -1
@@ -167,7 +167,12 @@ static int guest_io_okay(unsigned int port, unsigned int bytes,if (iopl_ok (v, regs) )return X86EMUL_OKAY ; -if ( (port + bytes) <= v->arch.pv.iobmp_limit ) +/* +* When @iobmp_nr is non-zero, Xen, like real CPUs and the TSS IOPB, +* always reads 2 bytes from @iobmp, which might be one byte beyond +* @nr_ports. +*/ +if ( (port + bytes) <= v->arch.pv.iobmp_nr ) {const void *__user addr = v->arch.pv.iobmp.p + (port >>3 );uint16_t mask;
xen/include/public/physdev.h +3 -0
@@ -87,6 +87,9 @@ DEFINE_XEN_GUEST_HANDLE(physdev_set_iopl_t);/* * Set the current VCPU's I/O-port permissions bitmap. * @arg == pointer to physdev_set_iobitmap structure. +* +* When @nr_ports is non-zero, Xen, like real CPUs and the TSS IOPB, always +* reads 2 bytes from @bitmap, which might be one byte beyond @nr_ports. */ #define PHYSDEVOP_set_iobitmap 7struct physdev_set_iobitmap {
Signature · 1 line
2.39.5
Jan Beulich
Reviewed-byRe: [PATCH 3/2] x86/pv: Rename pv.iobmp_limit to iobmp_nr and clarify behaviour
Quoted 11 lines · Andrew Cooper
Ever since it's introduction in commit
013351bd7ab3("Define new event-channel and physdev hypercalls"), the public interface was namednr_portswhile the internal field was callediobmp_limit.Rename the intenral field to
iobmp_nrto match the public interface, and clarify that, when nonzero, Xen will read 2 bytes.There isn't a perfect parallel with a real TSS, but
iobmp_nrbeing 0 is the paravirt "no IOPB" case, and it is important that no read occurs in this case.Signed-off-by Andrew Cooper <[email protected]>
with one cosmetic request:
xen/arch/x86/pv/emul-priv-op.c static int guest_io_okay(unsigned int port, unsigned int bytes, · line 175
Quoted 13 lines
xen/arch/x86/pv/emul-priv-op.c +6
-1@@ -167,7 +167,12 @@ static int guest_io_okay(unsigned int port, unsigned int bytes,if (iopl_ok (v, regs) )return X86EMUL_OKAY ; -if ( (port + bytes) <= v->arch.pv.iobmp_limit ) +/* +* When @iobmp_nr is non-zero, Xen, like real CPUs and the TSS IOPB, +* always reads 2 bytes from @iobmp, which might be one byte beyond +* @nr_ports. +*/ +if ( (port + bytes) <= v->arch.pv.iobmp_nr )
If you use @nr_ports in the comment here, then I think some connection wants establishing as to where this comes from. Otherwise use @iobmp_nr a 2nd time.
Jan
Re: [PATCH 3/2] x86/pv: Rename pv.iobmp_limit to iobmp_nr and clarify behaviour
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper
Ever since it's introduction in commit
013351bd7ab3("Define new event-channel and physdev hypercalls"), the public interface was namednr_portswhile the internal field was callediobmp_limit.
Quoted 8 lines
Quoted 7 lines
Rename the intenral field to
iobmp_nrto match the public interface, and clarify that, when nonzero, Xen will read 2 bytes.There isn't a perfect parallel with a real TSS, but
iobmp_nrbeing 0 is the paravirt "no IOPB" case, and it is important that no read occurs in this case.Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Thanks.
Quoted 8 lines
with one cosmetic request:
Quoted 6 lines
xen/arch/x86/pv/emul-priv-op.c +0
-0@@ -167,7 +167,12 @@ static int guest_io_okay(unsigned int port, unsigned int bytes,if (iopl_ok (v, regs) )return X86EMUL_OKAY ;
Quoted 9 lines
xen/arch/x86/pv/emul-priv-op.c static int guest_io_okay(unsigned int port, unsigned int bytes, · line 175
Quoted 7 lines
xen/arch/x86/pv/emul-priv-op.c +6
-1-if ( (port + bytes) <= v->arch.pv.iobmp_limit ) +/* +* When @iobmp_nr is non-zero, Xen, like real CPUs and the TSS IOPB, +* always reads 2 bytes from @iobmp, which might be one byte beyond +* @nr_ports. +*/ +if ( (port + bytes) <= v->arch.pv.iobmp_nr )If you use @nr_ports in the comment here, then I think some connection wants establishing as to where this comes from. Otherwise use @iobmp_nr a 2nd time.
Oops, that was a copy&paste error from the header. I'll switch it back to @iobmp_nr.
~Andrew