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

From: Borislav Petkov
Date: Tue Nov 14 2023 - 07:38:01 EST


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.

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

No ignoring - BUG

> + 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.

> + }
> +
> +#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.

Thx.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette