From patchwork Wed Jul 7 21:23:59 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohammed Gamal X-Patchwork-Id: 110700 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.4/8.14.3) with ESMTP id o67LPhES026982 for ; Wed, 7 Jul 2010 21:25:43 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756793Ab0GGVZl (ORCPT ); Wed, 7 Jul 2010 17:25:41 -0400 Received: from mail-bw0-f46.google.com ([209.85.214.46]:64268 "EHLO mail-bw0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756437Ab0GGVZl (ORCPT ); Wed, 7 Jul 2010 17:25:41 -0400 Received: by mail-bw0-f46.google.com with SMTP id 1so88158bwz.19 for ; Wed, 07 Jul 2010 14:25:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:date :message-id:x-mailer:in-reply-to:references; bh=o9P4YSxgk1D2b+hNf86BuuW2DeGmb1lpYsCUGd000Yo=; b=Y5pyQy/kwmCCqCC3Lvv9j8CRiqCzwImQDr+w57zKuls42/ywkZB9b7K9NUJ9bNowlq khodDucac2h+qjxm0gizLMsY+iOZjtxX8HmgG+fkgi3aONGmw+ird4io9L6OelOI/ta4 V2dNwkI3RAb/o+mxWG2G/Ewxq+PZmjdykyuXg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=PXunhzCEu5YpIrIKtD5YiHatglK3NHRdbGASWfHWWgHrsuLtFXxPcHxT4RgfYCNPjc +VHppF5NqkHF3yVoEQre9A2/N2bg96+Zy24zdqZRF1cyQ/PNUc/BHUxRaRA7FgLD7nAo r1MnnA3Oc1nipSxATKyT9CP6ljEe29PF0XFRw= Received: by 10.204.14.71 with SMTP id f7mr5557188bka.200.1278537940123; Wed, 07 Jul 2010 14:25:40 -0700 (PDT) Received: from localhost ([41.238.114.156]) by mx.google.com with ESMTPS id v6sm29731117bkx.11.2010.07.07.14.25.29 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 07 Jul 2010 14:25:39 -0700 (PDT) From: Mohammed Gamal To: avi@redhat.com Cc: mtosatti@redhat.com, kvm@vger.kernel.org, Mohammed Gamal Subject: [RFC PATCH 3/3] x86 emulator: Add segment limit checks and helper functions Date: Thu, 8 Jul 2010 00:23:59 +0300 Message-Id: <1278537839-20144-4-git-send-email-m.gamal005@gmail.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1278537839-20144-1-git-send-email-m.gamal005@gmail.com> References: <1278537839-20144-1-git-send-email-m.gamal005@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Wed, 07 Jul 2010 21:25:45 +0000 (UTC) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index f40479a..1bd34dc 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -632,6 +632,15 @@ static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, return ops->get_cached_segment_base(seg, ctxt->vcpu); } +static u32 seg_limit(struct x86_emulate_ctxt *ctxt, + struct x86_emulate_ops *ops, int seg) +{ + if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS) + return 0; + + return ops->get_cached_segment_limit(seg, ctxt->vcpu); +} + static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops, struct decode_cache *c) @@ -660,6 +669,24 @@ static unsigned long ss_base(struct x86_emulate_ctxt *ctxt, return seg_base(ctxt, ops, VCPU_SREG_SS); } +static u32 cs_limit(struct x86_emulate_ctxt *ctxt, + struct x86_emulate_ops *ops) +{ + return seg_limit(ctxt, ops, VCPU_SREG_CS); +} + +static u32 es_limit(struct x86_emulate_ctxt *ctxt, + struct x86_emulate_ops *ops) +{ + return seg_limit(ctxt, ops, VCPU_SREG_ES); +} + +static u32 ss_limit(struct x86_emulate_ctxt *ctxt, + struct x86_emulate_ops *ops) +{ + return seg_limit(ctxt, ops, VCPU_SREG_SS); +} + static void emulate_exception(struct x86_emulate_ctxt *ctxt, int vec, u32 error, bool valid) { @@ -718,6 +745,11 @@ static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, { int rc; + if (eip + size > cs_base(ctxt, ops) + cs_limit(ctxt, ops)) { + emulate_gp(ctxt, 0); + return X86EMUL_PROPAGATE_FAULT; + } + /* x86 instructions are limited to 15 bytes. */ if (eip + size - ctxt->eip > 15) return X86EMUL_UNHANDLEABLE; @@ -1202,6 +1234,10 @@ done_prefixes: c->src.ptr = (unsigned long *) register_address(c, seg_override_base(ctxt, ops, c), c->regs[VCPU_REGS_RSI]); + if (c->src.ptr > (unsigned long *) (es_base(ctxt, ops) + es_limit(ctxt, ops))) { + emulate_gp(ctxt, 0); + return X86EMUL_PROPAGATE_FAULT; + } c->src.val = 0; break; case SrcImmFAddr: @@ -1298,6 +1334,10 @@ done_prefixes: c->dst.ptr = (unsigned long *) register_address(c, es_base(ctxt, ops), c->regs[VCPU_REGS_RDI]); + if (c->dst.ptr > (unsigned long *) (es_base(ctxt, ops) + es_limit(ctxt, ops))) { + emulate_gp(ctxt, 0); + return X86EMUL_PROPAGATE_FAULT; + } c->dst.val = 0; break; } @@ -1617,7 +1657,7 @@ static inline int writeback(struct x86_emulate_ctxt *ctxt, return X86EMUL_CONTINUE; } -static inline void emulate_push(struct x86_emulate_ctxt *ctxt, +static inline int emulate_push(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) { struct decode_cache *c = &ctxt->decode; @@ -1628,6 +1668,12 @@ static inline void emulate_push(struct x86_emulate_ctxt *ctxt, register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes); c->dst.ptr = (void *) register_address(c, ss_base(ctxt, ops), c->regs[VCPU_REGS_RSP]); + if (c->dst.ptr > (unsigned long *) (ss_base(ctxt, ops) + ss_limit(ctxt, ops))) { + emulate_gp(ctxt, 0); + return X86EMUL_PROPAGATE_FAULT; + } + + return X86EMUL_CONTINUE; } static int emulate_pop(struct x86_emulate_ctxt *ctxt, @@ -1635,11 +1681,15 @@ static int emulate_pop(struct x86_emulate_ctxt *ctxt, void *dest, int len) { struct decode_cache *c = &ctxt->decode; + unsigned long reg_addr = register_address(c, ss_base(ctxt, ops), c->regs[VCPU_REGS_RSP]); int rc; - rc = read_emulated(ctxt, ops, register_address(c, ss_base(ctxt, ops), - c->regs[VCPU_REGS_RSP]), - dest, len); + if (reg_addr > ss_base(ctxt, ops) + ss_limit(ctxt, ops)) { + emulate_gp(ctxt, 0); + return X86EMUL_PROPAGATE_FAULT; + } + + rc = read_emulated(ctxt, ops, reg_addr, dest, len); if (rc != X86EMUL_CONTINUE) return rc; @@ -1690,14 +1740,14 @@ static int emulate_popf(struct x86_emulate_ctxt *ctxt, return rc; } -static void emulate_push_sreg(struct x86_emulate_ctxt *ctxt, +static int emulate_push_sreg(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops, int seg) { struct decode_cache *c = &ctxt->decode; c->src.val = ops->get_segment_selector(seg, ctxt->vcpu); - emulate_push(ctxt, ops); + return emulate_push(ctxt, ops); } static int emulate_pop_sreg(struct x86_emulate_ctxt *ctxt, @@ -1727,7 +1777,10 @@ static int emulate_pusha(struct x86_emulate_ctxt *ctxt, (reg == VCPU_REGS_RSP) ? (c->src.val = old_esp) : (c->src.val = c->regs[reg]); - emulate_push(ctxt, ops); + rc = emulate_push(ctxt, ops); + + if (rc != X86EMUL_CONTINUE) + return rc; rc = writeback(ctxt, ops); if (rc != X86EMUL_CONTINUE) @@ -1839,15 +1892,13 @@ static inline int emulate_grp45(struct x86_emulate_ctxt *ctxt, old_eip = c->eip; c->eip = c->src.val; c->src.val = old_eip; - emulate_push(ctxt, ops); - break; + return emulate_push(ctxt, ops); } case 4: /* jmp abs */ c->eip = c->src.val; break; case 6: /* push */ - emulate_push(ctxt, ops); - break; + return emulate_push(ctxt, ops); } return X86EMUL_CONTINUE; } @@ -2503,7 +2554,7 @@ static int emulator_do_task_switch(struct x86_emulate_ctxt *ctxt, c->op_bytes = c->ad_bytes = (next_tss_desc.type & 8) ? 4 : 2; c->lock_prefix = 0; c->src.val = (unsigned long) error_code; - emulate_push(ctxt, ops); + ret = emulate_push(ctxt, ops); } return ret; @@ -2636,7 +2687,9 @@ special_insn: emulate_2op_SrcV("add", c->src, c->dst, ctxt->eflags); break; case 0x06: /* push es */ - emulate_push_sreg(ctxt, ops, VCPU_SREG_ES); + rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_ES); + if (rc != X86EMUL_CONTINUE) + goto done; break; case 0x07: /* pop es */ rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_ES); @@ -2648,14 +2701,18 @@ special_insn: emulate_2op_SrcV("or", c->src, c->dst, ctxt->eflags); break; case 0x0e: /* push cs */ - emulate_push_sreg(ctxt, ops, VCPU_SREG_CS); + rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_CS); + if (rc != X86EMUL_CONTINUE) + goto done; break; case 0x10 ... 0x15: adc: /* adc */ emulate_2op_SrcV("adc", c->src, c->dst, ctxt->eflags); break; case 0x16: /* push ss */ - emulate_push_sreg(ctxt, ops, VCPU_SREG_SS); + rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_SS); + if (rc != X86EMUL_CONTINUE) + goto done; break; case 0x17: /* pop ss */ rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_SS); @@ -2667,7 +2724,9 @@ special_insn: emulate_2op_SrcV("sbb", c->src, c->dst, ctxt->eflags); break; case 0x1e: /* push ds */ - emulate_push_sreg(ctxt, ops, VCPU_SREG_DS); + rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_DS); + if (rc != X86EMUL_CONTINUE) + goto done; break; case 0x1f: /* pop ds */ rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_DS); @@ -2697,7 +2756,9 @@ special_insn: emulate_1op("dec", c->dst, ctxt->eflags); break; case 0x50 ... 0x57: /* push reg */ - emulate_push(ctxt, ops); + rc = emulate_push(ctxt, ops); + if (rc != X86EMUL_CONTINUE) + goto done; break; case 0x58 ... 0x5f: /* pop reg */ pop_instruction: @@ -2722,7 +2783,9 @@ special_insn: break; case 0x68: /* push imm */ case 0x6a: /* push imm8 */ - emulate_push(ctxt, ops); + rc = emulate_push(ctxt, ops); + if (rc != X86EMUL_CONTINUE) + goto done; break; case 0x6c: /* insb */ case 0x6d: /* insw/insd */ @@ -2850,7 +2913,9 @@ special_insn: goto xchg; case 0x9c: /* pushf */ c->src.val = (unsigned long) ctxt->eflags; - emulate_push(ctxt, ops); + rc = emulate_push(ctxt, ops); + if (rc != X86EMUL_CONTINUE) + goto done; break; case 0x9d: /* popf */ c->dst.type = OP_REG; @@ -2920,7 +2985,9 @@ special_insn: long int rel = c->src.val; c->src.val = (unsigned long) c->eip; jmp_rel(c, rel); - emulate_push(ctxt, ops); + rc = emulate_push(ctxt, ops); + if (rc != X86EMUL_CONTINUE) + goto done; break; } case 0xe9: /* jmp rel */ @@ -3245,7 +3312,9 @@ twobyte_insn: c->dst.type = OP_NONE; break; case 0xa0: /* push fs */ - emulate_push_sreg(ctxt, ops, VCPU_SREG_FS); + rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_FS); + if (rc != X86EMUL_CONTINUE) + goto done; break; case 0xa1: /* pop fs */ rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_FS); @@ -3264,7 +3333,9 @@ twobyte_insn: emulate_2op_cl("shld", c->src2, c->src, c->dst, ctxt->eflags); break; case 0xa8: /* push gs */ - emulate_push_sreg(ctxt, ops, VCPU_SREG_GS); + rc = emulate_push_sreg(ctxt, ops, VCPU_SREG_GS); + if (rc != X86EMUL_CONTINUE) + goto done; break; case 0xa9: /* pop gs */ rc = emulate_pop_sreg(ctxt, ops, VCPU_SREG_GS);