Skip to contentSkip to conversation

x86/its: Account for CS prefixes to indirect branches

PATCH New Pawan Gupta 1 participant 1 message Nov 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/its: Account for CS prefixes to indirect branches

cpu_wants_indirect_its_thunk_at() finds the last byte of an indirect branch as addr + 1 + reg / 8. This assumes the branch has no prefix other than REX.B. But since commit 4a1e02b15ac1 ("x86,retpoline:
Optimize patch_retpoline()"), emit_indirect() pads an indirect CALL with up to three CS prefixes. These prefixes move the end of the branch by up to three bytes, and the ITS check looks at the wrong byte. A CALL that ends in the lower half of a cacheline because of the CS prefixes does not get the ITS thunk.

ITS mitigation must use the same CS prefix logic as emit_indirect() to find the correct end of the branch. But emit_indirect() also adds INT3 padding for SLS.

Move the encoding of the branch itself into pre_emit_indirect_unsafe() and keep the SLS INT3 padding in emit_indirect(). The CS prefix logic does not change. Let ITS mitigation call pre_emit_indirect_unsafe() to get the actual end of the branch.

The new pre-emit version must not be used directly for patching, as it leaves out the SLS guard.

Fixes 4a1e02b15ac1 ("x86,retpoline: Optimize patch_retpoline()")
Cc [email protected] # 6.18.x
---
 arch/x86/kernel/alternative.c | 62 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 47 insertions(+), 15 deletions(-)
arch/x86/kernel/alternative.c +47 -15
index efbc11f8c336..16e0bce7fd6e 100644
@@ -721,11 +721,15 @@ static inline bool is_jcc32(struct insn *insn)
 #if defined(CONFIG_MITIGATION_RETPOLINE) && defined(CONFIG_OBJTOOL)
 
 /*
- * [CS]{,3} CALL/JMP *%\reg [INT3]*
+ * This only encodes the indirect branch without the INT3 padding required for
+ * SLS. Therefore, it is unsafe to use this for patching directly. ITS
+ * mitigation uses this to identify the branches that require mitigation.
+ *
+ * [CS]{,3} CALL/JMP *%\reg
  */
-static int emit_indirect(int op, int reg, u8 *bytes, int len)
+static int pre_emit_indirect_unsafe(int op, int reg, u8 *bytes, int len)
 {
-	int cs = 0, bp = 0;
+	int cs = 0;
 	int i = 0;
 	u8 modrm;
 
@@ -747,7 +751,6 @@ static int emit_indirect(int op, int reg, u8 *bytes, int len)
 
 	case JMP32_INSN_OPCODE:
 		modrm = 0x20; /* Reg = 4; JMP r/m */
-		bp = len;
 		break;
 
 	default:
@@ -769,8 +772,24 @@ static int emit_indirect(int op, int reg, u8 *bytes, int len)
 	bytes[i++] = 0xff; /* opcode */
 	bytes[i++] = modrm;
 
-	while (bp--)
-		bytes[i++] = 0xcc; /* INT3 */
+	return i;
+}
+
+/*
+ * pre_emit_indirect_unsafe() + [INT3]*
+ */
+static int emit_indirect(int op, int reg, u8 *bytes, int len)
+{
+	int i = pre_emit_indirect_unsafe(op, reg, bytes, len);
+
+	if (i < 0)
+		return i;
+
+	/* Pad INT3 to tail calls against SLS */
+	if (op == JMP32_INSN_OPCODE) {
+		while (i < len)
+			bytes[i++] = 0xcc; /* INT3 */
+	}
 
 	return i;
 }
@@ -840,13 +859,23 @@ static int emit_its_trampoline(void *addr, struct insn *insn, int reg, u8 *bytes
 }
 
 /* Check if an indirect branch is at ITS-unsafe address */
-static bool cpu_wants_indirect_its_thunk_at(unsigned long addr, int reg)
+static bool cpu_wants_indirect_its_thunk_at(int op, int reg, int len,
+					    unsigned long addr)
 {
+	u8 bytes[16];
+	int ind_len;
+
 	if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS))
 		return false;
 
-	/* Indirect branch opcode is 2 or 3 bytes depending on reg */
-	addr += 1 + reg / 8;
+	if (WARN_ON_ONCE(len > sizeof(bytes)))
+		return false;
+
+	ind_len = pre_emit_indirect_unsafe(op, reg, bytes, len);
+	if (ind_len <= 0)
+		return false;
+
+	addr += ind_len - 1;
 
 	/* Lower-half of the cacheline? */
 	return !(addr & 0x20);
@@ -854,7 +883,8 @@ static bool cpu_wants_indirect_its_thunk_at(unsigned long addr, int reg)
 #else /* CONFIG_MITIGATION_ITS */
 
 #ifdef CONFIG_FINEIBT
-static bool cpu_wants_indirect_its_thunk_at(unsigned long addr, int reg)
+static bool cpu_wants_indirect_its_thunk_at(int op, int reg, int len,
+					    unsigned long addr)
 {
 	return false;
 }
@@ -941,7 +971,7 @@ static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes)
 	 * Check if the address of last byte of emitted-indirect is in
 	 * lower-half of the cacheline. Such branches need ITS mitigation.
 	 */
-	if (cpu_wants_indirect_its_thunk_at((unsigned long)addr + i, reg))
+	if (cpu_wants_indirect_its_thunk_at(op, reg, insn->length - i, (unsigned long)addr + i))
 		return emit_its_trampoline(addr, insn, reg, bytes);
 #endif
 
@@ -1471,6 +1501,7 @@ extern u8 fineibt_paranoid_end[];
 
 #define fineibt_paranoid_size (fineibt_paranoid_end - fineibt_paranoid_start)
 #define fineibt_paranoid_ind  (fineibt_paranoid_ind - fineibt_paranoid_start)
+#define fineibt_paranoid_ind_size (fineibt_paranoid_size - fineibt_paranoid_ind)
 #define fineibt_paranoid_ud   0xd
 
 static u32 decode_preamble_hash(void *addr, int *reg)
@@ -1772,13 +1803,14 @@ static int cfi_rewrite_callers(s32 *start, s32 *end)
 		memcpy(bytes, fineibt_paranoid_start, fineibt_paranoid_size);
 		memcpy(bytes + fineibt_caller_hash, &hash, 4);
 
-		if (cpu_wants_indirect_its_thunk_at((unsigned long)addr + fineibt_paranoid_ind, 11)) {
+		if (cpu_wants_indirect_its_thunk_at(op, 11, fineibt_paranoid_ind_size,
+						    (unsigned long)addr + fineibt_paranoid_ind)) {
 			emit_paranoid_trampoline(addr + fineibt_caller_size,
 						 &insn, 11, bytes + fineibt_caller_size);
 		} else {
-			int len = fineibt_paranoid_size - fineibt_paranoid_ind;
-			ret = emit_indirect(op, 11, bytes + fineibt_paranoid_ind, len);
-			if (WARN_ON_ONCE(ret != len))
+			ret = emit_indirect(op, 11, bytes + fineibt_paranoid_ind,
+					    fineibt_paranoid_ind_size);
+			if (WARN_ON_ONCE(ret != fineibt_paranoid_ind_size))
 				continue;
 		}
 

---
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
change-id: 20261124-its-cs-prefix-dce5195bf5fc

Best regards,

Signature · 1 line
Pawan