lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Y906d8b6xhC17AAV@zn.tnic>
Date:   Fri, 3 Feb 2023 17:46:47 +0100
From:   Borislav Petkov <bp@...en8.de>
To:     Peter Zijlstra <peterz@...radead.org>
Cc:     x86@...nel.org, Masami Hiramatsu <mhiramat@...nel.org>,
        "Kirill A. Shutemov" <kirill.shutemov@...ux.intel.com>,
        Andrew Cooper <Andrew.Cooper3@...rix.com>,
        Josh Poimboeuf <jpoimboe@...hat.com>,
        linux-kernel@...r.kernel.org
Subject: Re: [RFC] x86/alternative: Support relocations in alternatives

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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ