Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: fei.yang@intel.com
To: intel-xe@lists.freedesktop.org
Cc: Peter Zijlstra <peterz@infradead.org>,
	lucas.demarchi@intel.com, stable@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>,
	"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Subject: [Intel-xe] [PATCH 1/1] x86/alternatives: Disable KASAN in apply_alternatives()
Date: Mon, 16 Oct 2023 08:40:24 -0700	[thread overview]
Message-ID: <20231016154025.3358622-2-fei.yang@intel.com> (raw)
In-Reply-To: <20231016154025.3358622-1-fei.yang@intel.com>

From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>

Fei has reported that KASAN triggers during apply_alternatives() on
a 5-level paging machine:

	BUG: KASAN: out-of-bounds in rcu_is_watching()
	Read of size 4 at addr ff110003ee6419a0 by task swapper/0/0
	...
	__asan_load4()
	rcu_is_watching()
	trace_hardirqs_on()
	text_poke_early()
	apply_alternatives()
	...

On machines with 5-level paging, cpu_feature_enabled(X86_FEATURE_LA57)
gets patched. It includes KASAN code, where KASAN_SHADOW_START depends on
__VIRTUAL_MASK_SHIFT, which is defined with cpu_feature_enabled().

KASAN gets confused when apply_alternatives() patches the
KASAN_SHADOW_START users. A test patch that makes KASAN_SHADOW_START
static, by replacing __VIRTUAL_MASK_SHIFT with 56, works around the issue.

Fix it for real by disabling KASAN while the kernel is patching alternatives.

[ mingo: updated the changelog ]

Fixes: 6657fca06e3f ("x86/mm: Allow to boot without LA57 if CONFIG_X86_5LEVEL=y")
Reported-by: Fei Yang <fei.yang@intel.com>
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20231012100424.1456-1-kirill.shutemov@linux.intel.com
(cherry picked from commit d35652a5fc9944784f6f50a5c979518ff8dacf61)
---
 arch/x86/kernel/alternative.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index 517ee01503be..73be3931e4f0 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -403,6 +403,17 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 	u8 insn_buff[MAX_PATCH_LEN];
 
 	DPRINTK(ALT, "alt table %px, -> %px", start, end);
+
+	/*
+	 * In the case CONFIG_X86_5LEVEL=y, KASAN_SHADOW_START is defined using
+	 * cpu_feature_enabled(X86_FEATURE_LA57) and is therefore patched here.
+	 * During the process, KASAN becomes confused seeing partial LA57
+	 * conversion and triggers a false-positive out-of-bound report.
+	 *
+	 * Disable KASAN until the patching is complete.
+	 */
+	kasan_disable_current();
+
 	/*
 	 * The scan order should be from start to end. A later scanned
 	 * alternative code can overwrite previously scanned alternative code.
@@ -452,6 +463,8 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 
 		text_poke_early(instr, insn_buff, insn_buff_sz);
 	}
+
+	kasan_enable_current();
 }
 
 static inline bool is_jcc32(struct insn *insn)
-- 
2.25.1


  reply	other threads:[~2023-10-16 15:38 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-16 15:40 [Intel-xe] [PATCH 0/1] x86/alternatives: Disable KASAN in apply_alternatives() fei.yang
2023-10-16 15:40 ` fei.yang [this message]
2023-10-16 16:13   ` [Intel-xe] [PATCH 1/1] " Lucas De Marchi
2023-10-16 17:03   ` Greg KH
2023-10-16 18:30     ` Yang, Fei
2023-10-16 15:41 ` [Intel-xe] ✓ CI.Patch_applied: success for " Patchwork
2023-10-16 15:42 ` [Intel-xe] ✗ CI.checkpatch: warning " Patchwork
2023-10-16 15:43 ` [Intel-xe] ✓ CI.KUnit: success " Patchwork
2023-10-16 15:50 ` [Intel-xe] ✓ CI.Build: " Patchwork
2023-10-16 15:50 ` [Intel-xe] ✓ CI.Hooks: " Patchwork
2023-10-16 15:52 ` [Intel-xe] ✓ CI.checksparse: " Patchwork
2023-10-16 16:25 ` [Intel-xe] ✓ CI.BAT: " Patchwork

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=20231016154025.3358622-2-fei.yang@intel.com \
    --to=fei.yang@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=lucas.demarchi@intel.com \
    --cc=peterz@infradead.org \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.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