Emulator segment checks
cover Emulator segment checks
This patchset implements segment checks for the emulator. These are needed for guests which can both get the emulator to execute an instruction from an unprivilged context, and also rely on segmentation for security.
Avi Kivity (7):
KVM: x86 emulator: Add helpers for memory access using segmented
addresses
KVM: x86 emulator: move invlpg emulation into a function
KVM: x86 emulator: change address linearization to return an error
code
KVM: x86 emulator: pass access size and read/write intent to
linearize()
KVM: x86 emulator: move linearize() downwards
KVM: x86 emulator: move desc_limit_scaled()
KVM: x86 emulator: implement segment permission checks
arch/x86/kvm/emulate.c | 222 ++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 176 insertions(+), 46 deletions(-)
PATCH 1/7 KVM: x86 emulator: Add helpers for memory access using segmented addresses
Will help later adding proper segment checks.
--- arch/x86/kvm/emulate.c | 75 +++++++++++++++++++++++++++++++++-------------- 1 files changed, 52 insertions(+), 23 deletions(-)
arch/x86/kvm/emulate.c +52 -23
@@ -511,6 +511,15 @@ static int emulate_nm(struct x86_emulate_ctxt *ctxt)return emulate_exception (ctxt,NM_VECTOR ,0 , false); } +static int segmented_read_std (struct x86_emulate_ctxt *ctxt, +struct segmented_address addr, +void *data, +unsigned size) +{ +return ctxt->ops->read_std (linear (ctxt, addr), data, size, ctxt->vcpu, + &ctxt->exception); +} +static int do_fetch_insn_byte (struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops,unsigned long eip,u8 *dest)
@@ -575,13 +584,11 @@ static int read_descriptor(struct x86_emulate_ctxt *ctxt,if (op_bytes ==2 ) op_bytes =3 ; *address =0 ; - rc = ops->read_std (linear (ctxt, addr), (unsigned long *)size,2 , - ctxt->vcpu, &ctxt->exception); + rc =segmented_read_std (ctxt, addr, size,2 );if (rc !=X86EMUL_CONTINUE )return rc; addr.ea +=2 ; - rc = ops->read_std (linear (ctxt, addr), address, op_bytes, - ctxt->vcpu, &ctxt->exception); + rc =segmented_read_std (ctxt, addr, address, op_bytes);return rc; }
@@ -921,6 +928,32 @@ static int read_emulated(struct x86_emulate_ctxt *ctxt,return X86EMUL_CONTINUE ; } +static int segmented_read (struct x86_emulate_ctxt *ctxt, +struct segmented_address addr, +void *data, +unsigned size) +{ +return read_emulated (ctxt, ctxt->ops,linear (ctxt, addr), data, size); +} + +static int segmented_write (struct x86_emulate_ctxt *ctxt, +struct segmented_address addr, +const void *data, +unsigned size) +{ +return ctxt->ops->write_emulated (linear (ctxt, addr), data, size, + &ctxt->exception, ctxt->vcpu); +} + +static int segmented_cmpxchg (struct x86_emulate_ctxt *ctxt, +struct segmented_address addr, +const void *orig_data,const void *data, +unsigned size) +{ +return ctxt->ops->cmpxchg_emulated (linear (ctxt, addr), orig_data, data, + size, &ctxt->exception, ctxt->vcpu); +} + static int pio_in_emulated(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops, unsigned int size, unsigned short port,
@@ -1168,20 +1201,16 @@ static inline int writeback(struct x86_emulate_ctxt *ctxt,break ;case OP_MEM :if (c->lock_prefix) - rc = ops->cmpxchg_emulated ( -linear (ctxt, c->dst.addr.mem), - &c->dst.orig_val, - &c->dst.val, - c->dst.bytes, - &ctxt->exception, - ctxt->vcpu); + rc =segmented_cmpxchg (ctxt, + c->dst.addr.mem, + &c->dst.orig_val, + &c->dst.val, + c->dst.bytes);else - rc = ops->write_emulated ( -linear (ctxt, c->dst.addr.mem), - &c->dst.val, - c->dst.bytes, - &ctxt->exception, - ctxt->vcpu); + rc =segmented_write (ctxt, + c->dst.addr.mem, + &c->dst.val, + c->dst.bytes);if (rc !=X86EMUL_CONTINUE )return rc;break ;
@@ -1220,7 +1249,7 @@ static int emulate_pop(struct x86_emulate_ctxt *ctxt, addr.ea =register_address (c, c->regs[VCPU_REGS_RSP ]); addr.seg =VCPU_SREG_SS ; - rc =read_emulated (ctxt, ops,linear (ctxt, addr), dest, len); + rc =segmented_read (ctxt, addr, dest, len);if (rc !=X86EMUL_CONTINUE )return rc;
@@ -3131,16 +3160,16 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt) }if ((c->src.type ==OP_MEM ) && !(c->d & NoAccess)) { - rc =read_emulated (ctxt, ops,linear (ctxt, c->src.addr.mem), - c->src.valptr, c->src.bytes); + rc =segmented_read (ctxt, c->src.addr.mem, + c->src.valptr, c->src.bytes);if (rc !=X86EMUL_CONTINUE ) goto done; c->src.orig_val64 = c->src.val64; }if (c->src2.type ==OP_MEM ) { - rc =read_emulated (ctxt, ops,linear (ctxt, c->src2.addr.mem), - &c->src2.val, c->src2.bytes); + rc =segmented_read (ctxt, c->src2.addr.mem, + &c->src2.val, c->src2.bytes);if (rc !=X86EMUL_CONTINUE ) goto done; }
@@ -3151,7 +3180,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)if ((c->dst.type ==OP_MEM ) && !(c->d & Mov)) {/* optimisation - avoid slow emulated read if Mov */ - rc =read_emulated (ctxt, ops,linear (ctxt, c->dst.addr.mem), + rc =segmented_read (ctxt, c->dst.addr.mem, &c->dst.val, c->dst.bytes);if (rc !=X86EMUL_CONTINUE ) goto done;
Signature · 1 line
1.7.1
PATCH 2/7 KVM: x86 emulator: move invlpg emulation into a function
In reply to Avi KivityIt's going to get more complicated soon.
--- arch/x86/kvm/emulate.c | 14 ++++++++++---- 1 files changed, 10 insertions(+), 4 deletions(-)
arch/x86/kvm/emulate.c +10 -4
@@ -2451,6 +2451,15 @@ static int em_movdqu(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int em_invlpg (struct x86_emulate_ctxt *ctxt) +{ +struct decode_cache *c = &ctxt->decode; +emulate_invlpg (ctxt->vcpu,linear (ctxt, c->src.addr.mem)); +/* Disable writeback. */ + c->dst.type =OP_NONE ; +return X86EMUL_CONTINUE ; +} +#define D (_y) { .flags = (_y) }#define N D(0)#define G (_f, _g) { .flags = ((_f) | Group), .u.group = (_g) }
@@ -3658,10 +3667,7 @@ twobyte_insn: rc =X86EMUL_PROPAGATE_FAULT ; goto done;case 7 :/* invlpg*/ -emulate_invlpg (ctxt->vcpu, -linear (ctxt, c->src.addr.mem)); -/* Disable writeback. */ - c->dst.type =OP_NONE ; + rc =em_invlpg (ctxt);break ;default : goto cannot_emulate;
Signature · 1 line
1.7.1
PATCH 3/7 KVM: x86 emulator: change address linearization to return an error code
In reply to Avi KivityPreparing to add segment checks.
--- arch/x86/kvm/emulate.c | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 39 insertions(+), 8 deletions(-)
arch/x86/kvm/emulate.c +39 -8
@@ -465,8 +465,9 @@ static unsigned seg_override(struct x86_emulate_ctxt *ctxt,return c->seg_override; } -static ulong linear (struct x86_emulate_ctxt *ctxt, -struct segmented_address addr) +static int linearize (struct x86_emulate_ctxt *ctxt, +struct segmented_address addr, +ulong *linear) {struct decode_cache *c = &ctxt->decode;ulong la;
@@ -474,7 +475,8 @@ static ulong linear(struct x86_emulate_ctxt *ctxt, la =seg_base (ctxt, ctxt->ops, addr.seg) + addr.ea;if (c->ad_bytes !=8 ) la &= (u32)-1 ; -return la; + *linear = la; +return X86EMUL_CONTINUE ; } static int emulate_exception(struct x86_emulate_ctxt *ctxt, int vec,
@@ -516,7 +518,13 @@ static int segmented_read_std(struct x86_emulate_ctxt *ctxt,void *data, unsigned size) { -return ctxt->ops->read_std (linear (ctxt, addr), data, size, ctxt->vcpu, + int rc; +ulong linear; + + rc =linearize (ctxt, addr, &linear); +if (rc !=X86EMUL_CONTINUE ) +return rc; +return ctxt->ops->read_std (linear, data, size, ctxt->vcpu, &ctxt->exception); }
@@ -933,7 +941,13 @@ static int segmented_read(struct x86_emulate_ctxt *ctxt,void *data, unsigned size) { -return read_emulated (ctxt, ctxt->ops,linear (ctxt, addr), data, size); + int rc; +ulong linear; + + rc =linearize (ctxt, addr, &linear); +if (rc !=X86EMUL_CONTINUE ) +return rc; +return read_emulated (ctxt, ctxt->ops, linear, data, size); } static int segmented_write(struct x86_emulate_ctxt *ctxt,
@@ -941,7 +955,13 @@ static int segmented_write(struct x86_emulate_ctxt *ctxt,const void *data, unsigned size) { -return ctxt->ops->write_emulated (linear (ctxt, addr), data, size, + int rc; +ulong linear; + + rc =linearize (ctxt, addr, &linear); +if (rc !=X86EMUL_CONTINUE ) +return rc; +return ctxt->ops->write_emulated (linear, data, size, &ctxt->exception, ctxt->vcpu); }
@@ -950,7 +970,13 @@ static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt, const void *orig_data, const void *data, unsigned size) { - return ctxt->ops->cmpxchg_emulated(linear(ctxt, addr), orig_data, data, + int rc; +ulong linear; + + rc =linearize (ctxt, addr, &linear); +if (rc !=X86EMUL_CONTINUE ) +return rc; +return ctxt->ops->cmpxchg_emulated (linear, orig_data, data, size, &ctxt->exception, ctxt->vcpu); }
@@ -2454,7 +2480,12 @@ static int em_movdqu(struct x86_emulate_ctxt *ctxt)static int em_invlpg (struct x86_emulate_ctxt *ctxt) {struct decode_cache *c = &ctxt->decode; -emulate_invlpg (ctxt->vcpu,linear (ctxt, c->src.addr.mem)); +int rc; +ulong linear; + + rc =linearize (ctxt, c->src.addr.mem, &linear); +if (rc ==X86EMUL_CONTINUE ) +emulate_invlpg (ctxt->vcpu, linear);/* Disable writeback. */ c->dst.type =OP_NONE ;return X86EMUL_CONTINUE ;
Signature · 1 line
1.7.1
PATCH 4/7 KVM: x86 emulator: pass access size and read/write intent to linearize()
In reply to Avi KivityNeeded for segment read/write checks.
--- arch/x86/kvm/emulate.c | 11 ++++++----- 1 files changed, 6 insertions(+), 5 deletions(-)
arch/x86/kvm/emulate.c +6 -5
@@ -467,6 +467,7 @@ static unsigned seg_override(struct x86_emulate_ctxt *ctxt,static int linearize (struct x86_emulate_ctxt *ctxt,struct segmented_address addr, +unsigned size,bool write,ulong *linear) {struct decode_cache *c = &ctxt->decode;
@@ -521,7 +522,7 @@ static int segmented_read_std(struct x86_emulate_ctxt *ctxt,int rc;ulong linear; - rc =linearize (ctxt, addr, &linear); + rc =linearize (ctxt, addr, size, false, &linear);if (rc !=X86EMUL_CONTINUE )return rc; return ctxt->ops->read_std(linear, data, size, ctxt->vcpu,
@@ -944,7 +945,7 @@ static int segmented_read(struct x86_emulate_ctxt *ctxt,int rc;ulong linear; - rc =linearize (ctxt, addr, &linear); + rc =linearize (ctxt, addr, size, false, &linear);if (rc !=X86EMUL_CONTINUE )return rc;return read_emulated (ctxt, ctxt->ops, linear, data, size);
@@ -958,7 +959,7 @@ static int segmented_write(struct x86_emulate_ctxt *ctxt,int rc;ulong linear; - rc =linearize (ctxt, addr, &linear); + rc =linearize (ctxt, addr, size, true, &linear);if (rc !=X86EMUL_CONTINUE )return rc; return ctxt->ops->write_emulated(linear, data, size,
@@ -973,7 +974,7 @@ static int segmented_cmpxchg(struct x86_emulate_ctxt *ctxt,int rc;ulong linear; - rc =linearize (ctxt, addr, &linear); + rc =linearize (ctxt, addr, size, true, &linear);if (rc !=X86EMUL_CONTINUE )return rc; return ctxt->ops->cmpxchg_emulated(linear, orig_data, data,
@@ -2483,7 +2484,7 @@ static int em_invlpg(struct x86_emulate_ctxt *ctxt)int rc;ulong linear; - rc =linearize (ctxt, c->src.addr.mem, &linear); + rc =linearize (ctxt, c->src.addr.mem,1 , false, &linear);if (rc ==X86EMUL_CONTINUE )emulate_invlpg (ctxt->vcpu, linear);/* Disable writeback. */
Signature · 1 line
1.7.1
PATCH 5/7 KVM: x86 emulator: move linearize() downwards
In reply to Avi KivitySo it can call emulate_gp() without forward declarations.
--- arch/x86/kvm/emulate.c | 30 +++++++++++++++--------------- 1 files changed, 15 insertions(+), 15 deletions(-)
arch/x86/kvm/emulate.c +15 -15
@@ -465,21 +465,6 @@ static unsigned seg_override(struct x86_emulate_ctxt *ctxt,return c->seg_override; } -static int linearize (struct x86_emulate_ctxt *ctxt, -struct segmented_address addr, -unsigned size,bool write, -ulong *linear) -{ -struct decode_cache *c = &ctxt->decode; -ulong la; - - la =seg_base (ctxt, ctxt->ops, addr.seg) + addr.ea; -if (c->ad_bytes !=8 ) - la &= (u32)-1 ; - *linear = la; -return X86EMUL_CONTINUE ; -} -static int emulate_exception (struct x86_emulate_ctxt *ctxt,int vec,u32 error,bool valid) {
@@ -514,6 +499,21 @@ static int emulate_nm(struct x86_emulate_ctxt *ctxt)return emulate_exception (ctxt,NM_VECTOR ,0 , false); } +static int linearize (struct x86_emulate_ctxt *ctxt, +struct segmented_address addr, +unsigned size,bool write, +ulong *linear) +{ +struct decode_cache *c = &ctxt->decode; +ulong la; + + la =seg_base (ctxt, ctxt->ops, addr.seg) + addr.ea; +if (c->ad_bytes !=8 ) + la &= (u32)-1 ; + *linear = la; +return X86EMUL_CONTINUE ; +} + static int segmented_read_std(struct x86_emulate_ctxt *ctxt, struct segmented_address addr, void *data,
Signature · 1 line
1.7.1
PATCH 6/7 KVM: x86 emulator: move desc_limit_scaled()
In reply to Avi KivityFor reuse later.
--- arch/x86/kvm/emulate.c | 14 +++++++------- 1 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/emulate.c +7 -7
@@ -440,6 +440,13 @@ static inline void jmp_rel(struct decode_cache *c, int rel)register_address_increment (c, &c->eip, rel); } +static u32 desc_limit_scaled (struct desc_struct *desc) +{ +u32 limit =get_desc_limit (desc); + +return desc->g ? (limit <<12 ) |0xfff : limit; +} +static void set_seg_override (struct decode_cache *c,int seg) { c->has_seg_override = true;
@@ -1011,13 +1018,6 @@ static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,return 1 ; } -static u32 desc_limit_scaled (struct desc_struct *desc) -{ -u32 limit =get_desc_limit (desc); - -return desc->g ? (limit <<12 ) |0xfff : limit; -} -static void get_descriptor_table_ptr (struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops,u16 selector,struct desc_ptr *dt)
Signature · 1 line
1.7.1
PATCH 7/7 KVM: x86 emulator: implement segment permission checks
In reply to Avi Kivity--- arch/x86/kvm/emulate.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 63 insertions(+), 0 deletions(-)
arch/x86/kvm/emulate.c +63 -0
@@ -486,6 +486,11 @@ static int emulate_gp(struct x86_emulate_ctxt *ctxt, int err)return emulate_exception (ctxt,GP_VECTOR , err, true); } +static int emulate_ss (struct x86_emulate_ctxt *ctxt,int err) +{ +return emulate_exception (ctxt,SS_VECTOR , err, true); +} +static int emulate_ud (struct x86_emulate_ctxt *ctxt) {return emulate_exception (ctxt,UD_VECTOR ,0 , false);
@@ -512,13 +517,71 @@ static int linearize(struct x86_emulate_ctxt *ctxt, ulong *linear) {struct decode_cache *c = &ctxt->decode; +struct desc_struct desc; +bool usable;ulong la; +u32 lim; +unsigned cpl, rpl; la =seg_base (ctxt, ctxt->ops, addr.seg) + addr.ea; +switch (ctxt->mode) { +case X86EMUL_MODE_REAL : +break ; +case X86EMUL_MODE_PROT64 : +if (((signed long )la <<16 ) >>16 != la) +return emulate_gp (ctxt,0 ); +break ; +default : + usable = ctxt->ops->get_cached_descriptor (&desc,NULL , addr.seg, + ctxt->vcpu); +if (!usable) + goto bad; +/* code segment or read-only data segment */ +if (((desc.type &8 ) || !(desc.type &2 )) && write) + goto bad; +/* unreadable code segment */ +if ((desc.type &8 ) && !(desc.type &2 )) + goto bad; + lim =desc_limit_scaled (&desc); +if ((desc.type &8 ) || !(desc.type &4 )) { +/* expand-up segment */ +if (addr.ea > lim || (u32)(addr.ea + size -1 ) > lim) + goto bad; + }else { +/* exapand-down segment */ +if (addr.ea <= lim || (u32)(addr.ea + size -1 ) <= lim) + goto bad; + lim = desc.d ?0xffffffff :0xffff ; +if (addr.ea > lim || (u32)(addr.ea + size -1 ) > lim) + goto bad; + } + cpl = ctxt->ops->cpl (ctxt->vcpu); + rpl = ctxt->ops->get_segment_selector (addr.seg, ctxt->vcpu) &3 ; + cpl =max (cpl, rpl); +if (!(desc.type &8 )) { +/* data segment */ +if (cpl > desc.dpl) + goto bad; + }else if ((desc.type &8 ) && !(desc.type &4 )) { +/* nonconforming code segment */ +if (cpl != desc.dpl) + goto bad; + }else if ((desc.type &8 ) && (desc.type &4 )) { +/* conforming code segment */ +if (cpl < desc.dpl) + goto bad; + } +break ; + }if (c->ad_bytes !=8 ) la &= (u32)-1 ; *linear = la;return X86EMUL_CONTINUE ; +bad: +if (addr.seg ==VCPU_SREG_SS ) +return emulate_ss (ctxt, addr.seg); +else +return emulate_gp (ctxt, addr.seg); } static int segmented_read_std(struct x86_emulate_ctxt *ctxt,
Signature · 1 line
1.7.1