Skip to content

Commit

Permalink
x86: Handle failures of parsing immediate operands in the instruction…
Browse files Browse the repository at this point in the history
… decoder

This can happen if the instruction is much longer than the maximum length,
or if insn->opnd_bytes is manually changed.

This patch also fixes warnings from -Wswitch-default flag.

Reported-by: Prashanth Nageshappa <[email protected]>
Signed-off-by: Masami Hiramatsu <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Ananth N Mavinakayanahalli <[email protected]>
Cc: Jim Keniston <[email protected]>
Cc: Linux-mm <[email protected]>
Cc: Oleg Nesterov <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Anton Arapov <[email protected]>
Cc: Srikar Dronamraju <[email protected]>
Cc: [email protected]
Cc: Peter Zijlstra <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
mhiramathitachi authored and Ingo Molnar committed Apr 16, 2012
1 parent 7ea6411 commit 6c7b8e8
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions arch/x86/lib/insn.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ void insn_get_displacement(struct insn *insn)
return;
}

/* Decode moffset16/32/64 */
static void __get_moffset(struct insn *insn)
/* Decode moffset16/32/64. Return 0 if failed */
static int __get_moffset(struct insn *insn)
{
switch (insn->addr_bytes) {
case 2:
Expand All @@ -397,15 +397,19 @@ static void __get_moffset(struct insn *insn)
insn->moffset2.value = get_next(int, insn);
insn->moffset2.nbytes = 4;
break;
default: /* opnd_bytes must be modified manually */
goto err_out;
}
insn->moffset1.got = insn->moffset2.got = 1;

return 1;

err_out:
return;
return 0;
}

/* Decode imm v32(Iz) */
static void __get_immv32(struct insn *insn)
/* Decode imm v32(Iz). Return 0 if failed */
static int __get_immv32(struct insn *insn)
{
switch (insn->opnd_bytes) {
case 2:
Expand All @@ -417,14 +421,18 @@ static void __get_immv32(struct insn *insn)
insn->immediate.value = get_next(int, insn);
insn->immediate.nbytes = 4;
break;
default: /* opnd_bytes must be modified manually */
goto err_out;
}

return 1;

err_out:
return;
return 0;
}

/* Decode imm v64(Iv/Ov) */
static void __get_immv(struct insn *insn)
/* Decode imm v64(Iv/Ov), Return 0 if failed */
static int __get_immv(struct insn *insn)
{
switch (insn->opnd_bytes) {
case 2:
Expand All @@ -441,15 +449,18 @@ static void __get_immv(struct insn *insn)
insn->immediate2.value = get_next(int, insn);
insn->immediate2.nbytes = 4;
break;
default: /* opnd_bytes must be modified manually */
goto err_out;
}
insn->immediate1.got = insn->immediate2.got = 1;

return 1;
err_out:
return;
return 0;
}

/* Decode ptr16:16/32(Ap) */
static void __get_immptr(struct insn *insn)
static int __get_immptr(struct insn *insn)
{
switch (insn->opnd_bytes) {
case 2:
Expand All @@ -462,14 +473,17 @@ static void __get_immptr(struct insn *insn)
break;
case 8:
/* ptr16:64 is not exist (no segment) */
return;
return 0;
default: /* opnd_bytes must be modified manually */
goto err_out;
}
insn->immediate2.value = get_next(unsigned short, insn);
insn->immediate2.nbytes = 2;
insn->immediate1.got = insn->immediate2.got = 1;

return 1;
err_out:
return;
return 0;
}

/**
Expand All @@ -489,7 +503,8 @@ void insn_get_immediate(struct insn *insn)
insn_get_displacement(insn);

if (inat_has_moffset(insn->attr)) {
__get_moffset(insn);
if (!__get_moffset(insn))
goto err_out;
goto done;
}

Expand Down Expand Up @@ -517,16 +532,20 @@ void insn_get_immediate(struct insn *insn)
insn->immediate2.nbytes = 4;
break;
case INAT_IMM_PTR:
__get_immptr(insn);
if (!__get_immptr(insn))
goto err_out;
break;
case INAT_IMM_VWORD32:
__get_immv32(insn);
if (!__get_immv32(insn))
goto err_out;
break;
case INAT_IMM_VWORD:
__get_immv(insn);
if (!__get_immv(insn))
goto err_out;
break;
default:
break;
/* Here, insn must have an immediate, but failed */
goto err_out;
}
if (inat_has_second_immediate(insn->attr)) {
insn->immediate2.value = get_next(char, insn);
Expand Down

0 comments on commit 6c7b8e8

Please sign in to comment.