Skip to content

Commit 8686a0e

Browse files
committed
Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
* 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86: Fix insn decoder for longer instruction
2 parents 7115e3f + 53a019a commit 8686a0e

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

‎arch/x86/lib/insn.c‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,23 @@
2222
#include <asm/inat.h>
2323
#include <asm/insn.h>
2424

25-
#define get_next(t, insn) \
26-
({t r; r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); r; })
25+
/* Verify next sizeof(t) bytes can be on the same instruction */
26+
#define validate_next(t, insn, n) \
27+
((insn)->next_byte + sizeof(t) + n - (insn)->kaddr <= MAX_INSN_SIZE)
28+
29+
#define __get_next(t, insn) \
30+
({ t r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); r; })
31+
32+
#define __peek_nbyte_next(t, insn, n) \
33+
({ t r = *(t*)((insn)->next_byte + n); r; })
2734

28-
#define peek_next(t, insn) \
29-
({t r; r = *(t*)insn->next_byte; r; })
35+
#define get_next(t, insn) \
36+
({ if (unlikely(!validate_next(t, insn, 0))) goto err_out; __get_next(t, insn); })
3037

3138
#define peek_nbyte_next(t, insn, n) \
32-
({t r; r = *(t*)((insn)->next_byte + n); r; })
39+
({ if (unlikely(!validate_next(t, insn, n))) goto err_out; __peek_nbyte_next(t, insn, n); })
40+
41+
#define peek_next(t, insn) peek_nbyte_next(t, insn, 0)
3342

3443
/**
3544
* insn_init() - initialize struct insn
@@ -158,6 +167,8 @@ void insn_get_prefixes(struct insn *insn)
158167
insn->vex_prefix.got = 1;
159168

160169
prefixes->got = 1;
170+
171+
err_out:
161172
return;
162173
}
163174

@@ -208,6 +219,9 @@ void insn_get_opcode(struct insn *insn)
208219
insn->attr = 0; /* This instruction is bad */
209220
end:
210221
opcode->got = 1;
222+
223+
err_out:
224+
return;
211225
}
212226

213227
/**
@@ -241,6 +255,9 @@ void insn_get_modrm(struct insn *insn)
241255
if (insn->x86_64 && inat_is_force64(insn->attr))
242256
insn->opnd_bytes = 8;
243257
modrm->got = 1;
258+
259+
err_out:
260+
return;
244261
}
245262

246263

@@ -290,6 +307,9 @@ void insn_get_sib(struct insn *insn)
290307
}
291308
}
292309
insn->sib.got = 1;
310+
311+
err_out:
312+
return;
293313
}
294314

295315

@@ -351,6 +371,9 @@ void insn_get_displacement(struct insn *insn)
351371
}
352372
out:
353373
insn->displacement.got = 1;
374+
375+
err_out:
376+
return;
354377
}
355378

356379
/* Decode moffset16/32/64 */
@@ -373,6 +396,9 @@ static void __get_moffset(struct insn *insn)
373396
break;
374397
}
375398
insn->moffset1.got = insn->moffset2.got = 1;
399+
400+
err_out:
401+
return;
376402
}
377403

378404
/* Decode imm v32(Iz) */
@@ -389,6 +415,9 @@ static void __get_immv32(struct insn *insn)
389415
insn->immediate.nbytes = 4;
390416
break;
391417
}
418+
419+
err_out:
420+
return;
392421
}
393422

394423
/* Decode imm v64(Iv/Ov) */
@@ -411,6 +440,9 @@ static void __get_immv(struct insn *insn)
411440
break;
412441
}
413442
insn->immediate1.got = insn->immediate2.got = 1;
443+
444+
err_out:
445+
return;
414446
}
415447

416448
/* Decode ptr16:16/32(Ap) */
@@ -432,6 +464,9 @@ static void __get_immptr(struct insn *insn)
432464
insn->immediate2.value = get_next(unsigned short, insn);
433465
insn->immediate2.nbytes = 2;
434466
insn->immediate1.got = insn->immediate2.got = 1;
467+
468+
err_out:
469+
return;
435470
}
436471

437472
/**
@@ -496,6 +531,9 @@ void insn_get_immediate(struct insn *insn)
496531
}
497532
done:
498533
insn->immediate.got = 1;
534+
535+
err_out:
536+
return;
499537
}
500538

501539
/**

0 commit comments

Comments
 (0)