Re: [PATCH v4 2/5] x86/alternative: add indirect call patching

From: Juergen Gross
Date: Tue Nov 14 2023 - 07:56:44 EST


On 14.11.23 13:37, Borislav Petkov wrote:
On Mon, Oct 30, 2023 at 03:25:05PM +0100, Juergen Gross wrote:
+ * Rewrite the "call BUG_func" replacement to point to the target of the
+ * indirect pv_ops call "call *disp(%ip)".
+ */
+static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a)
+{
+ void *target, *bug = &BUG_func;
+
+ if (a->replacementlen != 5 || insn_buff[0] != CALL_INSN_OPCODE) {
+ pr_err("Alternative: ALT_FLAG_CALL set for a non-call replacement instruction\n");

No need for the printk prefix.

Okay.


+ pr_err(" Ignoring the flag for the instruction at %pS (%px)\n", instr, instr);

No ignoring - BUG

Okay.


+ return 5;
+ }
+
+ if (a->instrlen != 6 || instr[0] != 0xff || instr[1] != 0x15) {
+ pr_err("Alternative: ALT_FLAG_CALL set for unrecognized indirect call\n");
+ pr_err(" Not replacing the instruction at %pS (%px)\n", instr, instr);
+ return -1;

Ditto.

Okay.


+ }
+
+#ifdef CONFIG_X86_64
+ /* ff 15 00 00 00 00 call *0x0(%rip) */
+ target = *(void **)(instr + a->instrlen + *(s32 *)(instr + 2));
+#else
+ /* ff 15 00 00 00 00 call *0x0 */
+ target = *(void **)(*(s32 *)(instr + 2));
+#endif
+ if (!target)
+ target = bug;
+
+ /* (BUG_func - .) + (target - BUG_func) := target - . */
+ *(s32 *)(insn_buff + 1) += target - bug;

If I squint hard enough, this looks like it is replacing one call with
another. We have a C macro alternative_call() which does exactly that.
Why can't you define an asm version ALTERNATIVE_CALL and use it
instead of using adding a new flag? We have 16 possible ones in total.

It is replacing an _indirect_ call with a _direct_ one, taking the call target
from the pointer used by the indirect call.


Juergen

Attachment: OpenPGP_0xB0DE9DD628BF132F.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature