public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: Peter Zijlstra <peterz@infradead.org>
Cc: x86@kernel.org, Masami Hiramatsu <mhiramat@kernel.org>,
	"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
	Andrew Cooper <Andrew.Cooper3@citrix.com>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [RFC] x86/alternative: Support relocations in alternatives
Date: Fri, 3 Feb 2023 17:46:47 +0100	[thread overview]
Message-ID: <Y906d8b6xhC17AAV@zn.tnic> (raw)
In-Reply-To: <Y90wk4FVBtmFJZ4s@zn.tnic>

On Fri, Feb 03, 2023 at 05:04:35PM +0100, Borislav Petkov wrote:
> Rest on IRC. :)

Here's what I'm thinking. It still fails somewhere while booting so it
is not good yet but the idea is to show what I mean:

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index 0eff4033eea4..2ede814de089 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -208,14 +208,23 @@ apply_reloc(int n, void *ptr, uintptr_t diff)
 	}
 }
 
+static void __always_inline apply_new_offset(int immed_size, void *ptr, s32 new_offset)
+{
+	switch (immed_size) {
+	case 1:	*(s8 *)ptr  = (s8) new_offset; break;
+	case 2: *(s16 *)ptr = (s16)new_offset; break;
+	case 4: *(s32 *)ptr =      new_offset; break;
+	default: BUG();
+	}
+}
 static void __init_or_module noinline
-apply_relocation(u8 *instr, size_t len, u8 *dest, u8 *src, size_t src_len)
+apply_relocation(u8 *insn_buff, size_t len, u8 *instr, u8 *repl, size_t repl_len)
 {
 	struct insn insn;
 	int i = 0;
 
 	for (;;) {
-		if (insn_decode_kernel(&insn, &instr[i]))
+		if (insn_decode_kernel(&insn, &insn_buff[i]))
 			return;
 
 		switch (insn.opcode.bytes[0]) {
@@ -229,20 +238,27 @@ apply_relocation(u8 *instr, size_t len, u8 *dest, u8 *src, size_t src_len)
 		case JMP8_INSN_OPCODE:
 		case JMP32_INSN_OPCODE:
 		case CALL_INSN_OPCODE:
-			u8 *target = src + i + insn.length + insn.immediate.value;
-			if (target < src || target > src + src_len) {
-				apply_reloc(insn.immediate.nbytes,
-					    instr + i + insn_offset_immediate(&insn),
-					    src - dest);
-			}
+			u8 *target = repl + i + insn.length + insn.immediate.value;
+			u8 *next_rip = instr + insn.length;
+			s32 new_offset;
+
+			if (instr < target)
+				new_offset = target - next_rip;
+			else
+				new_offset = next_rip - target;
+
+			apply_new_offset(insn.immediate.nbytes,
+					 insn_buff + i + insn_offset_immediate(&insn),
+					 new_offset);
+
 		}
 
 		if (insn_rip_relative(&insn)) {

-- 
Regards/Gruss,
    Boris.

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

  reply	other threads:[~2023-02-03 16:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-01 14:10 [RFC] x86/alternative: Support relocations in alternatives Peter Zijlstra
2023-02-01 17:02 ` Kirill A. Shutemov
2023-02-03 13:13 ` Borislav Petkov
2023-02-03 15:04   ` Peter Zijlstra
2023-02-03 15:12     ` Peter Zijlstra
2023-02-03 16:04     ` Borislav Petkov
2023-02-03 16:46       ` Borislav Petkov [this message]
2023-02-03 23:25         ` Borislav Petkov
2023-02-06 14:29 ` Borislav Petkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y906d8b6xhC17AAV@zn.tnic \
    --to=bp@alien8.de \
    --cc=Andrew.Cooper3@citrix.com \
    --cc=jpoimboe@redhat.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=peterz@infradead.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox