From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xen.org>
Cc: George Dunlap <george.dunlap@eu.citrix.com>,
Andrew Cooper <andrew.cooper3@citrix.com>,
Ian Jackson <Ian.Jackson@eu.citrix.com>,
Wei Liu <wei.liu2@citrix.com>, Jan Beulich <JBeulich@suse.com>
Subject: [PATCH v2 for-4.9 5/7] tools/insn-fuzz: Correct hook prototypes, and assert() appropriate segments
Date: Wed, 5 Apr 2017 18:53:31 +0100 [thread overview]
Message-ID: <1491414813-30003-6-git-send-email-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <1491414813-30003-1-git-send-email-andrew.cooper3@citrix.com>
The correct prototypes for the hooks are to use enum x86_segment rather than
unsigned int. It is implementation specific as to whether this compiles.
assert() that the emulator never passes an inappropriate segment. The only
hook which may legitimately be passed x86_seg_none is invlpg().
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: George Dunlap <george.dunlap@eu.citrix.com>
CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
CC: Wei Liu <wei.liu2@citrix.com>
---
tools/fuzz/x86_instruction_emulator/fuzz-emul.c | 45 ++++++++++++++++++++-----
tools/tests/x86_emulator/test_x86_emulator.c | 8 ++---
2 files changed, 41 insertions(+), 12 deletions(-)
diff --git a/tools/fuzz/x86_instruction_emulator/fuzz-emul.c b/tools/fuzz/x86_instruction_emulator/fuzz-emul.c
index a20212e..fedeb9f 100644
--- a/tools/fuzz/x86_instruction_emulator/fuzz-emul.c
+++ b/tools/fuzz/x86_instruction_emulator/fuzz-emul.c
@@ -117,12 +117,15 @@ static int data_read(struct x86_emulate_ctxt *ctxt,
}
static int fuzz_read(
- unsigned int seg,
+ enum x86_segment seg,
unsigned long offset,
void *p_data,
unsigned int bytes,
struct x86_emulate_ctxt *ctxt)
{
+ /* Reads expected for all user and system segments. */
+ assert((unsigned int)seg < x86_seg_none);
+
return data_read(ctxt, "read", p_data, bytes);
}
@@ -136,12 +139,14 @@ static int fuzz_read_io(
}
static int fuzz_insn_fetch(
- unsigned int seg,
+ enum x86_segment seg,
unsigned long offset,
void *p_data,
unsigned int bytes,
struct x86_emulate_ctxt *ctxt)
{
+ assert(seg == x86_seg_cs);
+
/*
* Zero-length instruction fetches are made at the destination of jumps,
* to perform segmentation checks. No data needs returning.
@@ -211,6 +216,8 @@ static int fuzz_rep_ins(
unsigned long *reps,
struct x86_emulate_ctxt *ctxt)
{
+ assert(dst_seg == x86_seg_es);
+
return _fuzz_rep_read(ctxt, "rep_ins", reps);
}
@@ -223,6 +230,9 @@ static int fuzz_rep_movs(
unsigned long *reps,
struct x86_emulate_ctxt *ctxt)
{
+ assert(is_x86_user_segment(src_seg));
+ assert(dst_seg == x86_seg_es);
+
return _fuzz_rep_read(ctxt, "rep_movs", reps);
}
@@ -234,6 +244,8 @@ static int fuzz_rep_outs(
unsigned long *reps,
struct x86_emulate_ctxt *ctxt)
{
+ assert(is_x86_user_segment(src_seg));
+
return _fuzz_rep_write(ctxt, "rep_outs", reps);
}
@@ -245,27 +257,43 @@ static int fuzz_rep_stos(
unsigned long *reps,
struct x86_emulate_ctxt *ctxt)
{
+ /*
+ * STOS itself may only have an %es segment, but the stos() hook is reused
+ * for CLZERO.
+ */
+ assert(is_x86_user_segment(seg));
+
return _fuzz_rep_write(ctxt, "rep_stos", reps);
}
static int fuzz_write(
- unsigned int seg,
+ enum x86_segment seg,
unsigned long offset,
void *p_data,
unsigned int bytes,
struct x86_emulate_ctxt *ctxt)
{
+ /* Writes not expected for any system segments. */
+ assert(is_x86_user_segment(seg));
+
return maybe_fail(ctxt, "write", true);
}
static int fuzz_cmpxchg(
- unsigned int seg,
+ enum x86_segment seg,
unsigned long offset,
void *old,
void *new,
unsigned int bytes,
struct x86_emulate_ctxt *ctxt)
{
+ /*
+ * Cmpxchg expected for user segments, and setting accessed/busy bits in
+ * GDT/LDT enties, but not expected for any IDT or TR accesses.
+ */
+ assert(is_x86_user_segment(seg) ||
+ seg == x86_seg_gdtr || seg == x86_seg_ldtr);
+
return maybe_fail(ctxt, "cmpxchg", true);
}
@@ -274,6 +302,9 @@ static int fuzz_invlpg(
unsigned long offset,
struct x86_emulate_ctxt *ctxt)
{
+ /* invlpg(), unlike all other hooks, may be called with x86_seg_none. */
+ assert((unsigned int)seg <= x86_seg_none);
+
return maybe_fail(ctxt, "invlpg", false);
}
@@ -300,8 +331,7 @@ static int fuzz_read_segment(
const struct fuzz_state *s = ctxt->data;
const struct fuzz_corpus *c = s->corpus;
- if ( seg >= SEG_NUM )
- return X86EMUL_UNHANDLEABLE;
+ assert((unsigned int)seg < x86_seg_none);
*reg = c->segments[seg];
@@ -317,8 +347,7 @@ static int fuzz_write_segment(
struct fuzz_corpus *c = s->corpus;
int rc;
- if ( seg >= SEG_NUM )
- return X86EMUL_UNHANDLEABLE;
+ assert((unsigned int)seg < x86_seg_none);
rc = maybe_fail(ctxt, "write_segment", true);
diff --git a/tools/tests/x86_emulator/test_x86_emulator.c b/tools/tests/x86_emulator/test_x86_emulator.c
index efeb175..1992c3f 100644
--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -155,7 +155,7 @@ static const struct {
static unsigned int bytes_read;
static int read(
- unsigned int seg,
+ enum x86_segment seg,
unsigned long offset,
void *p_data,
unsigned int bytes,
@@ -210,7 +210,7 @@ static int read(
}
static int fetch(
- unsigned int seg,
+ enum x86_segment seg,
unsigned long offset,
void *p_data,
unsigned int bytes,
@@ -224,7 +224,7 @@ static int fetch(
}
static int write(
- unsigned int seg,
+ enum x86_segment seg,
unsigned long offset,
void *p_data,
unsigned int bytes,
@@ -240,7 +240,7 @@ static int write(
}
static int cmpxchg(
- unsigned int seg,
+ enum x86_segment seg,
unsigned long offset,
void *old,
void *new,
--
2.1.4
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel
next prev parent reply other threads:[~2017-04-05 17:53 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-05 17:53 [PATCH v2 for-4.9 0/7] x86/emul: Userspace fuzzing harness fixes Andrew Cooper
2017-04-05 17:53 ` [PATCH v2 for-4.9 1/7] MAINTAINERS: Move the x86 instruction emulator under x86 maintainership Andrew Cooper
2017-04-06 9:13 ` Wei Liu
2017-04-06 11:00 ` Ian Jackson
2017-04-05 17:53 ` [PATCH v2 for-4.9 2/7] tools/insn-fuzz: Don't hit memcpy() for zero-length reads Andrew Cooper
2017-04-06 9:22 ` Jan Beulich
2017-04-05 17:53 ` [PATCH v2 for-4.9 3/7] tools/insn-fuzz: Avoid making use of static data Andrew Cooper
2017-04-05 17:53 ` [PATCH v2 for-4.9 4/7] tools/insn-fuzz: Fix a stability bug in afl-clang-fast mode Andrew Cooper
2017-04-05 17:53 ` Andrew Cooper [this message]
2017-04-06 9:28 ` [PATCH v2 for-4.9 5/7] tools/insn-fuzz: Correct hook prototypes, and assert() appropriate segments Jan Beulich
2017-04-05 17:53 ` [PATCH v2 for-4.9 6/7] tools/insn-fuzz: Provide IA32_DEBUGCTL consistently to the emulator Andrew Cooper
2017-04-05 17:53 ` [PATCH v2 for-4.9 7/7] tools/insn-fuzz: Fix assertion failures in x86_emulate_wrapper() Andrew Cooper
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=1491414813-30003-6-git-send-email-andrew.cooper3@citrix.com \
--to=andrew.cooper3@citrix.com \
--cc=Ian.Jackson@eu.citrix.com \
--cc=JBeulich@suse.com \
--cc=george.dunlap@eu.citrix.com \
--cc=wei.liu2@citrix.com \
--cc=xen-devel@lists.xen.org \
/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).