kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Avi Kivity <avi@redhat.com>
To: Mohammed Gamal <m.gamal005@gmail.com>
Cc: mtosatti@redhat.com, kvm@vger.kernel.org
Subject: Re: [RFC PATCH v2 3/3] x86 emulator: Add segment limit checks and helper functions
Date: Sun, 11 Jul 2010 21:07:13 +0300	[thread overview]
Message-ID: <4C3A0851.1030109@redhat.com> (raw)
In-Reply-To: <1278598236-12103-4-git-send-email-m.gamal005@gmail.com>

On 07/08/2010 05:10 PM, Mohammed Gamal wrote:
> This patch adds segment limit checks to the x86 emulator, in addition to some
> helper functions and changes to the return values of emulate_push to accomodate
> the new checks.
>
>
>
> +static u32 seg_limit(struct x86_emulate_ctxt *ctxt,
> +		     struct x86_emulate_ops *ops, int seg)
> +{
> +	if (ctxt->mode == X86EMUL_MODE_PROT64)
> +		return -1;
>    

That doesn't work well for 64 bit.  It returns 4G-1, you want 2^64-1.  
The return type needs to be u64 and the value -1ULL.


> +
> +	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)
>
>   static void emulate_pf(struct x86_emulate_ctxt *ctxt, unsigned long addr,
>   		       int err)
>   {
> @@ -719,6 +761,12 @@ static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
>   {
>   	int rc;
>
> +	/* eip is already relative to CS, so we just check it against the limit */
> +	if (eip>  cs_limit(ctxt, ops) - size - 1) {
>    

What if

    eip = 1
    limit = 3
    size = 8?

> +		emulate_gp(ctxt, 0);
> +		return X86EMUL_PROPAGATE_FAULT;
> +	}
> +
>   	/* x86 instructions are limited to 15 bytes. */
>   	if (eip + size - ctxt->eip>  15)
>   		return X86EMUL_UNHANDLEABLE;
> @@ -1222,6 +1270,11 @@ done_prefixes:
>   		c->src.ptr = (unsigned long *)
>   			register_address(c,  seg_override_base(ctxt, ops, c),
>   					 c->regs[VCPU_REGS_RSI]);
> +		if ((unsigned long)c->src.ptr - seg_override_base(ctxt, ops, c)>
> +			seg_override_limit(ctxt, ops, c) - c->src.bytes - 1) {
> +			emulate_gp(ctxt, 0);
> +			return X86EMUL_PROPAGATE_FAULT;
> +		}
>    

Similar issue.

This code is repeated two often. Best to have a helper for reading and 
writing that accepts the segment ID, which does the limit check (don't 
forget expand-down segments), then the read/write/fetch or #GP/#SS.

That helper can also add the base, so it reduces code elsewhere.  I 
suggest a first patch that introduces the read/write/fetch helpers, and 
a second patch that adds the limit checks.


-- 
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.


      reply	other threads:[~2010-07-11 18:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-08 14:10 [RFC PATCH v2 0/3] Add segment limit checks to emulator Mohammed Gamal
2010-07-08 14:10 ` [RFC PATCH v2 1/3] Add helper methods to get segment limits Mohammed Gamal
2010-07-08 14:10 ` [RFC PATCH v2 2/3] x86 emulator: Add cs_base() helper Mohammed Gamal
2010-07-08 14:10 ` [RFC PATCH v2 3/3] x86 emulator: Add segment limit checks and helper functions Mohammed Gamal
2010-07-11 18:07   ` Avi Kivity [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4C3A0851.1030109@redhat.com \
    --to=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=m.gamal005@gmail.com \
    --cc=mtosatti@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).