Skip to content

Commit c43a43e

Browse files
Peter Zijlstraingomolnar
authored andcommitted
x86/alternatives: Teach text_poke_bp() to emulate RET
Future patches will need to poke a RET instruction, provide the infrastructure required for this. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Cc: Masami Hiramatsu <mhiramat@kernel.org> Link: https://lore.kernel.org/r/20200818135804.982214828@infradead.org
1 parent f03c412 commit c43a43e

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

arch/x86/include/asm/text-patching.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ extern void text_poke_finish(void);
5353
#define INT3_INSN_SIZE 1
5454
#define INT3_INSN_OPCODE 0xCC
5555

56+
#define RET_INSN_SIZE 1
57+
#define RET_INSN_OPCODE 0xC3
58+
5659
#define CALL_INSN_SIZE 5
5760
#define CALL_INSN_OPCODE 0xE8
5861

@@ -73,6 +76,7 @@ static __always_inline int text_opcode_size(u8 opcode)
7376

7477
switch(opcode) {
7578
__CASE(INT3);
79+
__CASE(RET);
7680
__CASE(CALL);
7781
__CASE(JMP32);
7882
__CASE(JMP8);
@@ -140,12 +144,27 @@ void int3_emulate_push(struct pt_regs *regs, unsigned long val)
140144
*(unsigned long *)regs->sp = val;
141145
}
142146

147+
static __always_inline
148+
unsigned long int3_emulate_pop(struct pt_regs *regs)
149+
{
150+
unsigned long val = *(unsigned long *)regs->sp;
151+
regs->sp += sizeof(unsigned long);
152+
return val;
153+
}
154+
143155
static __always_inline
144156
void int3_emulate_call(struct pt_regs *regs, unsigned long func)
145157
{
146158
int3_emulate_push(regs, regs->ip - INT3_INSN_SIZE + CALL_INSN_SIZE);
147159
int3_emulate_jmp(regs, func);
148160
}
161+
162+
static __always_inline
163+
void int3_emulate_ret(struct pt_regs *regs)
164+
{
165+
unsigned long ip = int3_emulate_pop(regs);
166+
int3_emulate_jmp(regs, ip);
167+
}
149168
#endif /* !CONFIG_UML_X86 */
150169

151170
#endif /* _ASM_X86_TEXT_PATCHING_H */

arch/x86/kernel/alternative.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,10 @@ noinstr int poke_int3_handler(struct pt_regs *regs)
11031103
*/
11041104
goto out_put;
11051105

1106+
case RET_INSN_OPCODE:
1107+
int3_emulate_ret(regs);
1108+
break;
1109+
11061110
case CALL_INSN_OPCODE:
11071111
int3_emulate_call(regs, (long)ip + tp->rel32);
11081112
break;
@@ -1277,6 +1281,7 @@ static void text_poke_loc_init(struct text_poke_loc *tp, void *addr,
12771281

12781282
switch (tp->opcode) {
12791283
case INT3_INSN_OPCODE:
1284+
case RET_INSN_OPCODE:
12801285
break;
12811286

12821287
case CALL_INSN_OPCODE:

0 commit comments

Comments
 (0)