Skip to content

Commit 4e9a5ae

Browse files
mhiramatsuryasaimadhu
authored andcommitted
x86/uprobes: Do not use prefixes.nbytes when looping over prefixes.bytes
Since insn.prefixes.nbytes can be bigger than the size of insn.prefixes.bytes[] when a prefix is repeated, the proper check must be insn.prefixes.bytes[i] != 0 and i < 4 instead of using insn.prefixes.nbytes. Introduce a for_each_insn_prefix() macro for this purpose. Debugged by Kees Cook <keescook@chromium.org>. [ bp: Massage commit message, sync with the respective header in tools/ and drop "we". ] Fixes: 2b14449 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints") Reported-by: syzbot+9b64b619f10f19d19a7c@syzkaller.appspotmail.com Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/160697103739.3146288.7437620795200799020.stgit@devnote2
1 parent 8dcc0e1 commit 4e9a5ae

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

arch/x86/include/asm/insn.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
201201
return insn_offset_displacement(insn) + insn->displacement.nbytes;
202202
}
203203

204+
/**
205+
* for_each_insn_prefix() -- Iterate prefixes in the instruction
206+
* @insn: Pointer to struct insn.
207+
* @idx: Index storage.
208+
* @prefix: Prefix byte.
209+
*
210+
* Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
211+
* and the index is stored in @idx (note that this @idx is just for a cursor,
212+
* do not change it.)
213+
* Since prefixes.nbytes can be bigger than 4 if some prefixes
214+
* are repeated, it cannot be used for looping over the prefixes.
215+
*/
216+
#define for_each_insn_prefix(insn, idx, prefix) \
217+
for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
218+
204219
#define POP_SS_OPCODE 0x1f
205220
#define MOV_SREG_OPCODE 0x8e
206221

arch/x86/kernel/uprobes.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,13 @@ static volatile u32 good_2byte_insns[256 / 32] = {
255255

256256
static bool is_prefix_bad(struct insn *insn)
257257
{
258+
insn_byte_t p;
258259
int i;
259260

260-
for (i = 0; i < insn->prefixes.nbytes; i++) {
261+
for_each_insn_prefix(insn, i, p) {
261262
insn_attr_t attr;
262263

263-
attr = inat_get_opcode_attribute(insn->prefixes.bytes[i]);
264+
attr = inat_get_opcode_attribute(p);
264265
switch (attr) {
265266
case INAT_MAKE_PREFIX(INAT_PFX_ES):
266267
case INAT_MAKE_PREFIX(INAT_PFX_CS):
@@ -715,6 +716,7 @@ static const struct uprobe_xol_ops push_xol_ops = {
715716
static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
716717
{
717718
u8 opc1 = OPCODE1(insn);
719+
insn_byte_t p;
718720
int i;
719721

720722
switch (opc1) {
@@ -746,8 +748,8 @@ static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
746748
* Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
747749
* No one uses these insns, reject any branch insns with such prefix.
748750
*/
749-
for (i = 0; i < insn->prefixes.nbytes; i++) {
750-
if (insn->prefixes.bytes[i] == 0x66)
751+
for_each_insn_prefix(insn, i, p) {
752+
if (p == 0x66)
751753
return -ENOTSUPP;
752754
}
753755

tools/arch/x86/include/asm/insn.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ static inline int insn_offset_immediate(struct insn *insn)
201201
return insn_offset_displacement(insn) + insn->displacement.nbytes;
202202
}
203203

204+
/**
205+
* for_each_insn_prefix() -- Iterate prefixes in the instruction
206+
* @insn: Pointer to struct insn.
207+
* @idx: Index storage.
208+
* @prefix: Prefix byte.
209+
*
210+
* Iterate prefix bytes of given @insn. Each prefix byte is stored in @prefix
211+
* and the index is stored in @idx (note that this @idx is just for a cursor,
212+
* do not change it.)
213+
* Since prefixes.nbytes can be bigger than 4 if some prefixes
214+
* are repeated, it cannot be used for looping over the prefixes.
215+
*/
216+
#define for_each_insn_prefix(insn, idx, prefix) \
217+
for (idx = 0; idx < ARRAY_SIZE(insn->prefixes.bytes) && (prefix = insn->prefixes.bytes[idx]) != 0; idx++)
218+
204219
#define POP_SS_OPCODE 0x1f
205220
#define MOV_SREG_OPCODE 0x8e
206221

0 commit comments

Comments
 (0)